1.定义:无序,以键值(key,value)对的形式存储数据,键唯一,值不唯一;
注意:
1)键key的底层结构为Set集合,其特征之一是无序;
2)值value的底层结构为Collection集合,其特征之一是对象可重复;
3)Map集合不继承Collection接口
2.遍历方式(3种):
1)获取键key
2)获取值value
3)获取键值对
//定义一个Map集合,key和value不规定类型,任意放入数据,用keySet()和entrySet()及value()3种方式遍历出Map集合的数据
Map<String, Student> map=new HashMap<>();
map.put("T22", new Student("梅子1", 88.8));
map.put("T23", new Student("梅子2", 88.8));
map.put("T24", new Student("梅子3", 88.8));
map.put("T25", new Student("梅子4", 88.8));
map.put("T26", new Student("梅子5", 88.8));
//获取键
Set<String> ks = map.keySet();
System.out.println("--------键遍历-------------------");
for (String str : ks) {
Student stu = map.get(str);
System.out.println(str+","+stu.getSname()+","+stu.getScore());
}
System.out.println("----------值遍历------------");
Set<Entry<String, Student>> es = map.entrySet();
/*for (Entry sr : es) {
System.out.println(sr);
}*/
Iterator<Entry<String, Student>> it2 = es.iterator();
while(it2.hasNext()) {
System.out.println(it2.next());
}
// 定义一个Map集合,key和value不规定类型,任意放入数据,单独拿到所有的value的数据
System.out.println("----------单独取值-----------------");
Map<String, String> map2=new HashMap<>();
map2.put("a", "君君1");
map2.put("b", "君君2");
map2.put("c", "君君3");
map2.put("d", "君君4");
Set<Entry<String, String>> es2 = map2.entrySet();
/*for (Entry st : es2) {
System.out.println(st);
}*/
System.out.println("----------键值对-----------------");
Iterator<Entry<String, String>> it = es2.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
public static void main(String[] args) {
Map<String, Map<String, List<Student>>> map = new HashMap<>();
// 每一个部
Map<String, List<Student>> m1 = new HashMap<>();// 初中部
Map<String, List<Student>> m2 = new HashMap<>();// 高中部
Map<String, List<Student>> m3 = new HashMap<>();// 大学部
// 初中部
List<Student> lst1 = new ArrayList<>();
lst1.add(new Student("张晓东", 90.3));
lst1.add(new Student("张晓西", 70.3));
List<Student> lst2 = new ArrayList<>();
lst2.add(new Student("张晓北", 90.3));
lst2.add(new Student("张晓南", 82.3));
m1.put("T110", lst1);
m1.put("T111", lst2);
map.put("初中部", m1);
// 高中部
List<Student> lst3 = new ArrayList<>();
lst3.add(new Student("张三", 90.3));
lst3.add(new Student("李四", 70.3));
List<Student> lst4 = new ArrayList<>();
lst4.add(new Student("王五", 90.3));
lst4.add(new Student("小六", 82.3));
List<Student> lst5 = new ArrayList<>();
lst5.add(new Student("小八 ", 90.3));
lst5.add(new Student("小酒", 82.3));
m2.put("T206", lst3);
m2.put("T222", lst4);
m2.put("T208", lst5);
map.put("高中部", m2);
// 大学部
List<Student> lst6 = new ArrayList<>();
lst6.add(new Student("可乐", 90.3));
lst6.add(new Student("雪碧", 70.3));
List<Student> lst7 = new ArrayList<>();
lst7.add(new Student("哇哈哈", 90.3));
lst7.add(new Student("老干妈", 82.3));
m3.put("T230", lst6);
m3.put("T231", lst7);
map.put("大学部", m3);
Set<String> ks = map.keySet();
for (String str : ks) {
Map<String, List<Student>> mp = map.get(str);
Set<Entry<String, List<Student>>> sy = mp.entrySet();
for (Entry<String, List<Student>> sr : sy) {
String key = sr.getKey();
List<Student> val = sr.getValue();
Iterator<Student> it5 = val.iterator();
while (it5.hasNext()) {
System.out.println(it5.next() + "," + key + "," + str);
}
}
}
}
Map<String, String> map2=new HashMap<>();
map2.put("a", "君君1");
map2.put("b", "君君2");
map2.put("c", "君君3");
map2.put("d", "君君4");
Set<Entry<String, String>> es2 = map2.entrySet();
/*for (Entry st : es2) {
System.out.println(st);
}*/
System.out.println("----------键值对-----------------");
Iterator<Entry<String, String>> it = es2.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
// 定义一个Map集合,key和value不规定类型,针对集合中的元素进行排序
System.out.println("------------键排序------------------------");
TreeMap<String, Student> tmap=new TreeMap<>();
tmap.put("d", new Student("梅子4", 88.84));
tmap.put("c", new Student("梅子3", 88.83));
tmap.put("b", new Student("梅子1", 88.81));
tmap.put("a", new Student("梅子2", 88.82));
Set<String> ksd = tmap.keySet();
Iterator<String> it3 = ksd.iterator();
while(it3.hasNext()) {
System.out.println(it3.next());
}
System.out.println("------------值排序-------------------------");
/*Collection va = tmap.values();
Iterator it4 = va.iterator();
while(it4.hasNext()) {
System.out.println(it4.next());//其实也是根据键排序
}*/
System.out.println("------------键值对排序-------------------------");
Set<Entry<String, Student>> es3 = tmap.entrySet();
Iterator<Entry<String, Student>> it4 = es3.iterator();
while(it4.hasNext()) {
System.out.println(it4.next());//也是根据键排序
}