Collection集合的简单介绍及使用(下)
二、Set接口与实现类
特点:无序、无下标、元素不可重复。 方法:全部继承自Collection中的方法。
(1)HashSet
# 基于哈希码(HashCode)实现元素不可重复;当存入元素的哈希码相同时,会调用==或equals进行确认,结果为true,则拒绝后者存入。
# 底层是哈希表+链表+红黑树,如果链表中的元素超过8位则会转化成红黑树。
# 如果存储的是自定义类型对象,想要存储内容相等的对象,需要自定义类重写hashCode()和equals()。
代码示例:
public class TestHashSet {
public static void main ( String[ ] args) {
HashSet< String> hashSet= new HashSet < String> ( ) ;
hashSet. add ( "刘德华" ) ;
hashSet. add ( "梁朝伟" ) ;
hashSet. add ( "林志玲" ) ;
hashSet. add ( "周润发" ) ;
hashSet. add ( "刘德华" ) ;
System. out. println ( "元素个数:" + hashSet. size ( ) ) ;
System. out. println ( hashSet. toString ( ) ) ;
hashSet. remove ( "刘德华" ) ;
System. out. println ( "删除之后:" + hashSet. size ( ) ) ;
System. out. println ( "--------增强for--------" ) ;
for ( String string : hashSet) {
System. out. println ( string) ;
}
System. out. println ( hashSet. contains ( "郭富城" ) ) ;
System. out. println ( hashSet. isEmpty ( ) ) ;
}
}
注意:
- - - HashSet是根据hashcode计算保存的位置,如果此位置为空,则直接保存,如果不为空执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表。
- - - JDK1.8之后引入红黑树,提高效率。
(2)LinkedHashSet
# 因为是HashSet的子类,所以元素也是根据hashCode计算保存的位置、也是不可重复的。
# 它同时使用链表维护元素的插入顺序,是set集合中唯一一个能保证怎么存就怎么取的集合。
# 因为需要维护元素的插入顺序,因此性能低于HashSet,但在迭代访问Set里的全部元素时将有很好的性能,因为它是以链表来维护内部顺序。
代码示例:
public class Demo01 {
public static void main ( String[ ] args) {
LinkedHashSet< String> link = new LinkedHashSet < > ( ) ;
link. add ( "刘德华" ) ;
link. add ( "刘德华" ) ;
link. add ( "梁朝伟" ) ;
link. add ( "周润发" ) ;
System. out. println ( link) ;
}
}
(3)TreeSet
# 底层是红黑树,基于排列顺序实现元素不重复,实现了SortedSet接口,对集合元素自动排序。
# 元素对象的类型必须实现Comparable或Comparator接口,指定排序规则:自然排序或比较器,通过重写compareTo或compare方法确定存储的位置。
代码示例:自然排序
public class Test {
public static void main ( String[ ] args) {
TreeSet< Student> set = new TreeSet < > ( ) ;
set. add ( new Student ( 3 , "张三" , 17 ) ) ;
set. add ( new Student ( 2 , "赵六" , 20 ) ) ;
set. add ( new Student ( 1 , "李四" , 19 ) ) ;
set. forEach ( item - > {
System. out. println ( item) ;
} ) ;
}
}
class Student implements Comparable < Student> {
private int id;
private String name;
private int age;
public Student ( int id, String name, int age) {
this . id = id;
this . name = name;
this . age = age;
}
public int getId ( ) {
return id;
}
@Override
public String toString ( ) {
return id + "\t\t" + name + "\t\t" + age;
}
@Override
public int compareTo ( Student o) {
return this . getId ( ) - o. getId ( ) ;
}
}
代码示例:比较器
public class Test {
public static void main ( String[ ] args) {
TreeSet< User> treeSet = new TreeSet < > ( new Comparator < User> ( ) {
@Override
public int compare ( User o1, User o2) {
return o1. getId ( ) - o2. getId ( ) ;
}
} ) ;
treeSet. add ( new User ( 101 , "小陈" , 20 ) ) ;
treeSet. add ( new User ( 104 , "小李" , 23 ) ) ;
treeSet. add ( new User ( 103 , "小吴" , 22 ) ) ;
treeSet. add ( new User ( 102 , "小明" , 21 ) ) ;
treeSet. forEach ( item - > {
System. out. println ( item) ;
} ) ;
}
}
class User {
int id;
String name;
int age;
public User ( int id, String name, int age) {
this . id = id;
this . name = name;
this . age = age;
}
public int getId ( ) {
return id;
}
@Override
public String toString ( ) {
return id + "\t\t" + name + "\t\t" + age;
}
}
(4)Queue
# 线性队列,特点是先进先出,继承了Collection接口。
代码示例:
public class Test {
public static void main ( String[ ] args) {
Queue< String> queue = new LinkedList < > ( ) ;
queue. offer ( "小陈" ) ;
queue. offer ( "小吴" ) ;
queue. offer ( "小李" ) ;
for ( String str: queue) {
System. out. println ( str) ;
}
System. out. println ( "--------------" ) ;
System. out. println ( queue. poll ( ) ) ;
System. out. println ( "--------------" ) ;
for ( String str: queue) {
System. out. println ( str) ;
}
}
}