集合之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是第一次添加,则返回nullInteger 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);//替换元素,返回是否替换成功的booleanboolean 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对应的valueInteger 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就添加,如果有就返回对应的valueInteger 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对应的valueSet<String> keySet = map.keySet();for (String key : keySet) {Integer value = map.get(key);//传入key获取到对应的valueSystem.out.println(key + " -- " + value);}System.out.println("---------------");//遍历方式2 -- entrySet()//entry:表示映射关系(key + value)//思路:获取map中所有的映射关系对象,将映射关系对象存放在set集合中,遍历set集合将映射关系对象取出,就可以通过映射关系对象获取到key和valueSet<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;}//判断两个学生是否相同(班级号 + 学号)@Overridepublic 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;}@Overridepublic String toString() {return name + "\t" + sex + "\t" + age + "\t" + classId + "\t" + id;}//排序规则:按照年龄排序@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic 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>>() {@Overridepublic 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>() {@Overridepublic 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:配置文件

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

  1. Map接口以及Collections工具类

    文章目录 1.Map接口概述 1.1 Map的实现类的结构 1.2 Map中存储的key-value结构的理解 1.3 HashMap的底层实现原理(以JDK7为例) 1.4 Map接口的常用方法 1 ...

  2. JavaSE学习总结(十四)Map集合/Map和Collection的区别/HashMap/LinkedHashMap/TreeMap/集合间的嵌套/Hashtable/Collections工具类

    一.Map集合 我们知道,一个学号就能对应一个学生,并且每个学生的学号都不同,学号就像一个键,对应的学生就是该键对应的值.日常生活中经常能见到这种类似学号对应学生的例子.Java 为了我们更加方便地去 ...

  3. 集合框架学习笔记:Collection体系和Map体系、Collections工具类

    集合框架 Java是面向对象编程,万事万物皆"对象",为了方便对"对象"进行操作,需要对"对象"进行存储,而Java集合就是存储" ...

  4. 集合框架(Map容器/Collections工具类)

    >两大主流:collection.map(接口) 底层实现为数组和链表: RationalRose工具:接口与典型实现类: Map为key和value对的形式; >HashMap(线程不安 ...

  5. Java中集合相关案例(泛型通配符、Collections工具类、TreeSet、TreeMap、HashMap、HashSet和集合嵌套案例)

    集合 一.集合相关案例 1.泛型通配符案例 2.集合工具类(Collections工具类) 3.TreeSet和TreeMap案例 4.HashMap案例 5.HashSet案例 6.TreeSet案 ...

  6. Day18JavaSE——Map集合Collections工具类集合案例练习

    Day18JavaSE--Map集合&Collections工具类&集合案例练习 文章目录 Day18JavaSE--Map集合&Collections工具类&集合案例 ...

  7. Map、HashMap、TreeMap、Collections工具类

    一.Map 1.概述: 将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值. Map<K,E> map = new HashMap<>();//实例化,使 ...

  8. Java知识点04——集合(Set、List、Queue、Map、Collection和Iterator、Collections工具类)

    Java知识点04--集合(Set.List.Queue.Map.Collection.Iterator.Collections工具类) 一.集合 1.1 集合概述 二.Collection 2.1 ...

  9. day20Map集合(HashMapTreeMap)Collections工具类集合嵌套

    Map接口的概述         将键映射到值的对象.         Map不能包含重复的键;         每个键可以映射到最多一个值. Map接口与Collection接口有什么区别?     ...

最新文章

  1. 交换机配置软件crt安装_非常详细的锐捷二层交换机配置教程,适合新手小白
  2. 2019上海车展展后报告(整车篇)
  3. 平时一些mysql小技巧及常识
  4. 计算机网络和通讯原理图,第章 计算机网络通信原理.ppt
  5. java 泛型 t_Kotlin(2) 泛型与集合
  6. Linux修改UTF8字符编码
  7. php监听mq消息,客户端监听服务端获取rabbitmq消息队列,rabbitmq有消息的时候客户端刷新页面才能获取到消息,监听没起到作用,请求各位大神指点迷津...
  8. CF896E Welcome home,Chtholly/[Ynoi2018]五彩斑斓的世界(并查集+第二分块)
  9. SAS® Model Manager功能调研
  10. python image清除_60秒掌握Python内置模块Turtle的用法——绘制漫天雪花
  11. 修改本地文件存储路径
  12. 用命令行netsh修改windows的ip、网关、dns
  13. krc 编辑 linux,krc文件怎么打开?krc是什么文件?
  14. android 6.0 讯飞TTS
  15. 666: 神奇的 SQL 之别样的写法 → 行行比较
  16. 阅读3Hierarchical integrated machine learning model for predicting flight departure delays and...
  17. proxmox VE开NAT小鸡 无法联网,怎么开NAT模式
  18. Java规则引擎easy rules
  19. 【AI学习总结】均方误差(Mean Square Error,MSE)与交叉熵(Cross Entropy,CE)损失函数
  20. 每次压力大到爆,驾校教练总爱跑敬老院干这件事

热门文章

  1. golang中的字符串
  2. 各种浏览器下载tampermonkey网址
  3. 用人工智能做广告,它成为第一家走上IPO的人工智能企业
  4. 用python画卡通图_需要用Python和OpenCV制作一张卡通漫画版的图片
  5. php的解析别名,浅谈laravel aliases别名的原理
  6. Git 配置别名 —— 让命令变得更简单
  7. 小米路由开启SSH访问权限
  8. Python装逼神器,Python实现一键批量扣图
  9. Springsecurity+cas整合后无法单点登出
  10. 四大热门BI产品的深度对比