更多文章欢迎关注公众号“三横兰”
通过图片可以看出Java的集合主要有两种,一种是Collection接口,存放单一元素;另一种是Map接口,存放键值对元素。
Collection集合常用的接口有List、Set、Queue
- List:存放的元素是有序的、可以重复的。对顺序有要求的就使用改接口
- Set:存放的元素是无序的、注重唯一性、不可以重复。可以用于去重情况
- Queue:存放的元素是有序的、可重复的。
- ArrayList:存储数据用的是Object[]数组,查找功能方便,线程不安全
- Vector: 存储数据用的是Object[]数组,注重线程安全的用Vector
- linkedList:存储数据用的是双向链表数据结构,插入和删除数据比ArrayList更高效,非线程安全
-
HashSet:存储数据结构以及对数据的操作主要是通过HashMap来实现的,线程不安全。HashSet判断值唯一,先通过hashCode()函数获取hash值判断相等不相等,如果相等则再调用equals方法判断。
-
linkedHashSet:继承了HashSet。与HashSet的区别在于,HashSet的元素是无序的,而linkedHashSet使用链表维护元素的次序,访问元素的时候会按照插入的顺序访问集合的元素
public class Test { public static void main(String[] args) { Student student1 = new Student("student1"); Student student2 = new Student("student2"); Student student3 = new Student("student3"); HashSethashSet = new HashSet<>(); hashSet.add(student1); hashSet.add(student2); hashSet.add(student3); linkedHashSet linkedHashSet = new linkedHashSet<>(); linkedHashSet.add(student1); linkedHashSet.add(student2); linkedHashSet.add(student3); printSet(hashSet); printSet(linkedHashSet); } public static void printSet(Set set) { System.out.println(set.getClass()); for (Iterator iterator = set.iterator(); iterator.hasNext();) { Student student = (Student) iterator.next(); System.out.println(student.name); } } } 打印出来的结果:
class java.util.HashSet student3 student1 student2 class java.util.linkedHashSet student1 student2 student3
-
TreeSet:实现了SortSet的接口,可以看出集合的元素是出于排序状态的,放入TreeSet中的对象需实现Comparable接口并覆盖其compareTo()方法。有自然排序和定制排序,默认使用自然排序,如果元素不是自定义的对象,那么会按照升序排列。
public class Test { public static void main(String[] args) { Student student1 = new Student("student1"); Student student2 = new Student("student2"); Student student3 = new Student("student3"); // 自然排序 TreeSettreeSet = new TreeSet<>(); treeSet.add(student1); treeSet.add(student3); treeSet.add(student2); printSet(treeSet); // 定制排序 TreeSet treeSet2 = new TreeSet<>(new StudentComparator()); treeSet2.add(student1); treeSet2.add(student3); treeSet2.add(student2); printSet(treeSet2); } public static void printSet(Set set) { System.out.println(set.getClass()); for (Iterator iterator = set.iterator(); iterator.hasNext();) { Student student = (Student) iterator.next(); System.out.println(student.name); } } } class Student implements Comparable{ public Student(String name) { super(); this.name = name; } String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(Object o) { // 升序排列 return this.name.compareTo(((Student)o).getName()); } } class StudentComparator implements Comparator { @Override public int compare(Student o1, Student o2) { // 降序排列 return o2.getName().compareTo(o1.getName()); } } 打印出来的结果:
class java.util.TreeSet student1 student2 student3 class java.util.TreeSet student3 student2 student1
- HashMap:非线程安全;可以存储null的key和value,作为key只能有一个,作为value可以有无数个
- HashTable:线程安全;不可以有null的key和value。因为线程安全的问题,HashTable要比HashMap慢,并且如果考虑线程安全的话可以使用ConcurrentHashMap。HashTable继承了Dictionary,而后者已经被废弃,所以作为子类的HashTable也渐渐被打入冷宫,所以慎用
- linkedHashMap:与HashSet和linkedHashSet的关系类似。
- TreeMap:TreeMap比HashMap多实现了NavigableMap和SortedMap这两个接口。字面上的理解就知道比HashMap多了键排序和搜索的能力,主要是用于排序
Java集合的小总结,后续会详细学习各集合的具体用法。
蜗牛速度般的学习,慢牛般的成长-
更多文章欢迎关注公众号“三横兰”



