s-day04-TreeMap保存自定义对象&集合的框架体系
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
package com.inmind.treemap03;
|
||||||
|
|
||||||
|
import com.inmind.treeset02.Student;
|
||||||
|
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
/*
|
||||||
|
TreeMap的基本使用
|
||||||
|
带有自然排序功能的双列集合(以Key值的自然排序功能进行排序)
|
||||||
|
|
||||||
|
总结:treeSet和treeMap都是具有排序功能的集合,所以要存入有排序功能的自定义对象才能使用排序功能,如果
|
||||||
|
没有排序功能,会报错:java.lang.ClassCastException: com.inmind.treeset02.Student cannot be cast to java.lang.Comparable
|
||||||
|
|
||||||
|
Collection:单列集合父接口
|
||||||
|
List:
|
||||||
|
ArrayList:底层是object[],查询快,增删慢
|
||||||
|
LinkedList:底层是双向链接,查询慢,增删快
|
||||||
|
Set:
|
||||||
|
HashSet:底层哈希表,在实现去重的基础上,能够高效查询,HashCode,equals重写
|
||||||
|
LinkedHashSet:底层哈希表+链表,实现Set集合的有序
|
||||||
|
TreeSet:拥有自然排序功能且会直接排序,元素必要有排序(Comparable,Comparator)
|
||||||
|
Map:
|
||||||
|
HashMap:存取无序的,不可重复的双列集合
|
||||||
|
LinkedhashMap:存取有序,不可重复的双列集合
|
||||||
|
TreeMap:拥有自然排序功能且会直接排序,元素必要有排序(Comparable,Comparator)
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class TreeMapDemo10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个学生类为key,住址为String的有排序功能的双列集合
|
||||||
|
TreeMap<Student,String> treeMap = new TreeMap<>();
|
||||||
|
treeMap.put(new Student("小王1", 18), "南京");
|
||||||
|
treeMap.put(new Student("小王2", 17), "南京");
|
||||||
|
treeMap.put(new Student("小王3", 20), "南京");
|
||||||
|
|
||||||
|
System.out.println(treeMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user