集合之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<>();
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 bool = map.replace("马超",21,23);
System.out.println("替换元素,返回是否替换成功的boolean:" + bool);
System.out.println("判断map中是否有指定的key:" + map.containsKey("刘备"));
System.out.println("判断map中是否有指定的value:" + map.containsValue(23));
Integer integer = map.get("诸葛亮");
System.out.println("获取指定key对应的value:" + integer);
Integer orDefault = map.getOrDefault("关羽", 666);
System.out.println("获取指定key对应的value:" + orDefault);
System.out.println("判断map集合中是否不包含元素:" + map.isEmpty());
TreeMap<String, Integer> newMap = new TreeMap<>();
newMap.put("aa",10);
newMap.put("bb",30);
newMap.put("cc",20);
map.putAll(newMap);
Integer putIfAbsent = map.putIfAbsent("ddd", 40);
System.out.println("如果map中没有该key就添加,如果有就返回对应的value:" + putIfAbsent);
map.remove("赵云");
map.remove("cc", 20);
System.out.println("获取map中映射关系的个数:" + map.size());
Collection<Integer> values = map.values();
System.out.println(Arrays.toString(values.toArray()));
System.out.println("---------------");
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println(key + " -- " + value);
}
System.out.println("---------------");
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;
}
}
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);
Set<Entry<String, Integer>> entrySet = map.entrySet();
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:配置文件