![]() TreeMap is the only Map with the subMap( ) method, which allows you to return a portion of the tree.Ī map of weak keys that allow objects referred to by the map to be released designed to solve certain types of problems. The point of a TreeMap is that you get the results in sorted order. When you view the keys or the pairs, they will be in sorted order (determined by Comparable or Comparator, discussed later). Implementation based on a red-black tree. Only slightly slower than a HashMap, except when iterating, where it is faster due to the linked list used to maintain the internal ordering. Like a HashMap, but when you iterate through it, you get the pairs in insertion order, or in least-recently-used (LRU) order. Performance can be adjusted via constructors that allow you to set the capacity and load factor of the hash table. (Use this instead of Hashtable.) Provides constant-time performance for inserting and locating pairs. 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. An ArrayList allows you to select from a sequence of objects using a number, so in a sense it associates numbers to objects. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |