Before converting a Map to a List in Java, we should be very clear about these data structures which is widely used in Java. So lets begin with Map. What is Map? Map is an Interface in Java which store key and value object. It's Java representation of popular hash table data structure which allows you to search an existing element in O(1) time, at the same time also makes insertion and removal easier. We use key object to retrieve the value object by using hashing functionality provided by Map. As we have seen in how get method of HashMap works, In Java, equals() and hashcode() method are integral part of storing and retrieving object from it. Map allows duplicate values but no duplicate keys. Map has its implementation in various classes like HashMap, ConcurrentHashMap and TreeMap. The Map interface also provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. Now let's understand what is a List in Java, It is also an interface to provide an ordered collection based on index. Elements can be inserted and deleted using positions and duplicate elements are allowed in the list. There are multiple implementation of List is available in Java e.g. ArrayList, which is based in array and LinkedList which is based in linked list data structure. Now let's see a Java program which is used to convert a Map to List.
Read more »
No comments:
Post a Comment