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<Map<String, Employee>> list = new ArrayList<>();
Map<String, Employee> 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<String, Employee> 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<String, Employee> 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<String,Employee> map = list.get(i);//m1
//Map中没有迭代器,但是可以通过entry()方法返回一个Set集合就可以遍历了
Set<Map.Entry<String, Employee>> set = map.entrySet();
Iterator<Map.Entry<String, Employee>> it = set.iterator();
while (it.hasNext()) {
//获取到Map集合
Map.Entry<String, Employee> m = it.next();
Employee e = m.getValue();
System.out.println(e.getId()+"-"+e.getName());
}
}
}
}
2019-08-13 集合例子
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 贾志敏:“当年,我为了生活走上这三尺讲台。今天,我离开这三尺讲台则一刻也无法生活。” 贾志敏曾撰文称,叶圣陶先生早...
- 每次有朋友离职,出来聚的时候,都少不了会讲讲为什么离职。 根据我的不完全统计,“钱少”算主要原因,但并不算说服自己...
- Swift的数据类型有: 整型,Int 浮点型,Float 布尔型, Bool 字符串,String 元组,T...