大数据之路week02--day03 Map集合、Collections工具类的用法

时间:2023-03-09 15:49:11
大数据之路week02--day03 Map集合、Collections工具类的用法

1、Map(掌握)

  (1)将键映射到值的对象。一个映射不能包含重复的键:每个键最多只能映射到一个值。

  (2)Map和Collection的区别?

    A: Map 存储的是键值对形式的元素,键唯一,值可以重复。 理解为:夫妻对

    B: Collection存储的是单独出现的元素,子接口List元素可重复,子接口Set元素唯一。 理解为: 光棍

  (3)Map接口功能概述

    A: 添加功能

      V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。 

    B: 删除功能   

       void clear() 从该地图中删除所有的映射(可选操作)。
      V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。

    C: 判断功能   

      boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。  
      boolean containsValue(Object Value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。
      boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。

    D: 获取功能

      Set<Map.Entry<K,V>> entrySet() 返回此地图中包含的映射的Set视图。
      V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
      Set<K> keySet() 返回此地图中包含的键的Set视图。
      Collection<V> values() 返回此地图中包含的值的Collection视图。

    E: 长度功能

      int size() 返回此地图中键值映射的数量。
 package com.wyh.mapSummary;

 import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月20日 上午9:44:41
*
* * 1、添加功能
* V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
* 2、删除功能
* void clear() 从该地图中删除所有的映射(可选操作)。
* V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。
* 3、判断功能
* boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
* boolean containsValue(Object Value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。
* boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。
* 4、获取功能
* Set<Map.Entry<K,V>> entrySet() 返回此地图中包含的映射的Set视图。
* V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
* Set<K> keySet() 返回此地图中包含的键的Set视图。
* Collection<V> values() 返回此地图中包含的值的Collection视图。
* 5、长度功能
* int size() 返回此地图中键值映射的数量。
*/
public class Map01 {
public static void main(String[] args) {
//
Map<String,String> map = new HashMap<String,String>(); //V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
map.put("王友虎", "超人");
map.put("赵以浩", "绿巨人");
map.put("李宏灿", "奇异博士");
map.put("齐博源", "雷神"); //void clear() 从该地图中删除所有的映射(可选操作)。
//map.clear(); //V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。
//map.remove("王友虎"); //boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
//System.out.println("containsKey:"+map.containsKey("王友虎")); //boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。
//System.out.println("isEmpty:"+map.isEmpty()); //int size() 返回此地图中键值映射的数量。
//System.out.println("size:"+map.size()); //Set<K> keySet() 返回此地图中包含的键的Set视图。
Set<String> set = map.keySet();
for(String s : set) {
//V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
System.out.println(s+"---"+map.get(s));
} System.out.println(map); } }

  (4)Map集合的遍历

    A: 键找值

      a: 获取所有键的集合

      b: 遍历键的集合,得到每一个键

      c: 根据键到集合中去找值

 package com.wyh.map;

 import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月17日 下午7:51:02 *
*
* 根据键找值
*/
public class MapDemo3
{
public static void main(String[] args) {
//创建map集合
Map<String,String> map = new HashMap<String,String>(); //添加元素
map.put("小月", "小美");
map.put("李晨", "范冰冰");
map.put("邓超","孙俪");
map.put("黄晓明", "杨颖"); //获取到所有的key
Set<String> set = map.keySet(); //遍历
for(String key : set) {
String s = map.get(key);
System.out.println(key+"---"+s);
} } }

    B: 键值对对象找键和值

      a: 获取所有的键值对对象的集合

      b: 遍历键值对对象的集合,获取每一个键值对对象

      c: 根据键值对对象去获取键和值

 package com.wyh.map;

 import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月17日 下午8:13:52
*
* 根据键值对对象找键和值
*/
public class MapDemo4 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>(); //添加元素
map.put("小月", "小美");
map.put("李晨", "范冰冰");
map.put("邓超","孙俪");
map.put("黄晓明", "杨颖"); Set<Map.Entry<String,String>> set = map.entrySet(); for(Map.Entry<String,String> me : set) {
System.out.println(me.getKey()+"---"+me.getValue());
}
} }

  (5)HashMap集合的练习

    A: HashMap<String,String>

 package com.wyh.mapSummary;

 import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月20日 上午10:38:29
*/
public class Map02 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>(); // 添加元素
map.put("小月", "小美");
map.put("李晨", "范冰冰");
map.put("邓超", "孙俪");
map.put("黄晓明", "杨颖"); Set<Map.Entry<String, String>> set = map.entrySet(); for (Map.Entry<String, String> me : set) {
System.out.println(me.getKey() + "---" + me.getValue());
}
} }

    B: HashMap<Integer,String>

 package com.wyh.mapSummary;

 import java.util.HashMap;
