1212
added set examples · asdevi/JavaCollectionsExample@09dddba · GitHub
Skip to content

Commit 09dddba

Browse files
committed
added set examples
1 parent 07a3bb2 commit 09dddba

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

JavaCollections/bin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/listExamples/
2+
/setExample/

JavaCollections/src/listExamples/ArrayListExample.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class ArrayListExample {
1313
* ArraList and vector implements RandomAccess, Serializable and Cloneable Interfaces
1414
* Synchronized-> No
1515
* Thread safe-> NO
16+
* Default capacity-10
17+
* Fill Ratio or Load factor:1 or 100%
18+
* Growth Rate: current_size + current_size/2
1619
*/
1720
public void arrayListExample(){
1821

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package setExample;
2+
3+
import java.util.HashSet;
4+
import java.util.Iterator;
5+
6+
public class HashSetExample {
7+
8+
/**
9+
* Set(I)-> HashSet (C) and LinkedHashSet(C) are implementations
10+
* Set(I) -> SortedSet(Child Interface)->NavigableSet(I)=> TreeSet(C) is the implementation
11+
*Important points to remember:
12+
*1. To store group of individual objects.
13+
*2. Duplicates not allowed
14+
*3.Insertion order will not be maintained
15+
*4.Set(I) doesn't have any new methods other than given in Collection(I).
16+
*5. DS for HashSet is Hash table
17+
*6. If we add duplicate value to HashSet, simply it will return false to the
18+
*add() and it won't throw any error or exception.
19+
*7. We can insert null values
20+
*8. Heterogeneous values can be added.
21+
*9. Implements Serializable and Cloneable?-> Yes
22+
*10. Data are stored based on hashcode, so search is very effective.
23+
*11. Fill Ratio or Load factor:0.75 or 75%
24+
*12.Default capacity-16
25+
*/
26+
27+
/*
28+
* Number Constructors available in HashSet=4
29+
* 1. HashSet hs= new HashSet();// size-16 and fill ratio is 0.75
30+
* 2. HashSet hs= new HashSet(int initialSize);size as declared and fill ratio is 0.75(default)
31+
* 3. HashSet hs= new HashSet(int initialSize, float fillRatio);//size and fill ratio can be declared
32+
* 4. HashSet hs= new HashSet(Collection c);// creates a HashSet for any given Collection (Acts as interconversion)
33+
*/
34+
35+
public void basicExamplesHashSet(){
36+
HashSet<String>hashSet= new HashSet<String>();
37+
hashSet.add("A");
38+
hashSet.add("D");
39+
hashSet.add("E");
40+
hashSet.add("F");
41+
hashSet.add("A"); // the return type of add() is boolean. Since A is already there it will return false and wont add
42+
hashSet.add(null);
43+
//We have no control on insertion order
44+
System.out.println("Contents of the HashSet :"+ hashSet);
45+
}
46+
47+
/*
48+
* Iterate using Iterator
49+
*/
50+
51+
public void iterateUsingIterator(){
52+
HashSet<String>hashSet= new HashSet<String>();
53+
hashSet.add("A");
54+
hashSet.add("D");
55+
hashSet.add("E");
56+
hashSet.add("F");
57+
hashSet.add("A");
58+
59+
Iterator< String> iterator= hashSet.iterator();
60+
while(iterator.hasNext()){
61+
System.out.println("Elements of HashSet :"+ iterator.next());
62+
}
63+
64+
}
65+
66+
67+
public static void main(String[] args) {
68+
// TODO Auto-generated method stub
69+
HashSetExample hashSetExample= new HashSetExample();
70+
hashSetExample.basicExamplesHashSet();
71+
hashSetExample.iterateUsingIterator();
72+
}
73+
74+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package setExample;
2+
3+
import java.util.LinkedHashSet;
4+
5+
public class LinkedHashSetExample {
6+
7+
/**
8+
* LinkedHashSet->Child class of HashSet
9+
* DS-> Hash table + Linked List
10+
* Insertion order is preserved
11+
* Duplicates not allowed
12+
*/
13+
14+
public void linkedHashSetExample(){
15+
LinkedHashSet linkedHashSet = new LinkedHashSet();
16+
linkedHashSet.add(1);
17+
linkedHashSet.add("A");
18+
linkedHashSet.add("B");
19+
linkedHashSet.add("C");
20+
linkedHashSet.add("10");
21+
22+
System.out.println("Insertion Order preserved Linked Hash Set :"+ linkedHashSet);
23+
}
24+
25+
public static void main(String[] args) {
26+
// TODO Auto-generated method stub
27+
LinkedHashSetExample example= new LinkedHashSetExample();
28+
example.linkedHashSetExample();
29+
}
30+
31+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package setExample;
2+
3+
import java.util.TreeSet;
4+
import java.lang.*;
5+
public class TreeSetExample {
6+
7+
/**
8+
* TreeSet is the implementation class of SortedSet Interface.
9+
* Does not allow duplicates.
10+
* Sorts the elements
11+
* It has 6 methods like first,last,headSet,tailSet,subSet and comparator.
12+
* Difference between Hashset and Treeset, hash doesnt maintain order
13+
* but Treeset maintains ascending or alphabetical order.
14+
* DS-> Balanced Tree
15+
* Heterogeneous data not allowed. If added Exception, ClassCast will occur
16+
* For default natural sorting order,the objects should be homogeneous and comparable else class cast exception
17+
* To say any class is comporable or not, the class should implement comparable interface.
18+
*/
19+
/*
20+
* Constructors present are 4
21+
* 1. TreeSet ts= new TreeSet();// default sort order
22+
* 2. TreeSet ts= new TreeSet(Comparator c);// customized sorting order defined by comparator object
23+
* 3. TreeSet ts= new TreeSet(Collection c);// equivalent tree set of any collection will be created
24+
* 4. TreeSet ts= new TreeSet(SortedSet s);// creates tree set for given sorted set
25+
*/
26+
public void treeSetExample(){
27+
//Creating object
28+
TreeSet<Integer> treeSet= new TreeSet<Integer>() ;
29+
treeSet.add(10);
30+
treeSet.add(1);
31+
treeSet.add(2);
32+
treeSet.add(9);
33+
treeSet.add(7);
34+
treeSet.add(3);
35+
36+
System.out.println("Elements are sorted on ascending order :"+ treeSet);
37+
38+
//first()
39+
System.out.println("First element :"+treeSet.first());
40+
//last()
41+
System.out.println("Last element :"+treeSet.last());
42+
//headSet()
43+
System.out.println("Values lesser than given value"+treeSet.headSet(3));
44+
//tailSet()
45+
System.out.println("Values equal to and higher than given value"+treeSet.tailSet(9));
46+
//subSet()
47+
System.out.println("Subset values :"+treeSet.subSet(2, 9));
48+
//comparator()
49+
System.out.println("Comparator returns null if the sorting is default natural order :"+ treeSet.comparator());
50+
//Adding null to a non empty tree set causes null pointer excpetion
51+
//treeSet.add(null);
52+
/*null can be easily added if the tree set is empty. If there are any elements present, the
53+
* comparator will check for the sorting order between the previosly added element and
54+
* the null. Since it compares null with the objects exisiting we are getting NPE.
55+
* Same is the case, if we add null first and add other elements, NPE will happen.
56+
*/
57+
58+
59+
}
60+
61+
public static void main(String[] args) {
62+
// TODO Auto-generated method stub
63+
64+
TreeSetExample treeSetExample= new TreeSetExample();
65+
treeSetExample.treeSetExample();
66+
}
67+
68+
}

0 commit comments

Comments
 (0)