[go: up one dir, main page]

0% found this document useful (0 votes)
5 views6 pages

Set

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Set

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

SET:

====
It is the Child Interface of Collection.
 If we want to Represent a Group of Individual Objects as a
Single Entity where Duplicates are Not allowed
and Insertion Order is Not Preserved then we should go for Set.
 Set Interface doesn't contain any New Methods and Hence we have
to Use Only Collection Interface Methods.

1)HashSet

The Underlying Data Structure is Hashtable.


Insertion Order is Not Preserved and it is Based on hashCode of the Objects.
Duplicate Objects are Not Allowed.
If we are trying to Insert Duplicate Objects then we won't get any Compile Time OR
Runtime Error.
add() Simply Returns false.
null Insertion is Possible.
Heterogeneous objects are allowed.
HashSet implements Serializable and Cloneable Interfaces but Not RandomAccess.
If Our Frequent Operation is Search Operation, then HashSet is the Best Choice.

Example :

package set;

import java.util.HashSet;
import java.util.Set;

public class SetDemo {

public static void main(String[] args) {

Set<Integer> s = new HashSet<>();


System.out.println(s.add(45));
System.out.println(s.add(67));
System.out.println(s.add(89));
System.out.println(s.add(null));
System.out.println(s.add(45));
System.out.println(s);

}
}

Example :

package set;

import java.util.HashSet;
import java.util.Set;

public class SetDemo {

public static void main(String[] args) {


Set s = new HashSet<>();
System.out.println(s.add(45));
System.out.println(s.add(67));
System.out.println(s.add("Santosh"));
System.out.println(s.add(null));
System.out.println(s.add(45));
System.out.println(s);

}
}

Example :

package set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetDemo {

public static void main(String[] args) {

Set<Integer> s = new HashSet<>();


System.out.println(s.add(45));
System.out.println(s.add(67));
System.out.println(s.add(89));
System.out.println(s.add(null));
System.out.println(s.add(45));
System.out.println(s);

Iterator<Integer> iterator = s.iterator();


while (iterator.hasNext()) {
Integer next = iterator.next();
System.out.println(next);
}

for (Integer s1 : s) {
System.out.println(s1);
}
}
}

Constructors:

1) HashSet h = new HashSet();


Creates an Empty HashSet Object with Default Initial Capacity 16 and Default
Fill Ratio : 0.75.

The initial capacity of HashSet is 16 . When the load factor (0.75) is reached,
i.e. 16 * 0.75 = 12 ;
on the insertion of the 12th element the capacity is doubled , i.e. it becomes
32 .

Example :
package set;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo {

public static void main(String[] args) {

HashSet s = new HashSet();


System.out.println(s.add(23));
System.out.println(s.add(45));
System.out.println(s.add(78));
s.add(77);
s.add(89);
s.add(96);
s.add(66);
s.add(null);
s.add("Santosh");
s.add(33);
s.add(44);
s.add(11);

System.out.println(s.size());

System.out.println(s);

}
}

2) HashSet h = new HashSet(int initial Capacity);


Creates an Empty HashSet Object with specified Initial Capacity and Default
Fill Ratio : 0.75.

Example :

package set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HashSetDemo {

public static void main(String[] args) {

Set s = new HashSet(12);


System.out.println(s.add(23));
System.out.println(s.add(45));
System.out.println(s.add(78));
s.add(77);
s.add(89);
s.add(96);
s.add(66);
s.add(null);
s.add("Santosh");

System.out.println(s);

}
}

3) HashSet h = new HashSet(initial Capacity, float fillRatio);

Example :

package set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetDemo {

public static void main(String[] args) {

Set s = new HashSet(12,50);


System.out.println(s.add(23));
System.out.println(s.add(45));
System.out.println(s.add(78));
s.add(77);
s.add(89);
s.add(96);

System.out.println(s);

}
}

4) HashSet h = new HashSet(Collection c);

package set;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetDemo {


public static void main(String[] args) {

ArrayList al = new ArrayList();


al.add(11);
al.add(22);
al.add(11);
al.add(22);
al.add(55);
System.out.println(al);

HashSet s=new HashSet(al);


System.out.println(s);
}
}

2)LinkedHashSet:

3.1.1) LinkedHashSet:  It is the Child Class of HashSet.


 It is Exactly Same as HashSet Except the following Differences.

HashSet LinkedHashSet
The Underlying Data Structure is Hashtable. The Underlying
Data Structure is a Combination
of LinkedList and
Hashtable

Insertion Order is Not Preserved. Insertion Order


will be Preserved.
Introduced in 1.2 Version. Introduced in 1.4
Version.

Example :

package set;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class SetDemo {

public static void main(String[] args) {


LinkedHashSet ls = new LinkedHashSet();
ls.add(11);
ls.add(88);
ls.add(11);
ls.add(77);
ls.add(88);
ls.add(90);
System.out.println(ls);
}
}

You might also like