Day18

发布时间:2023年12月20日

Day18

一,Map

1,HashMap

1.1HashMap的使用
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test01 {
	/**
	 * 知识点:HashMap的使用
	 */
	public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();
		
		//添加元素
		Integer put1 = map.put("麻生希", 24);
		Integer put2 = map.put("椎名空", 21);
		Integer put3 = map.put("水菜丽", 26);
		Integer put4 = map.put("朝桐光", 29);
		Integer put5 = map.put("樱井步", 26);
		
		//替换元素
		Integer put6 = map.put("麻生希", 25);
		
		//put()的返回值:如果第一次添加key就返回null,如果就重复key就替换value并返回被替换的值
		System.out.println("put1:" + put1);
		System.out.println("put2:" + put2);
		System.out.println("put3:" + put3);
		System.out.println("put4:" + put4);
		System.out.println("put5:" + put5);
		System.out.println("put6:" + put6);
		
		//替换元素
		map.replace("麻生希", 26);
		
		//替换元素
		map.replace("麻生希", 26, 27);
		
		//将newMap中所有的元素添加到map中
		HashMap<String, Integer> newMap = new HashMap<>();
		newMap.put("aaa", 10);
		newMap.put("bbb", 10);
		newMap.put("ccc", 10);
		map.putAll(newMap);
		
		//如果有key就查询出对应的value,如果没key就添加
		Integer putIfAbsent = map.putIfAbsent("麻生希1", 26);
		System.out.println("putIfAbsent:" + putIfAbsent);
		
		//通过key获取对应的value
		Integer integer = map.get("椎名空");
		System.out.println("通过key获取对应的value:" + integer);//21
		
		//通过key获取对应的value,如果没有key就返回默认值
		Integer orDefault = map.getOrDefault("椎名空111", 666);
		System.out.println("通过key获取对应的value:" + orDefault);//666
		
		//依据key删除元素
		map.remove("aaa");
		//依据key+value删除元素
		map.remove("bbb", 10);
		
		//清空集合中所有的元素
		//map.clear();
		
		System.out.println("判断集合中是否有包含的Key:" + map.containsKey("椎名空"));//true
		System.out.println("判断集合中是否有包含的Value:" + map.containsValue(26));//true
		System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
		System.out.println("获取元素个数:" + map.size());
		
		//获取map中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));
		
		System.out.println("-----------------------");
		
		//遍历方式一:
		//keySet():获取map集合中所有的key,返回Set集合
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + " -- " + value);
		}
		
		System.out.println("-----------------------");
		
		//遍历方式二:
		//entrySet():获取map集合中所有的映射关系对象,返回Set集合
		Set<Entry<String, Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + " -- " + value);
		}	
	}
}
1.2HashMap的特点
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test02 {
	/**
	 * 知识点:HashMap的特点
	 * 
	 * 特点:无序+key唯一
	 */
	public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();
		
		//添加元素
		map.put("麻生希", 24);
		map.put("椎名空", 21);
		map.put("水菜丽", 26);
		map.put("朝桐光", 29);
		map.put("樱井步", 26);
		map.put("麻生希", 25);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry);
		}
	}
}
1.3,HashMap的value排序
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test03 {
	/**
	 * 知识点:HashMap的面试题
	 * 
	 * 需求:HashMap的value排序
	 */
	public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();
		
		//添加元素
		map.put("麻生希", 24);
		map.put("椎名空", 21);
		map.put("水菜丽", 26);
		map.put("朝桐光", 29);
		map.put("樱井步", 26);
		
		//将map集合中所有的映射关系取出,并放入Set集合中
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		
		//将Set集合转换为ArrayList
		ArrayList<Entry<String, Integer>> list = new ArrayList<>(entrySet);
		
		//设置ArrayList的排序
		list.sort(new Comparator<Entry<String, Integer>>() {
			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				Integer value1 = o1.getValue();
				Integer value2 = o2.getValue();
				return value1 - value2;
			}
		});
		
		for (Entry<String, Integer> entry : list) {
			System.out.println(entry);
		}
	}
}

2,LinkedHashMap

2.1,LinkedHashMap的使用
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test01 {
	/**
	 * 知识点:LinkedHashMap的使用
	 */
	public static void main(String[] args) {
		
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
		
		//添加元素
		Integer put1 = map.put("麻生希", 24);
		Integer put2 = map.put("椎名空", 21);
		Integer put3 = map.put("水菜丽", 26);
		Integer put4 = map.put("朝桐光", 29);
		Integer put5 = map.put("樱井步", 26);
		
		//替换元素
		Integer put6 = map.put("麻生希", 25);
		
		//put()的返回值:如果第一次添加key就返回null,如果就重复key就替换value并返回被替换的值
		System.out.println("put1:" + put1);
		System.out.println("put2:" + put2);
		System.out.println("put3:" + put3);
		System.out.println("put4:" + put4);
		System.out.println("put5:" + put5);
		System.out.println("put6:" + put6);
		
		//替换元素
		map.replace("麻生希", 26);
		
		//替换元素
		map.replace("麻生希", 26, 27);
		
		//将newMap中所有的元素添加到map中
		LinkedHashMap<String, Integer> newMap = new LinkedHashMap<>();
		newMap.put("aaa", 10);
		newMap.put("bbb", 10);
		newMap.put("ccc", 10);
		map.putAll(newMap);
		
		//如果有key就查询出对应的value,如果没key就添加
		Integer putIfAbsent = map.putIfAbsent("麻生希1", 26);
		System.out.println("putIfAbsent:" + putIfAbsent);
		
		//通过key获取对应的value
		Integer integer = map.get("椎名空");
		System.out.println("通过key获取对应的value:" + integer);//21
		
		//通过key获取对应的value,如果没有key就返回默认值
		Integer orDefault = map.getOrDefault("椎名空111", 666);
		System.out.println("通过key获取对应的value:" + orDefault);//666
		
		//依据key删除元素
		map.remove("aaa");
		//依据key+value删除元素
		map.remove("bbb", 10);
		
		//清空集合中所有的元素
		//map.clear();
		
		System.out.println("判断集合中是否有包含的Key:" + map.containsKey("椎名空"));//true
		System.out.println("判断集合中是否有包含的Value:" + map.containsValue(26));//true
		System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
		System.out.println("获取元素个数:" + map.size());
		
		//获取map中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));
		
		System.out.println("-----------------------");
		
		//遍历方式一:
		//keySet():获取map集合中所有的key,返回Set集合
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + " -- " + value);
		}
		
		System.out.println("-----------------------");
		
		//遍历方式二:
		//entrySet():获取map集合中所有的映射关系对象,返回Set集合
		Set<Entry<String, Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + " -- " + value);
		}
	}
}
2.2,LinkedHashMap的特点
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test02 {
	/**
	 * 知识点:LinkedHashMap的特点
	 * 
	 * 继承关系:class LinkedHashMap extends HashMap
	 * 特点:有序+key唯一
	 */
	public static void main(String[] args) {
		
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
		
		//添加元素
		map.put("麻生希", 24);
		map.put("椎名空", 21);
		map.put("水菜丽", 26);
		map.put("朝桐光", 29);
		map.put("樱井步", 26);
		map.put("麻生希", 25);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry);
		}
	}
}

3,Hashtable

3.1,Hashtable的使用
import java.util.Arrays;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;

public class Test01 {
	/**
	 * 知识点:Hashtable的使用
	 */
	public static void main(String[] args) {
		
		Hashtable<String, Integer> map = new Hashtable<>();
		
		//添加元素
		Integer put1 = map.put("麻生希", 24);
		Integer put2 = map.put("椎名空", 21);
		Integer put3 = map.put("水菜丽", 26);
		Integer put4 = map.put("朝桐光", 29);
		Integer put5 = map.put("樱井步", 26);
		
		//替换元素
		Integer put6 = map.put("麻生希", 25);
		
		//put()的返回值:如果第一次添加key就返回null,如果就重复key就替换value并返回被替换的值
		System.out.println("put1:" + put1);
		System.out.println("put2:" + put2);
		System.out.println("put3:" + put3);
		System.out.println("put4:" + put4);
		System.out.println("put5:" + put5);
		System.out.println("put6:" + put6);
		
		//替换元素
		map.replace("麻生希", 26);
		
		//替换元素
		map.replace("麻生希", 26, 27);
		
		//将newMap中所有的元素添加到map中
		Hashtable<String, Integer> newMap = new Hashtable<>();
		newMap.put("aaa", 10);
		newMap.put("bbb", 10);
		newMap.put("ccc", 10);
		map.putAll(newMap);
		
		//如果有key就查询出对应的value,如果没key就添加
		Integer putIfAbsent = map.putIfAbsent("麻生希1", 26);
		System.out.println("putIfAbsent:" + putIfAbsent);
		
		//通过key获取对应的value
		Integer integer = map.get("椎名空");
		System.out.println("通过key获取对应的value:" + integer);//21
		
		//通过key获取对应的value,如果没有key就返回默认值
		Integer orDefault = map.getOrDefault("椎名空111", 666);
		System.out.println("通过key获取对应的value:" + orDefault);//666
		
		//依据key删除元素
		map.remove("aaa");
		//依据key+value删除元素
		map.remove("bbb", 10);
		
		//清空集合中所有的元素
		//map.clear();
		
		System.out.println("判断集合中是否有包含的Key:" + map.containsKey("椎名空"));//true
		System.out.println("判断集合中是否有包含的Value:" + map.containsValue(26));//true
		System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
		System.out.println("获取元素个数:" + map.size());
		
		//获取map中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));
		
		System.out.println("-----------------------");
		
		//遍历方式一:
		//keySet():获取map集合中所有的key,返回Set集合
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + " -- " + value);
		}
		
		System.out.println("-----------------------");
		
		//遍历方式二:
		//entrySet():获取map集合中所有的映射关系对象,返回Set集合
		Set<Entry<String, Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + " -- " + value);
		}
	}
}
3.2,Hashtable的特点
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;

public class Test02 {
	/**
	 * 知识点:Hashtable的特点
	 * 
	 * 特点:无序+key唯一+线程安全
	 */
	public static void main(String[] args) {
		
		Hashtable<String, Integer> map = new Hashtable<>();
		
		//添加元素
		map.put("麻生希", 24);
		map.put("椎名空", 21);
		map.put("水菜丽", 26);
		map.put("朝桐光", 29);
		map.put("樱井步", 26);
		map.put("麻生希", 25);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry);
		}
	}
}

4,TreeMap

4.1,TreeMap的使用

import java.util.Arrays;
import java.util.Collection;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test01 {
	/**
	 * 知识点:TreeMap的使用
	 */
	public static void main(String[] args) {
		
		TreeMap<String, Integer> map = new TreeMap<>();
		
		//添加元素
		Integer put1 = map.put("麻生希", 24);
		Integer put2 = map.put("椎名空", 21);
		Integer put3 = map.put("水菜丽", 26);
		Integer put4 = map.put("朝桐光", 29);
		Integer put5 = map.put("樱井步", 26);
		
		//替换元素
		Integer put6 = map.put("麻生希", 25);
		
		//put()的返回值:如果第一次添加key就返回null,如果就重复key就替换value并返回被替换的值
		System.out.println("put1:" + put1);
		System.out.println("put2:" + put2);
		System.out.println("put3:" + put3);
		System.out.println("put4:" + put4);
		System.out.println("put5:" + put5);
		System.out.println("put6:" + put6);
		
		//替换元素
		map.replace("麻生希", 26);
		
		//替换元素
		map.replace("麻生希", 26, 27);
		
		//将newMap中所有的元素添加到map中
		TreeMap<String, Integer> newMap = new TreeMap<>();
		newMap.put("aaa", 10);
		newMap.put("bbb", 10);
		newMap.put("ccc", 10);
		map.putAll(newMap);
		
		//如果有key就查询出对应的value,如果没key就添加
		Integer putIfAbsent = map.putIfAbsent("麻生希1", 26);
		System.out.println("putIfAbsent:" + putIfAbsent);
		
		//通过key获取对应的value
		Integer integer = map.get("椎名空");
		System.out.println("通过key获取对应的value:" + integer);//21
		
		//通过key获取对应的value,如果没有key就返回默认值
		Integer orDefault = map.getOrDefault("椎名空111", 666);
		System.out.println("通过key获取对应的value:" + orDefault);//666
		
		//依据key删除元素
		map.remove("aaa");
		//依据key+value删除元素
		map.remove("bbb", 10);
		
		//清空集合中所有的元素
		//map.clear();
		
		System.out.println("判断集合中是否有包含的Key:" + map.containsKey("椎名空"));//true
		System.out.println("判断集合中是否有包含的Value:" + map.containsValue(26));//true
		System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
		System.out.println("获取元素个数:" + map.size());
		
		//获取map中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));
		
		System.out.println("-----------------------");
		
		//遍历方式一:
		//keySet():获取map集合中所有的key,返回Set集合
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + " -- " + value);
		}
		
		System.out.println("-----------------------");
		
		//遍历方式二:
		//entrySet():获取map集合中所有的映射关系对象,返回Set集合
		Set<Entry<String, Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + " -- " + value);
		}
	}
}
4.2,TreeMap的特点
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test02 {
	/**
	 * 知识点:TreeMap的特点
	 * 
	 * 特点:key做自然排序
	 */
	public static void main(String[] args) {
		
		TreeMap<String, Integer> map = new TreeMap<>();
		
		//添加元素
		map.put("b", 24);
		map.put("a", 21);
		map.put("d", 26);
		map.put("c", 29);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry);
		}
	}
}
4.3,TreeMap存储自定义类型 - 使用内置比较器
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class Test03 {
	/**
	 * 知识点:TreeMap存储自定义类型 - 使用内置比较器
	 */
	public static void main(String[] args) {
		
		TreeMap<Student, String> map = new TreeMap<>();
		
		//添加元素
		map.put(new Student("麻生希", '女', 26, "2308", "001"),"写代码");        
		map.put(new Student("椎名空", '女', 23, "2308", "002"),"拍电影");        
		map.put(new Student("水菜丽", '女', 29, "2308", "003"),"玩游戏");        
		map.put(new Student("朝桐光", '女', 32, "2308", "004"),"品茗");        
		map.put(new Student("北岛玲", '女', 42, "2308", "005"),"对弈");        
		map.put(new Student("三上悠亚", '女', 31, "2308", "006"),"闻香");       
		map.put(new Student("古川伊织", '女', 24, "2308", "007"),"探幽");       
		map.put(new Student("濑亚美莉", '女', 21, "2308", "008"),"侯月");       
		map.put(new Student("深田咏美", '女', 23, "2308", "009"),"赏花");       
		map.put(new Student("北条麻衣", '女', 35, "2308", "010"),"对饮");       
		map.put(new Student("徐灿", '男', 23, "2308", "012"),"打游戏");         
		map.put(new Student("彭鹏", '男', 26, "2309", "007"),"打羽毛球");         
		map.put(new Student("周隽乐", '男', 27, "2309", "008"),"睡觉");   
		
		Set<Entry<Student,String>> entrySet = map.entrySet();
		for (Entry<Student, String> entry : entrySet) {
			System.out.println(entry);
		}
	}
}
//学生类实现Comparable接口
public class Student implements Comparable<Student>{

