您的位置:首页 > 其它

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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息