集合之Map家族的TreeMap + Sort +Properties及Collections工具类和总结

集合之Map家族的TreeMap + Sort +Properties及Collections工具类和总结

一、TreeMap

1.TreeMap的使用

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

public class Test01 {

	public static void main(String[] args) {
		
		TreeMap<String, Integer> map = new TreeMap<>();
		
		//添加元素 -- 如果key是第一次添加,则返回null
		Integer put1 = map.put("张飞", 19);
		Integer put2 = map.put("关羽", 20);
		Integer put3 = map.put("刘备", 23);
		Integer put4 = map.put("诸葛亮", 19);
		Integer put5 = map.put("赵云", 22);
		Integer put6 = map.put("马超", 21);
		
		System.out.println("put()的返回值:" + put1);
		System.out.println("put()的返回值:" + put2);
		System.out.println("put()的返回值:" + put3);
		System.out.println("put()的返回值:" + put4);
		System.out.println("put()的返回值:" + put5);
		System.out.println("put()的返回值:" + put6);
		
		//替换元素,返回被替换掉的值
		Integer put = map.put("关羽", 22);
		System.out.println("put()的返回值:" + put);
		
		//替换元素,返回被替换掉的值
		Integer replace = map.replace("关羽",25);
		System.out.println("repace()的返回值:" + replace);
		
		//替换元素,返回是否替换成功的boolean
		boolean bool = map.replace("马超",21,23);
		System.out.println("替换元素,返回是否替换成功的boolean:" + bool);
		
		//清空所有元素
		//map.clear();
		
		System.out.println("判断map中是否有指定的key:" + map.containsKey("刘备"));
		System.out.println("判断map中是否有指定的value:" + map.containsValue(23));
		
		//获取指定key对应的value
		Integer integer = map.get("诸葛亮");
		System.out.println("获取指定key对应的value:" + integer);
		
		//获取指定key对应的value,如果没key,就返回默认值
		Integer orDefault = map.getOrDefault("关羽", 666);
		System.out.println("获取指定key对应的value:" + orDefault);
		
		System.out.println("判断map集合中是否不包含元素:" + map.isEmpty());
		
		//将newMap中所有的元素都添加到map中
		TreeMap<String, Integer> newMap = new TreeMap<>();
		
		newMap.put("aa",10);
		newMap.put("bb",30);
		newMap.put("cc",20);
		map.putAll(newMap);
		
		//如果map中没有该key就添加,如果有就返回对应的value
		Integer putIfAbsent = map.putIfAbsent("ddd", 40);
		System.out.println("如果map中没有该key就添加,如果有就返回对应的value:" + putIfAbsent);
		
		//根据key删除映射关系
		map.remove("赵云");
		//根据key+value删除映射关系
		map.remove("cc", 20);
		
		System.out.println("获取map中映射关系的个数:" + map.size());
		
		//获取map中所有的value,返回集合
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));
		
		System.out.println("---------------");
		
		//遍历方式1 -- keySet()
		//思路:获取map中所有的key,将key存放在set集合中,遍历set集合将key取出,就可以通过map获取key对应的value
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);//传入key获取到对应的value
			System.out.println(key + " -- " + value);
		}
		
		System.out.println("---------------");
		
		//遍历方式2 -- entrySet()
		//entry:表示映射关系(key + value)
		//思路:获取map中所有的映射关系对象,将映射关系对象存放在set集合中,遍历set集合将映射关系对象取出,就可以通过映射关系对象获取到key和value
		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.验证TreeMap的特点

特点:针对于Key的自然排序
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class Test02 {

	public static void main(String[] args) {
		
		TreeMap<String, Integer>  map = new TreeMap<>();
		
		map.put("c",10);
		map.put("d",40);
		map.put("a",20);
		map.put("b",30);
		map.put("d",50);
		
		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.内置比较器

import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class Test03 {

	public static void main(String[] args) {
		
		TreeMap<Student,String> map = new TreeMap<>();
		
		map.put(new Student("孙尚香", '女', 22, "2211", "001"),"赏月");     
		map.put(new Student("安吉拉", '女', 25, "2211", "002"),"闻香");     
		map.put(new Student("虞姬", '女', 19, "2211", "003"),"听雨");     
		map.put(new Student("妲己", '女', 29, "2211", "004"),"拾花");     
		map.put(new Student("杨贵妃", '女', 21, "2211", "005"),"闻香");    
		map.put(new Student("钟无艳", '女', 20, "2211", "006"),"听雨");    
		map.put(new Student("貂蝉", '女', 19, "2211", "007"),"拾花");    
		map.put(new Student("大桥", '女', 18, "2211", "008"),"赏月");    
		map.put(new Student("伽罗", '女', 21, "2211", "009"),"赏花");    
		map.put(new Student("黄忠", '男', 25, "2211", "010"),"酌酒");     
		map.put(new Student("小乔", '女', 22, "2212", "001"),"赏花");    
		map.put(new Student("阿珂", '女', 24, "2212", "002"),"闻香");    
		map.put(new Student("瑶", '女', 20, "2212", "003"),"上身");   
		map.put(new Student("上官婉儿", '女', 22, "2212", "004"),"酌酒");  
		map.put(new Student("墨瞳茉拉", '女', 22, "2213", "004"),"抚琴"); 
		
		Set<Entry<Student, String>> entrySet = map.entrySet();
		for (Entry<Student, String> entry : entrySet) {
			System.out.println(entry);
		}
	}
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(classId.equals(stu.classId) && 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) {
		//年龄差
		int num = this.age - o.age;
		if(num != 0){
			return num;
		}
		
		if(this == o || this.equals(o)){
			return 0;
		}
		
		return 1;//年龄相同,但是两个学生又不是同一个对象,我们还是返回1
	}
}

4.外置比较器

import java.util.Comparator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class Test04 {

	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 == o2 || o1.equals(o2)) {
				return 0;
			}
			int len1 = o1.getName().length();
			int len2 = o2.getName().length();
			if (len1 != len2) {
				return len1-len2;
			}
			int age1 = o1.getAge();
			int age2 = o2.getAge();
			if (age1 != age2) {
				return age1-age2;
			}
				return 1;
			}
		});
		map.put(new Student("孙尚香", '女', 22, "2211", "001"),"赏月");     
		map.put(new Student("安吉拉", '女', 25, "2211", "002"),"闻香");     
		map.put(new Student("虞姬", '女', 19, "2211", "003"),"听雨");     
		map.put(new Student("妲己", '女', 29, "2211", "004"),"拾花");     
		map.put(new Student("杨贵妃", '女', 21, "2211", "005"),"闻香");    
		map.put(new Student("钟无艳", '女', 20, "2211", "006"),"听雨");    
		map.put(new Student("貂蝉", '女', 19, "2211", "007"),"拾花");    
		map.put(new Student("大桥", '女', 18, "2211", "008"),"赏月");    
		map.put(new Student("伽罗", '女', 21, "2211", "009"),"赏花");    
		map.put(new Student("黄忠", '男', 25, "2211", "010"),"酌酒");     
		map.put(new Student("小乔", '女', 22, "2212", "001"),"赏花");    
		map.put(new Student("阿珂", '女', 24, "2212", "002"),"闻香");    
		map.put(new Student("瑶", '女', 20, "2212", "003"),"上身");   
		map.put(new Student("上官婉儿", '女', 22, "2212", "004"),"酌酒");  
		map.put(new Student("墨瞳茉拉", '女', 22, "2213", "004"),"抚琴");
		
		Set<Entry<Student,String>> entrySet = map.entrySet();
		for (Entry<Student, String> entry : entrySet) {
			System.out.println(entry);
		}
	}
}

二、Sort

1.ArrayList排序

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

public class Test01 {

	public static void main(String[] args) {
		
		ArrayList<String> list = new ArrayList<>();
		
		list.add("d");
		list.add("a");
		list.add("bc");
		list.add("b");
		list.add("B");
		list.add("bac");
		list.add("e");
		list.add("A");
		list.add("ecz");
		list.add("ea");
		list.add("C");
		list.add("c");
		
		list.sort(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});
		
		for (String element : list) {
			System.out.println(element);
		}
	}
}

2.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 Test02 {

	public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();
		
		 map.put("大桥",18);
		 map.put("小乔",19);
		 map.put("西施",25);
		 map.put("王昭君",20);
		 map.put("杨贵妃",23);
		 map.put("貂蝉",28);
		 
			//将map所有的映射关系取出 -- entrySet()
		 Set<Entry<String, Integer>> entrySet = map.entrySet();
		 
		//将Set集合转换为ArrayList集合
		 ArrayList<Entry<String, Integer>> list = new ArrayList<>(entrySet);
		 
		 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();
				if (value1 == value2) {
					return 1;
				}
				return value1 - value2;
			}
		});
		 for (Entry<String, Integer> entry : list) {
			System.out.println(entry);
		}
	}
}

三、Properties – 配置文件

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

public class Test01 {
	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);
		
	}
}
username=gl
password=123456

四、Collections – 集合工具类

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

public class Test01 {

	public static void main(String[] args) {
		
		ArrayList<String> list = new ArrayList<>();
		
		//批量添加
		Collections.addAll(list, "c","a","d","b");
		
		//使用集合中元素的内置比较器进行排序
		Collections.sort(list);
		
		//使用外置置比较器进行排序
		Collections.sort(list, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return -o1.compareTo(o2);
			}
		});
		
		//替换
		Collections.fill(list, "abc");
		
		System.out.println(Arrays.toString(list.toArray()));
	}
}

五、总结

1.Collection 与 Map的区别

1.Collection 存单个值,可以获取迭代器进行遍历
2.Map存两个值(Key-Value),不可以获取迭代器,不能遍历(Map可以间接遍历)

2.ArrayList 与 LinkedList的区别

1.使用上的区别:
LinkedList添加了
队列模式-先进先出(removeFirst())
栈模式-先进后出(removeLast())
2.效率上的区别:
1.ArrayList底层数据结构是一维数组
2.LinkedList底层数据结构是双向链表
添加 - 不扩容的情况:ArrayList快
添加 - 扩容的情况:LinkedList快
删除:LinkedList快
查询:ArrayList快
修改:ArrayList快
3.注意:工作中常用ArrayList,因为很多需求都需要使用查询功能,ArrayList查询更快

3.各种集合的应用场景

1.ArrayList:存数据,线程不安全
2.LinkedLinked:队列模式、栈模式,线程不安全
3.Vector:弃用,线程安全
4.Stack:弃用,线程安全
5.HashSet:去重+无序,线程不安全
6.LinkedHashSet:去重+有序,线程不安全
7.TreeSet:排序,线程不安全
8.HashMap:存key+value,key去重,无序,线程不安全
9.LinkedHashMap:存key+value,key去重,有序,线程不安全
10.Hashtable:弃用,存key+value,key去重,无序,线程安全,方法加锁-效率低
11.ConcurrentHashMap:存key+value,key去重,无序,线程安全,局部加锁、CAS-效率高
12.TreeMap:存key+value,针对于Key排序
13.Properties:配置文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨霖先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值