![]() ![]() Maintains key-value associations (pairs) so you can look up a value using a key. This results in a dramatic performance improvement. A HashMap takes the hashCode( ) of the object and uses it to quickly hunt for the key. All Java objects can produce a hash code, and hashCode( ) is a method in the root class Object. The hash code is a way to take some information in the object in question and turn it into a “relatively unique” int for that object. Instead of a slow search for the key, it uses a special value called a hash code. If you look at what must be done for a get( ), it seems pretty slow to search through (for example) an ArrayList for the key. The all have the same basic Map interface, but they differ in behaviors including efficiency, order in which the pairs are held and presented, how long the objects are held by the map, and how key equality is determined.Ī big issue with maps is performance. The standard Java library contains different types of Maps: HashMap, TreeMap, LinkedHashMap, WeakHashMap, and IdentityHashMap. You can also test a Map to see if it contains a key or a value with containsKey( ) and containsValue( ). get(Object key) produces the value given the corresponding key. The put(Object key, Object value) method adds a value (the thing you want) and associates it with a key (the thing you look it up with). The concept shows up in Java as the Map interface. Conceptually, it seems like an ArrayList, but instead of looking up objects using a number, you look them up using another object! This is a key technique in programming. ![]() Its selection criterion is “the last thing pushed on the stack.” A powerful twist on this idea of “selecting from a sequence” is termed a map, a dictionary, or an associative array (you saw a simple example of this in AssociativeArray.java in the previous chapter). But what if you’d like to select from a sequence of objects using some other criterion? A stack is an example. If you want to sort by the last name only then first line inside compareTo() method is enough.An ArrayList allows you to select from a sequence of objects using a number, so in a sense it associates numbers to objects. I wanted to sort the author names by last name first and if the last name is same then by first name. I have written the logic of sorting in the compareTo() method, you can write logic based on the requirement. Let’s take an example to understand this better: Example: Sorting Custom object by implementing Comparable interfaceĪs you can see I have implemented the Comparable interface in my Author class because I want to sort the objects of this class. Since this method is abstract, you must implement this method in your class if you implement the Comparable interface. This interface has only one method which is: public abstract int compareTo(T obj) However if you want to sort the objects of custom class then you need to implement the Comparable interface in our custom class. In the above example, you have seen that how easy it is to sort the Arrays and list of objects that implements Comparable interface, you just need to call the Collections.sort (and Arrays.sort). Sorted List: Apple, Banana, Grapes, Guava, Orange, (Arrays.toString(names)) įor(String s: fruits) (s+", ") * Interface so we can use the sort method ![]() Example: Sorting arrays and Wrapper class import Before we see how to sort an objects of custom objects, lets see how we can sort elements of arrays and Wrapper classes that already implements Comparable. Lists (and arrays) of objects that implement Comparable interface can be sorted automatically by Collections.sort (and Arrays.sort). Comparable interface is mainly used to sort the arrays (or lists) of custom objects. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |