InterviewSolution
| 1. |
What Is Map.entry In Map? |
|
Answer» Map.Entry is an interface in java.util, in fact Entry is a nested interface in Map interface. Nested interface must be QUALIFIED by the NAME of the class or interface of which it is a member, that's why it is qualified as Map.Entry. Map.Entry REPRESENTS a key/value pair that forms one element of a Map. The Map.entrySet method RETURNS a collection-view of the map, whose elements are of this class. As exp. If you have a Map called cityMap then using entrySet method you can get the set view of the map whose elements are of type Map.Entry, and then using getKey and getValue methods of the Map.Entry you can get the key and value pair of the Map. for(Map.Entry<String, String> entry: cityMap.entrySet()){ System.out.println("Key is " + entry.getKey() + " Value is " + entry.getValue()); } Map.Entry is an interface in java.util, in fact Entry is a nested interface in Map interface. Nested interface must be qualified by the name of the class or interface of which it is a member, that's why it is qualified as Map.Entry. Map.Entry represents a key/value pair that forms one element of a Map. The Map.entrySet method returns a collection-view of the map, whose elements are of this class. As exp. If you have a Map called cityMap then using entrySet method you can get the set view of the map whose elements are of type Map.Entry, and then using getKey and getValue methods of the Map.Entry you can get the key and value pair of the Map. for(Map.Entry<String, String> entry: cityMap.entrySet()){ System.out.println("Key is " + entry.getKey() + " Value is " + entry.getValue()); } |
|