Set--HashSet, LinkedHashSet, TreeSet

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]



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值