Set--HashSet, LinkedHashSet, TreeSet
2015-04-02 22:28
295 查看
A set is an efficient data structure for storing and processing nonduplicate elements. A map is like a dictionary that provides a quick lookup to retrieve a value using a key.
HashSet
no order,
no duplicate
the strings are not stored in the order in which they are inserted into the set. no duplicate elements in hashset
e.g
public class TestHashSet {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
//Add String to the set
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("New York");
System.out.println(set);
//display the elements in the hash set
for (String string : set) {
System.out.print(string.toUpperCase() + " ");
}
}
}
Output:
[San Francisco, New York, London, Paris]
SAN FRANCISCO NEW YORK LONDON PARIS
LinkedHashSet
order in the elements inserted sequence
no duplicate
LinkedHashSet extends HashSet with a linked-list implementation that supports an ordering of the elements in the set. The elements in a HashSet are not ordered, but the elements in a LinkedHashSet can be retrieved in the order in which they were inserted
into the set.
e.g
import java.util.LinkedHashSet;
import java.util.Set;
public class TestLinkedHashSet {
public static void main(String[] args) {
//create a link hashset
Set<String> set = new LinkedHashSet<String>();
//add strings to the set
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
System.out.println(set);
//display the elements in the link hash set
for (String element : set) {
System.out.print(element.toUpperCase() + " ");
}
}
}
Output:
[London, Paris, New York, San Francisco, Beijing]
LONDON PARIS NEW YORK SAN FRANCISCO BEIJING
TreeSet
no duplicate
sorted
SortedSet is a subinterface of Set, which guarantees that the elements in the set are sorted. TreeSet implements the sorted interface.
e.g
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
//Create a hash set
Set<String> set = new HashSet<String>();
//add strings to the
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
set.add("New York");
//before the treeset
System.out.println("The hashSet sequence is : " + set );
TreeSet<String> treeSet = new TreeSet<String>(set);
System.out.println("Sorted tree set sequence is : " + treeSet);
//use the methods in sortedset interface
System.out.println("first(): " + treeSet.first());
System.out.println("last(): " + treeSet.last());
System.out.println("headset(\"New York\"): " + treeSet.headSet("New York"));
System.out.println("tailset(\"New York\"): " + treeSet.tailSet("New York"));
}
}
Output:
The hashSet sequence is : [San Francisco, Beijing, New York, London, Paris]
Sorted tree set sequence is : [Beijing, London, New York, Paris, San Francisco]
first(): Beijing
last(): San Francisco
headset("New York"): [Beijing, London]
tailset("New York"): [New York, Paris, San Francisco]
HashSet
no order,
no duplicate
the strings are not stored in the order in which they are inserted into the set. no duplicate elements in hashset
e.g
public class TestHashSet {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
//Add String to the set
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("New York");
System.out.println(set);
//display the elements in the hash set
for (String string : set) {
System.out.print(string.toUpperCase() + " ");
}
}
}
Output:
[San Francisco, New York, London, Paris]
SAN FRANCISCO NEW YORK LONDON PARIS
LinkedHashSet
order in the elements inserted sequence
no duplicate
LinkedHashSet extends HashSet with a linked-list implementation that supports an ordering of the elements in the set. The elements in a HashSet are not ordered, but the elements in a LinkedHashSet can be retrieved in the order in which they were inserted
into the set.
e.g
import java.util.LinkedHashSet;
import java.util.Set;
public class TestLinkedHashSet {
public static void main(String[] args) {
//create a link hashset
Set<String> set = new LinkedHashSet<String>();
//add strings to the set
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
System.out.println(set);
//display the elements in the link hash set
for (String element : set) {
System.out.print(element.toUpperCase() + " ");
}
}
}
Output:
[London, Paris, New York, San Francisco, Beijing]
LONDON PARIS NEW YORK SAN FRANCISCO BEIJING
TreeSet
no duplicate
sorted
SortedSet is a subinterface of Set, which guarantees that the elements in the set are sorted. TreeSet implements the sorted interface.
e.g
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
//Create a hash set
Set<String> set = new HashSet<String>();
//add strings to the
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
set.add("New York");
//before the treeset
System.out.println("The hashSet sequence is : " + set );
TreeSet<String> treeSet = new TreeSet<String>(set);
System.out.println("Sorted tree set sequence is : " + treeSet);
//use the methods in sortedset interface
System.out.println("first(): " + treeSet.first());
System.out.println("last(): " + treeSet.last());
System.out.println("headset(\"New York\"): " + treeSet.headSet("New York"));
System.out.println("tailset(\"New York\"): " + treeSet.tailSet("New York"));
}
}
Output:
The hashSet sequence is : [San Francisco, Beijing, New York, London, Paris]
Sorted tree set sequence is : [Beijing, London, New York, Paris, San Francisco]
first(): Beijing
last(): San Francisco
headset("New York"): [Beijing, London]
tailset("New York"): [New York, Paris, San Francisco]
相关文章推荐
- 【Java集合之Set】HashSet、TreeSet、LinkedHashSet区别
- Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet
- Set-TreeSet-HashSet-LinkedHashSet
- HashSet、TreeSet和LinkedHashSet
- 复习java基础第三天(集合:Collection、Set、HashSet、LinkedHashSet、TreeSet)
- 集合类:List(ArrayList、LinkedList、Vector)、Set(HashSet、TreeSet)介绍
- 黑马程序员:集合类:List(ArrayList、LinkedList、Vector)、Set(HashSet、TreeSet)介绍
- set集合分类总结(hashSet、treeSet、linkedHashSet)
- Java中的集合HashSet、LinkedHashSet、TreeSet和EnumSet(二)
- Java高级之集合与泛型>Collection、Set、HashSet、LinkedHashSet、TreeSet
- 集合类(常见的集合类:Collection、List、Set、ArrayList、linkedList、Vector、HashSet、TreeSet)
- Set集合以及HashSet、LinkedHashSet、TreeSet等讲解
- java集合类,List和Set比较,各自的子类比较(ArrayList,Vector,LinkedList;HashSet,TreeSet),Map集合比较
- Set接口下的 HashSet, LinkedHashSet, TreeSet
- Lesson_for_java_day13--java中的集合——Collection、List、ArrayList、LinkedList、Set、HashSet、TreeSet
- 17. 集合类 (Set的子类HashSet、LinkedHashSet、TreeSet)
- 【Java_集合框架Set】HashSet、LinkedHashSet、TreeSet使用区别
- HashSet-TreeSet-LinkedHashSet
- Java Collection —— Set(HashSet、TreeSet、LinkedHashSet)
- Set下面HashSet,TreeSet和LinkedHashSet的区别