-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetExample.java
55 lines (40 loc) · 1.49 KB
/
SetExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
//Set - it is an interface from collection.It will accept only unique elements - Duplicates not allowed.
//Unordered elements in set.
//Max one is allowed as null.
//HashSet-- It is extends AbstractSet- Unique elements. Unodered- It does not maintain any order, Hashtable is used to store the data(Hashing machanism).
//In Hashing Mechanism - There is hashcode for every element.
//Linkedhashset-- It extends the Hashset- Unique elements- It maintains insertion order.
//TreeSet-- Sorted set- elements are sorted in ascending order.
// It is also having only unique elements.
public class SetExample {
public static void main(String[] args) {
//TreeSet
TreeSet<String> treeSet=new TreeSet<String>();
treeSet.add("A");
treeSet.add("B");
treeSet.add("G");
treeSet.add("E");
treeSet.add("D");
treeSet.add("F");
System.out.println(treeSet);
/* //LinkedHashSet Example
* LinkedHashSet<String> linkedHashSet=new LinkedHashSet<String>();
*
* linkedHashSet.add("A"); linkedHashSet.add("B"); linkedHashSet.add("C");
* linkedHashSet.add("E"); linkedHashSet.add("D"); linkedHashSet.add("F");
*
* System.out.println(linkedHashSet);
*/
/* //Hashset Example
* HashSet<String> hashSet=new HashSet<String>();
*
* hashSet.add("A"); hashSet.add("B"); hashSet.add("C"); hashSet.add("E");
* hashSet.add("D"); hashSet.add("F");
*
* System.out.println(hashSet);
*/
}
}