Beginners Learn Java Lesson 17 Map Interface and Iterator

Posted by Pavel_Nedved on Tue, 30 Jul 2019 21:37:17 +0200

Map interface
Classes that implement the Map interface are used to store key-value pairs.
The implementation classes of Map interface include HashMap and TreeMap.
Key-value pairs stored in Map classes are identified by keys, so key values (as indexes) cannot be repeated (if two objects equals each other, their hashCode s must be the same), and if repeated, the contents corresponding to the same key value will be overwritten.
The corresponding usage is as follows:

public class TestM {
    public static void main(String[] args) {

        Map<Integer,String> m=new HashMap<>();
        m.put(1,"aa");//Deposit
        m.put(2,"bb");
        m.put(3,"cc");
        System.out.println(m.get(2));//take out
        System.out.println(m.size());
        System.out.println(m.isEmpty());
        System.out.println(m.containsKey(1));
        System.out.println(m.containsValue("dd"));
        Map<Integer,String> m1=new HashMap<>();
        m1.put(1,"ss");
        m.putAll(m1);
        System.out.println(m);
        //Keys cannot be repeated
        m.put(2,"ww");
        System.out.println(m);
    }
}

public class TestM1 {
    public static void main(String[] args) {
        People p1=new People(123,"tom",3000);
        People p2=new People(133,"jerry",3500);
        People p3=new People(143,"tim",4000);
        Map<Integer,People> m=new HashMap<>();
        m.put(123,p1);
        m.put(133,p2);
        m.put(143,p3);
        People pe=m.get(123);
        System.out.println(pe.getName());
        System.out.println(pe);

    }
}

class People{
    private int id;
    private String name;
    private double salary;

    public People (int id,String name,double salary){
        super();
        this.id=id;
        this.name=name;
        this.salary=salary;
    }
    @Override
    public  String toString(){
        return "id:"+id+"name:"+name+"salary"+salary;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }
}


iterator
Overview: Help us do set traversal tools.
What it does: There are many collections in java that use different storage methods when storing elements. So when we want to extract elements from these sets, we can do it in a general way.
Traverse list, set, map with iterator:

public class Testd {
    public static void main(String[] args) {
        test();
        testset();
        testmap();
    }
    public  static void test(){
        List<String> list=new ArrayList<>();
        list.add("sss");
        list.add("www");
        list.add("qqq");
        //Traversal using iterators
        for (Iterator<String> it = list.iterator(); ((Iterator) it).hasNext();){
            Object t= ((Iterator) it).next();
            System.out.println(t);


        }

        }
    public  static void testset(){
        Set<String> set = new HashSet<>();
        set.add("sss");
        set.add("www");
        set.add("qqq");
        //Traversal using iterators
        for (Iterator<String> it = set.iterator(); ((Iterator) it).hasNext(); ) {
            Object t = ((Iterator) it).next();
            System.out.println(t);
        }

    }
    public  static void testmap() {
        Map<Integer,String> map = new HashMap<>();
        map.put(10,"sss");
        map.put(20,"www");
        map.put(30,"eee");
        //Traversal using iterators
        Set<Map.Entry<Integer,String>> ss=map.entrySet();
        for(Iterator<Map.Entry<Integer,String>>it=ss.iterator();it.hasNext();){
            Map.Entry<Integer,String> t=it.next();
            System.out.println(t.getKey()+"--"+t.getValue());
        }
    }
}

Topics: Java