In the last article, you have learned how to convert a List to Map in Java 8, and in this article, you will learn the opposite, like converting a Map to a List.ītw, if you are new to the Java world or still catching up after a long break from Java, you should join The Complete Java Masterclass course for more structured learning. Still, because of Streams and lambda expression, it seems you are doing everything in one step. But, values can be duplicated in Map hence API provides values() method which returns Collection.īefore Java 8, the problem of converting a Map to List is actually a problem of converting Map to Set and then to List, which is also not different in Java 8. Since Set cannot have duplicates and Map also cannot have duplicate keys, the Set data structure seems to be perfect for returning keys and entries. Hence, JDK API provides an equivalent method to convert a Map to Set, like keySet() will return a set of keys and entrySet() will return a set of entries. The API doesn't provide these methods because Map doesn't guarantee any order and the List interface guarantees ordering, like insertion order. Nissan wasn't found in map, thus a new pair was created!Īfter clear operation, size: 0 3.Hello guys, when you convert a Map to List in Java 8 or before, you have three choices like you can get a list of keys from Map, a List of values from Map, or a List of entries from Map, which encapsulates both keys and values. ("Nissan wasn't found in map, thus a new pair was created!") Īudi was found in the map and its value was updated! Val = (Integer) vehicles.putIfAbsent("Nissan", 9) ("Audi was found in the map and its value was updated!") Integer val = (Integer) vehicles.putIfAbsent("Audi", 9) Import Įnumeration elems = ((ConcurrentHashMap) vehicles).elements() Finally, this class allows neither keys nor values to be null.Ī simple example that uses a ConcurrentHashMap is shown below:ĬoncurrentHashMapExample.java: package Thus, this structure is safe to use in case of multiple threads. The class is a hash table that supports the full concurrency of retrievals. ("\nPrinting all values:") Īs you can observe, the elements of the map are printed in a strict lexicographic order, which does not appear in the previous examples of HashMap and HashTable. ("Lowest key: " ((TreeMap) vehicles).firstKey()) ("Highest key: " ((TreeMap) vehicles).lastKey()) Finally, this class is not synchronized and thus, if an application uses multiple threads, the map must be synchronized externally.Ī simple example that uses a TreeMap is shown below: Also, this class maintains order on its elements. The TreeMap is a Red-Black tree implementation that is sorted according to the natural ordering of its keys, or by a Comparator provided at the creation time. The next statements throw a NullPointerException, if uncommented.Īfter clear operation, size: 0 2.3 TreeMap This class contains two fundamental parameters: initial capacity and performance, with the same definitions as the HashMap class.Ī simple example that uses a HashTable is shown below: However, neither the key nor the value can be null. The HashTable class implements a hash table and maps keys to values. ("After clear operation, size: " vehicles.size()) Ī sample execution is shown below: Total vehicles: 4Īfter clear operation, size: 0 2.2 HashTable ("Found total " vehicles.get(searchKey) " " searchKey " cars!\n") Iterate over all vehicles, using the keySet method. The capacity is defined as the number of buckets in the hash table, while the load factor is a measure that indicates the maximum value the hash table can reach, before being automatically increased.Ī simple example that uses a HashMap is shown below: Finally, a HashMap contains two fundamental parameters: initial capacity and performance. Also, this class does not maintain any order among its elements and especially, it does not guarantee that the order will remain constant over time. A HashMap is a hash table-based implementation of the Map interface. The most common class that implements the Map interface is the Java HashMap. In this chapter, we will present the most common and used. There are many classes that implement the Java Map interface. size: Returns the number of key-value pairs currently in the map.remove: Removes the requested key and its value from the map, if the key exists.put: Adds the requested key-value pair in the map.keySet: Returns a Set that contains all keys of the map.entrySet: Returns a Set view of the mappings contained in this map.get: Retrieve the value of the requested key.equals: Compares an Object with the map for equality.containsValue: Returns true if the map contains the requested value.containsKey: Returns true if the map contains the requested key.clear: Removes all the elements from the map.
0 Comments
Leave a Reply. |