2019-08-13 集合例子

package com.foreknow.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.foreknow.demo.Employee;

public class DemoCollection {
    public static void main(String[] args) {
        List> list = new ArrayList<>();
        
        Map m1 = new HashMap<>();
        Employee e1 = new Employee("1", "tom1", "work", 11.1);
        Employee e2 = new Employee("2", "tom2", "work2", 22.2);
        Employee e3 = new Employee("3", "tom3", "work3", 33.3);
        m1.put("e1", e1);
        m1.put("e2", e2);
        m1.put("e3", e3);
        
        Map m2 = new HashMap<>();
        Employee e4 = new Employee("4", "tom4", "work", 11.1);
        Employee e5 = new Employee("5", "tom5", "work2", 22.2);
        Employee e6 = new Employee("6", "tom6", "work3", 33.3);
        m2.put("e4", e4);
        m2.put("e5", e5);
        m2.put("e6", e6);
        
        Map m3 = new HashMap<>();
        Employee e7 = new Employee("7", "tom7", "work", 11.1);
        Employee e8 = new Employee("8", "tom8", "work2", 22.2);
        Employee e9 = new Employee("9", "tom9", "work3", 33.3);
        m3.put("e7", e7);
        m3.put("e8", e8);
        m3.put("e9", e9);
        
        list.add(m1);
        list.add(m2);
        list.add(m3);
        
        //获取员工信息
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);//m1
            //Map中没有迭代器,但是可以通过entry()方法返回一个Set集合就可以遍历了
            Set> set = map.entrySet();
            Iterator> it = set.iterator();
            while (it.hasNext()) {
                //获取到Map集合
                Map.Entry m = it.next();
                Employee e = m.getValue();
                System.out.println(e.getId()+"-"+e.getName());
            }
        }
    
    }
}

你可能感兴趣的:(2019-08-13 集合例子)