	private String name;
	private char sex;
	private int age;
	private String classId;
	private String id;
	
	public Student() {
	}
	
	public Student(String classId, String id) {
		this.classId = classId;
		this.id = id;
	}

	public Student(String name, char sex, int age, String classId, String id) {
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.classId = classId;
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj){
			return true;
		}
		
		if(obj instanceof Student){
			Student stu = (Student) obj;
			if(this.classId.equals(stu.classId) && this.id.equals(stu.id)){
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return name + "\t" + sex + "\t" + age + "\t" + classId + "\t" + id;
	}

	//排序规则:按照学生年龄排序
	@Override
	public int compareTo(Student o) {
		return this.age - o.age;
	}
}
4.4,TreeMap存储自定义类型 - 使用外置比较器
import java.util.Map.Entry;
import com.qf.extends01.Student;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;

public class Test04 {
	/**
	 * 知识点:TreeMap存储自定义类型 - 使用外置比较器
	 */
	public static void main(String[] args) {
		
		TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
			//排序规则:按照名字长度排序,长度一致按照年龄排序
			@Override
			public int compare(Student o1, Student o2) {
				if(o1.equals(o2)){
					return 0;
				}
				
				int length1 = o1.getName().length();
				int length2 = o2.getName().length();
				if(length1 != length2){
					return length1 - length2;
				}
				
				int age1 = o1.getAge();
				int age2 = o2.getAge();
				if(age1 != age2){
					return age1 - age2;
				}
				return 1;
			}
		});
		
