Map:
A map contains values on the basis of key and value pair. Each key and value pair is known as an entry. Map contains only unique keys.
Map is useful if you have to search, update or delete elements on the basis of key.
Code:
- import java.util.*;
- public class MapDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Map<Integer, String>map=new HashMap<>();
- map.put(102, "Sujan");
- map.put(101, "Shimul");
- map.put(103, "Shakil");
- System.out.println("Print generic(new style)");
- print(map);
- System.out.println("\nPrint non-generic(old style)");
- Set set=map.entrySet();
- Iterator iterator=set.iterator();
- while (iterator.hasNext()) {
- Map.Entry xEntry=(Map.Entry)iterator.next();
- System.out.println(xEntry.getKey()+" "+xEntry.getValue());
- }
- }
- public static void print(Map<Integer, String>map) {
- for(Map.Entry m:map.entrySet()){
- System.out.println(m.getKey()+" "+m.getValue());
- }
- }
- }
0 Comments:
Post a Comment