s-day03--LinkedHashSet的特点
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
package com.inmind.set02;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
LinkedHashSet的特点
|
||||||
|
1.存取有序
|
||||||
|
2.没有索引
|
||||||
|
3.不能重复
|
||||||
|
|
||||||
|
LinkedHashSet的底层数据结构:在哈希表的基础上,添加了链表,它是HashSet的子类
|
||||||
|
哈希表:去重,高效查询
|
||||||
|
链接:保证数据的顺序
|
||||||
|
|
||||||
|
注意:当我们定义的集合既要去重,也要保证顺序,采用LinkedHashSet
|
||||||
|
*/
|
||||||
|
public class LinkedHashSetDemo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student s1 = new Student("刘备1", 20);
|
||||||
|
Student s2 = new Student("刘备2", 19);
|
||||||
|
Student s3 = new Student("刘备3", 21);
|
||||||
|
Student s4 = new Student("刘备3", 21);
|
||||||
|
Student s5 = new Student("刘备3", 21);
|
||||||
|
LinkedHashSet<Student> sets = new LinkedHashSet<>();
|
||||||
|
sets.add(s1);
|
||||||
|
sets.add(s2);
|
||||||
|
sets.add(s3);
|
||||||
|
sets.add(s4);
|
||||||
|
sets.add(s5);
|
||||||
|
System.out.println(sets);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user