		//添加元素
		map.put(new Student("麻生希", '女', 26, "2308", "001"),"写代码");        
		map.put(new Student("椎名空", '女', 23, "2308", "002"),"拍电影");        
		map.put(new Student("水菜丽", '女', 29, "2308", "003"),"玩游戏");        
		map.put(new Student("朝桐光", '女', 32, "2308", "004"),"品茗");        
		map.put(new Student("北岛玲", '女', 42, "2308", "005"),"对弈");        
		map.put(new Student("三上悠亚", '女', 31, "2308", "006"),"闻香");       
		map.put(new Student("古川伊织", '女', 24, "2308", "007"),"探幽");       
		map.put(new Student("濑亚美莉", '女', 21, "2308", "008"),"侯月");       
		map.put(new Student("深田咏美", '女', 23, "2308", "009"),"赏花");       
		map.put(new Student("北条麻衣", '女', 35, "2308", "010"),"对饮");       
		map.put(new Student("徐灿", '男', 23, "2308", "012"),"打游戏");         
		map.put(new Student("彭鹏", '男', 26, "2309", "007"),"打羽毛球");         
		map.put(new Student("周隽乐", '男', 27, "2309", "008"),"睡觉");   
		
		Set<Entry<Student,String>> entrySet = map.entrySet();
		for (Entry<Student, String> entry : entrySet) {
			System.out.println(entry);
		}
	}
}

5,Collections - 集合工具类

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test01 {
	/**
	 * 知识点:Collections - 集合工具类
	 */
	public static void main(String[] args) {
		
		ArrayList<Integer> list = new ArrayList<>();
		
		//批量添加
		Collections.addAll(list, 5,3,4,1,2);
		
		//排序 -- 内置比较器
		Collections.sort(list);
		
		//查找(二分法)
		int index = Collections.binarySearch(list, 2);
		System.out.println("查找到元素的下标为:" + index);
		
		//排序 -- 外置比较器
		Collections.sort(list, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return -Integer.compare(o1, o2);
			}
		});
		
		//替换
		Collections.fill(list, 666);
		
		//将list生成线程安全的List
		List<Integer> synchronizedList = Collections.synchronizedList(list);
		
		System.out.println(Arrays.toString(synchronizedList.toArray()));
		
	}
}

6,ConcurrentHashMap

6.1ConcurrentHashMap的使用
import java.util.Arrays;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class Test01 {
	/**
	 * 知识点:ConcurrentHashMap的使用
	 */
	public static void main(String[] args) {
		
		ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
		
		//添加元素
		Integer put1 = map.put("麻生希", 24);
		Integer put2 = map.put("椎名空", 21);
		Integer put3 = map.put("水菜丽", 26);
		Integer put4 = map.put("朝桐光", 29);
		Integer put5 = map.put("樱井步", 26);
		
		//替换元素
		Integer put6 = map.put("麻生希", 25);
		
		//put()的返回值:如果第一次添加key就返回null,如果就重复key就替换value并返回被替换的值
		System.out.println("put1:" + put1);
		System.out.println("put2:" + put2);
		System.out.println("put3:" + put3);
		System.out.println("put4:" + put4);
		System.out.println("put5:" + put5);
		System.out.println("put6:" + put6);
		
		//替换元素
		map.replace("麻生希", 26);
		
		//替换元素
		map.replace("麻生希", 26, 27);
		
		//将newMap中所有的元素添加到map中
		ConcurrentHashMap<String, Integer> newMap = new ConcurrentHashMap<>();
		newMap.put("aaa", 10);
		newMap.put("bbb", 10);
		newMap.put("ccc", 10);
		map.putAll(newMap);
		
		//如果有key就查询出对应的value,如果没key就添加
		Integer putIfAbsent = map.putIfAbsent("麻生希1", 26);
		System.out.println("putIfAbsent:" + putIfAbsent);
		
		//通过key获取对应的value
		Integer integer = map.get("椎名空");
		System.out.println("通过key获取对应的value:" + integer);//21
		
		//通过key获取对应的value,如果没有key就返回默认值
		Integer orDefault = map.getOrDefault("椎名空111", 666);
		System.out.println("通过key获取对应的value:" + orDefault);//666
		
		//依据key删除元素
		map.remove("aaa");
		//依据key+value删除元素
		map.remove("bbb", 10);
		
		//清空集合中所有的元素
		//map.clear();
		
		System.out.println("判断集合中是否有包含的Key:" + map.containsKey("椎名空"));//true
		System.out.println("判断集合中是否有包含的Value:" + map.containsValue(26));//true
		System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
		System.out.println("获取元素个数:" + map.size());
		
		//获取map中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));
		
		System.out.println("-----------------------");
		
		//遍历方式一:
		//keySet():获取map集合中所有的key,返回Set集合
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + " -- " + value);
		}
		
		System.out.println("-----------------------");
		
		//遍历方式二:
		//entrySet():获取map集合中所有的映射关系对象,返回Set集合
		Set<Entry<String, Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + " -- " + value);
		}
	}
}
6.2,ConcurrentHashMap的特点

