0% found this document useful (0 votes)
12 views13 pages

Java Set Interface and Implementations

Uploaded by

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

Java Set Interface and Implementations

Uploaded by

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

SET ( I ) :-

SET ( I )
↙ ↘
HASHSET SORTEDSET ( I )
↓ ↓
LINKED HASHSET NAVIGABLE SET ( I )

TREESET
It is a predefined Interface present in “ UTIL “ package, It does not follows any
insertion order of elements.
It will follows internally hashing mechanism for storing the elements.
It does not allows duplicate values.

For this interface we have three implementation classes


1. Hashset
2. Linked Hashset
3. Treeset

HASHSET ( C ) :-
It is a predefined class present in “ UTIL “ package it internally contains or uses
hash table data structure.
It does not allow duplicate elements.
It does not follow any order of elements.
It will allow null values, but only one.
It is a non-synchronized class.
CONSTRUCTORS :-
HashSet ( )
HashSet ( int )
HashSet ( int, float )
HashSet ( collection )

METHODS :-
1. public int size ( )
2. public boolean isEmpty ( )
3. public boolean add ( object )
4. public boolean addAll ( collection )
5. public boolean remove ( object )
6. public boolean removeAll ( collection )
7. public void clear ( )
8. public boolean retainAll ( collection )
9. public boolean contains ( object )
10. public boolean containsAll ( collection )
11. public Iterator iterator ( )
12. public Object [ ] toArray ( )
13. public Object clone ( )
14. public boolean equals ( object )
15. public int hashcode ( )

USE AND DESCRIPTION ABOUT METHODS :-


1. int size ( ) → To get the number of elements in the Set.
2. boolean isEmpty ( ) → To check whether the set is empty or not.
3. boolean add ( object ) → Appends the specified element to the end of the set.
4. boolean addAll ( collection ) → Appends the specified element at that given index of the set .
5. boolean remove ( object ) → Removes the Specified element from the Set.
6. boolean removeAll ( collection ) → Removes the common elements of the any two set
Objects.
7. void clear ( ) → Removes all the elements from the set.
8. boolean retainAll ( Collection ) → Retains only the elements that are common in both sets.
9. boolean contains ( object ) → Returns true if the set contains the specified element else returns
false.
10. boolean containsAll ( collection ) → Returns true if the set contains the same elements else
returns false.
11. Iterator iterator ( ) → Returns an iterator over the elements in the set in proper sequence.
12. Object [ ] toArray ( ) → Returns an array containing all of the elements in the set in proper
sequence.
13. Object clone ( ) → It is used to return a shallow copy of an HashSet.

14. boolean equals (Collection object ) → Returns true if the elements in Object1 matches to the
elements in that particular index of Object2 if not matches returns false.

15. int hashcode ( ) → Returns hashcode of that set.


1. Create a java application where we need to create object of hashset class and add
duplicate values and null values then display the elements using iterator.
import [Link].*;
public class Arraylist {

public static void main(String[] args) {


HashSet hs=new HashSet();
[Link](1);
[Link](2);
[Link](3);
[Link](1);
[Link](null);
[Link](2);
[Link](null);
[Link](5);
Iterator itr=[Link]();
while([Link]())
{
[Link]([Link]()+" ");
}
}
}

OUT PUT :-
123456
JAMES CSS SQL

2. Create a java application where we need to display the common values in 2 sets.
import [Link].*;
public class Arraylist {

public static void main(String[] args) {


HashSet hs=new HashSet();
[Link](1);
[Link](2);
[Link](3);
[Link](4);
HashSet hs1=new HashSet();
[Link](2);
[Link](3);
[Link](hs1);
Iterator itr=[Link]();
while([Link]())
[Link]([Link]()+" ");
}
}
OUT PUT :- 2 3

LINKED HASHSET :-

It is a predefined class available in ” UTIL “ package.


It does not allow duplicate elements.
It will follow insertion order of elements.
It will allow null values but only one.
It is a non-synchronized class.
It is an implementation class of set and Hashset.
It internally follows Hashtable with a doubly linked list running through it.
CONSTRUCTORS :-
Linked HashSet ( )
Linked HashSet ( collection )

Methods which are available in set interface and Hashset class can be used in linked HashSet.

1. Create a java application where we need to create object for linked Hashset class and add
only Integer type of values, Create second object for Linked Hashset add only Integer
values including the values of previous object of linked hashset then display all these
values including the sum of all these values by using iterator.
import [Link].*;
public class Arraylist {

public static void main(String[] args) {


LinkedHashSet<Integer> hs=new LinkedHashSet<>();
[Link](1);
[Link](2);
[Link](3);
[Link](4);
HashSet hs1=new HashSet(hs);
[Link](7);
[Link](8);
[Link](hs1);
Iterator itr=[Link]();
int sum=0;
while([Link]())
{
int h=(int)[Link]();
sum=sum+h;
[Link](h+" ");
}
[Link](" -> "+sum);
}
}
OUT PUT :-
1 2 3 4 7 8 -> 25

2. Create a java application where we have an array of integer remove duplicate elements
and preserve the order of the remaining elements using a linked Hashset.
import [Link].*;
public class Arraylist {

public static void main(String[] args) {


LinkedHashSet ll=new LinkedHashSet();
int a[]= {1,2,4,8,2,3,10,80,4,10};
for(int i=0;i<[Link];i++)
[Link](a[i]);
Iterator li=[Link]();
while([Link]())
[Link]([Link]()+" ");
}
}
OUT PUT :-
1 2 4 8 3 10 80

