Java Generic And Collection Basic

  1. 深入了解需要精读Java Generics and Collections

  2. 泛读材料40 Java Collections Interview Questions and Answers

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        list.add("Robin");
        list.add("TianYa");
        list.add("Generic");
        
        //Three Way Iterator List
        System.out.println("***For Loop***");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        
        System.out.println("***Foreach Loop***");
        for (String n : list) {
            System.out.println(n);
        }
        
        System.out.println("***Iterator***");
        for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }
        
        //List Reference Object
        List<Object> listObjcet = new ArrayList<>();
        
        listObjcet.add(new PersonInfo(1, "Robin", "Java"));
        listObjcet.add(new PersonInfo(2, "Tianya", "C++"));
        listObjcet.add(new PersonInfo(3, "Collection", "C"));

        System.out.println("***For Loop Object***");
        for (Iterator<Object> it = listObjcet.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }
        
    }

}
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Set<String> s1 = new HashSet<String>();
        
        s1.add("Robin");
        s1.add("TianYa");
        s1.add("SetCollection");

        System.out.println("***Foreach Loop***");
        for (String n : s1) {
            System.out.println(n);
        }
        
        System.out.println("***Iterator***");
        for (Iterator<String> it = s1.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }
        
        if (s1.contains("TianYa")) {
            System.out.println("There Hava Tianya");
            System.out.println(s1.size());
            System.out.println(s1.hashCode());
        }

        Set<Object> setObjcet = new HashSet<Object>();
        
        setObjcet.add(new PersonInfo(1, "Robin", "Java"));
        setObjcet.add(new PersonInfo(2, "Tianya", "C++"));
        setObjcet.add(new PersonInfo(3, "Collection", "C"));

        System.out.println("***For Loop Object***");
        for (Iterator<Object> it = setObjcet.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }
    }

}
public class PersonInfo {
    private int id;
    private String name;
    private String language;
    
    public PersonInfo(int id, String name, String language) {
        super();
        this.id = id;
        this.name = name;
        this.language = language;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    @Override
    public String toString() {
        return "PersonInfo [id=" + id + ", name=" + name + ", language=" + language + "]";
    }

}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class MapTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<String, String> map1 = new HashMap<String, String>();
        
        map1.put("Name", "Robin");
        map1.put("Hello", "World");
        map1.put("Tian", "Ya");
        
        System.out.println(map1.get("Name"));
        System.out.println(map1.keySet());
        
        //For Loop Iterator
        System.out.println("***For***");
        for (Map.Entry<String, String> entry : map1.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
        
        //Iterator Generic
        System.out.println("***Generic Iterator***");
        Iterator<Map.Entry<String, String>> it = map1.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            System.out.println("Key " + entry.getKey() + " value " + entry.getValue());
        }
        
        //Iterator base Key
        System.out.println("***Base For***");
        for (String key : map1.keySet()) {
            String values = map1.get(key);
            System.out.println("Key " + key + " value " + values);
        }
        

    }

}

标签: none

添加新评论