import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class Test02 {
	/**
	 * 知识点:ConcurrentHashMap的特点
	 * 
	 * 特点:无序+key唯一+线程安全
	 */
	public static void main(String[] args) {
		
		ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
		
		//添加元素
		map.put("麻生希", 24);
		map.put("椎名空", 21);
		map.put("水菜丽", 26);
		map.put("朝桐光", 29);
		map.put("樱井步", 26);
		map.put("麻生希", 25);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry);
		}
		
		/**
		 * HashMap vs LinkedHashMap vs Hashtable vs ConcurrentHashMap
		 * 
		 * 共同点:方法的使用都是一样的
		 * 
		 * HashMap:无序+key唯一
		 * LinkedHashMap:有序+key唯一
		 * Hashtable:无序+key唯一+线程安全(方法上加锁,效率低 - 弃用)
		 * ConcurrentHashMap:无序+key唯一+线程安全(局部加锁,效率高 )
		 */	
	}
}

7,Properties - 配置文件类

import java.io.IOException;
import java.util.Properties;

public class Test01 {
	/**
	 * 知识点:Properties - 配置文件类
	 */
	public static void main(String[] args) throws IOException {
		
		//创建配置文件类的对象
		Properties properties = new Properties();
		
		//将配置文件加载到对象中
		properties.load(Test01.class.getClassLoader().getResourceAsStream("DBConfig.properties"));
		
		//获取配置文件中的数据
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		
		System.out.println(username + " -- " + password);
	}
}

DBConfig.properties在src下创建得文件

userName=root
passWord=123123

8,ArrayList排序

import java.util.ArrayList;
import java.util.Comparator;

public class Test01 {
	/**
	 * 知识点:ArrayList排序
	 */
	public static void main(String[] args) {
		
		ArrayList<Student> list = new ArrayList<>();
		
		list.add(new Student("麻生希", '女', 26, "2308", "001"));        
		list.add(new Student("波多野结衣", '女', 29, "2309", "001"));      
		list.add(new Student("椎名空", '女', 23, "2308", "002"));    
		list.add(new Student("徐灿", '男', 25, "2309", "006"));         
		list.add(new Student("水菜丽", '女', 29, "2308", "003")); 
		list.add(new Student("小西满里惠", '女', 31, "2309", "002"));      
		list.add(new Student("铃原爱蜜莉", '女', 23, "2309", "003"));  
		list.add(new Student("朝桐光", '女', 32, "2308", "004"));  
		list.add(new Student("彭鹏", '男', 26, "2309", "007"));    
		list.add(new Student("北岛玲", '女', 42, "2308", "005"));        
		list.add(new Student("三上悠亚", '女', 31, "2308", "006"));       
		list.add(new Student("古川伊织", '女', 24, "2308", "007")); 
		
		//自定义排序规则
		list.sort(new Comparator<Student>() {
			//排序规则:按照名字长度排序,长度一致按照年龄排序
			@Override
			public int compare(Student o1, Student o2) {
				if(o1.equals(o2)){
					return 0;
				}
				
				int length1 = o1.getName().length();
				int length2 = o2.getName().length();
				if(length1 != length2){
					return length1 - length2;
				}
				
				int age1 = o1.getAge();
				int age2 = o2.getAge();
				if(age1 != age2){
					return age1 - age2;
				}
				return 1;
			}
		});
		
		for (Student stu : list) {
			System.out.println(stu);
		}
	}
}

public class Student implements Comparable<Student>{

	private String name;
	private char sex;
	private int age;
	private String classId;
	private String id;
	
	public Student() {
	}
	
	public Student(String classId, String id) {
		this.classId = classId;
		this.id = id;
	}

	public Student(String name, char sex, int age, String classId, String id) {
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.classId = classId;
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj){
			return true;
		}
		
		if(obj instanceof Student){
			Student stu = (Student) obj;
			if(this.classId.equals(stu.classId) && this.id.equals(stu.id)){
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return name + "\t" + sex + "\t" + age + "\t" + classId + "\t" + id;
	}

	//排序规则:按照学生年龄排序
	@Override
	public int compareTo(Student o) {
		return this.age - o.age;
	}
}
文章来源:https://blog.csdn.net/haikeydnk/article/details/135113100
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。