import java.util.Set; /**
* @author WYH
* @version 2019年11月20日 上午11:02:16
*/
public class Map03 {
public static void main(String[] args) {
HashMap<Integer,String> hm = new HashMap<Integer,String>(); hm.put(1, "王友虎");
hm.put(2, "赵以浩");
hm.put(3, "齐博源");
hm.put(4, "李先锋");
hm.put(5, "李宏灿");
hm.put(6, "张国兴"); Set<Integer> set = hm.keySet(); for(Integer i : set) {
System.out.println(i+"---"+hm.get(i));
} } }

    C: HashMap<String,Student>

 package com.wyh.mapSummary;

 import java.util.HashMap;
import java.util.Set; import com.wyh.map.Student; /**
* @author WYH
* @version 2019年11月20日 上午11:13:55
*/
public class Map04 {
public static void main(String[] args) {
HashMap<String, Student> hm = new HashMap<String, Student>(); // 创建对象
Student s1 = new Student("王友虎", 22);
Student s2 = new Student("赵以浩", 24);
Student s3 = new Student("齐博源", 21);
Student s4 = new Student("李先锋", 22);
Student s5 = new Student("李宏灿", 22); // 添加到集合中去
hm.put("1", s1);
hm.put("2", s2);
hm.put("3", s3);
hm.put("4", s4);
hm.put("5", s5); Set<String> set = hm.keySet();
for(String s : set) {
System.out.println(s+"--"+hm.get(s).getName()+"--"+hm.get(s).getAge());
} } }

    D: HashMap<Student,String>  容易忽略一点,为了保证键值相同,是一个学生类,而且是由HashMap存储, 就要重写,hashCode()和equals()方法

学生类:

 package com.wyh.mapSummary;

 /**
* @author WYH
* @version 2019年11月19日 下午7:01:40
*/
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} }

测试类:

 package com.wyh.map;

 import java.util.HashMap;
import java.util.Set; /**
* @author WYH
* @version 2019年11月19日 下午7:20:14
*
* <Student,String>
*
* 容易忽略一点,为了保证键值相同,是一个学生类,而且是由HashMap存储, 就要重写,hashCode()和equals()方法
*/
public class HashMapDemo2 {
public static void main(String[] args) {
//创建集合对象
HashMap<Student,String> hm = new HashMap<Student,String>(); //创建对象
Student s1 = new Student("王友虎",22);
Student s2 = new Student("赵以浩",24);
Student s3 = new Student("齐博源",21);
Student s4 = new Student("李先锋",22);
Student s5 = new Student("李宏灿",22);
Student s6 = new Student("王友虎",22); //添加到集合
hm.put(s1, "001");
hm.put(s2, "002");
hm.put(s3, "003");
hm.put(s4, "004");
hm.put(s5, "005");
hm.put(s6, "006"); Set<Student> set = hm.keySet(); for(Student key : set) {
String s = hm.get(key);
System.out.println(key.getName()+"---"+key.getAge()+"---"+s);
} } }

  (6)TreeMap的练习

    A: TreeMap<String,String>

 package com.wyh.mapSummary;

 import java.util.Set;
import java.util.TreeMap; /**
* @author WYH
* @version 2019年11月20日 下午2:06:17
*/
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<String,String> tm = new TreeMap<String,String>(); tm.put("123", "张三");
tm.put("143", "李四");
tm.put("123", "张三");
tm.put("1123", "489");
tm.put("1423", "789");
tm.put("133", "999"); Set<String> s = tm.keySet(); for(String s1 : s) {
System.out.println(tm.get(s1)); } } }

    B: TreeMap<Student,String>  无参构造方法默认调用的是自然排序,但是这里要自己重写方法

 package com.wyh.map;

 import java.util.Comparator;

 import java.util.Set;
import java.util.TreeMap; /**
* @author WYH
* @version 2019年11月19日 下午7:54:29
* 无参构造方法默认调用的是自然排序,但是这里要自己重写方法
*
*
*/
public class TreeMapDemo2 {
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<Student,String>(new Comparator<Student>() { @Override
public int compare(Student s1, Student s2) {
int num = s1.getName().length() - s2.getName().length();
int num1 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
int num2 = num1 == 0 ? s1.getAge() - s2.getAge() : num1;
return num2;
} }); //创建对象
Student s1 = new Student("王友虎",22);
Student s2 = new Student("赵以浩",24);
Student s3 = new Student("齐博源",21);
Student s4 = new Student("李先锋",22);
Student s5 = new Student("李宏灿",22);
Student s6 = new Student("王友虎",22); tm.put(s1, "六期");
tm.put(s2, "五期");
tm.put(s3, "六期");
tm.put(s4, "七期");
tm.put(s5, "六期");
tm.put(s6, "六期"); Set<Student> set = tm.keySet(); for(Student s : set) {
System.out.println(s.getName()+"---"+s.getAge()+"---"+tm.get(s));
} } }