SORTED SET :-
It is a predefined Interface present in “ UTIL “ package.
It will store the elements in sorting order and by default ascending order.
It will allow null value.
It does not allow any duplicate values.
METHODS :-
1. public int size ( )
2. public boolean isEmpty ( )
3. public Iterator iterator ( )
4. public Object first ( )
5. public Object last ( )
6. public boolean contains ( Object )
7. public boolean containsAll ( Object )
8. public void clear ( )
9. public Set tailSet ( Object )
10. public Set headSet ( Object )
11. public Set subset ( Object, Object )

USE AND DESCRIPTION ABOUT METHODS :-


1. int size ( ) → To get the number of elements in the Set.
2. boolean isEmpty ( ) → To check whether the set is empty or not.
3. Iterator iterator ( ) → Returns an iterator over the elements in the set in proper sequence.

4. Object first ( ) → Retrieves the first element from the set.


5. Object last ( ) → Retrieves the last element from the set.

6. boolean contains ( Object ) → Returns true if the set contains the specified element else
returns false.
7. boolean containsAll ( collection ) → Returns true if the set contains the same elements else
returns false.
8. void clear ( ) → Removes all the elements from the set.
9. Set tailSet ( Object ) → Retrieves the set of elements which are next to the given object and also
includes the object when returning set.

10. Set headSet ( Object ) → Retrieves the set of elements which are before to the given object and
excludes the object when returning set.

11. Set subset ( Object, Object ) → Retrieves the set of elements which are in between the given
object and includes the 1st object excludes 2nd object when returning set.
NAVIGABLE SET ( I ):-

It is a predefined Interface available in “ UTIL ” package, it does not allow any


duplicates values.
It will provide the flexibility like where we can get the values and remove the
elements at first and last.
We can get the values inherited from sorted set interface.

METHODS :-
1. public Object pollFirst ( )
2. public Object pollLast ( )
3. public Object lower ( Object )
4. public Object higher ( Object )
5. public Iterator descendingIterator ( )
6. public Set descendingSet ( )
7. public Set headset ( Object )
8. public Set tailSet ( Object )
9. public Comparator comparator ( )
10. public Set subset ( Object, Object )

USE AND DESCRIPTION ABOUT METHODS :-


1. Object pollFirst ( ) → Retrieves and removes the first element from the set.

2. Object pollLast ( ) → Retrieves and removes the last element from the set.

3. Object lower ( Object ) → Retrieves the next lowest element from the set.
4. Object higher ( Object ) → Retrieves the next highest element from the set.

5. Iterator descendingIterator ( ) → Returns Iterator object which retrieves the elements in


descending order.

6. Set descendingSet ( ) → Returns a Set in descending order.

7. Set headset ( Object ) → Retrieves the set of elements which are before to the given object and
excludes the object when returning set.
8. Set tailSet ( Object ) → Retrieves the set of elements which are next to the given object and also
includes the object when returning set.
9. Comparator comparator ( )
10. Set subset ( Object, Object ) → Retrieves the set of elements which are in between the given
object and includes the 1st object excludes 2nd object when returning set.

TREE SET :-
It is a predefined class available in “ UTIL ” package.
It does not allow both duplicates and null values.
It will provide the flexibility like where we can store and retrieve the elements in
stored order and by default ascending order.
It is the implementation class of set, sorted set, Navigable set.
It is a non-synchronized class.

CONSTRUCTORS :-
TreeSet ( )
TreeSet ( collection )
TreeSet ( comparator )
TreeSet ( sortedSet )

METHODS :-

Methods which are available in set, sorted Set, Navigable set are available in this class.

1. Create a java application where we need to create object for tree set class add only
integer type of values then display all these values in both ascending and descending
order.
import [Link].*;
public class Arraylist {

public static void main(String[] args) {


TreeSet<Integer> ts=new TreeSet<>();
[Link](4);
[Link](2);
[Link](3);
[Link](8);
[Link](6);
Iterator itr=[Link]();
while([Link]())
{
[Link]([Link]()+" ");
}
Iterator itr1=[Link]();
[Link]();
while([Link]())
{
[Link]([Link]()+" ");
}
}
}
OUT PUT :-
23468
86432

2. Create a java application where we need to create object of tree set class and add multiple
employee salaries to the object then display the values of Highest and Lowest salaries,
then display the range of salaries, Then salaries which are less than given value and the
salaries which are greater than given value.
import [Link].*;
public class Arraylist {

public static void main(String[] args) {


TreeSet<Double> ts=new TreeSet<>();
[Link](25334.34);
[Link](436436.436);
[Link](354636.346);
[Link](343254.34);
[Link](436344.34);
[Link](765344.674);
[Link]([Link]());
[Link]([Link]());
[Link]([Link](436344.34,6547484.45));
[Link]([Link](436436.436));
[Link]([Link](436436.436));
}
}
OUT PUT :-
25334.34
765344.674
[436344.34, 436436.436, 765344.674]
[436436.436, 765344.674]
[25334.34, 343254.34, 354636.346, 436344.34]

You might also like