The set interface extends the collection interface in java.
Does not allow duplication
Allows only unique things.
Does not provide positional access.
Set<String> set = Set.of("Apple", "Banana", "Cat");
System.out.println(" Set : " + set);
The HashSet In Java
Hash Set does not allow duplication.
Does not care about the insertion order.
Does not care about sorded order
It uses hash table.
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class HashsetClass {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
// adding elements in hashset
numbers.add(29);
numbers.add(23);
numbers.add(3);
numbers.add(2);
System.out.println(" Hashset is : " + numbers);
}
}
The Linked HashSet In Java
Care about insertion order.
No duplication allowed.
Does not care about sorted order.
package setInterface;
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetClass {
public static void main(String[] args) {
Set<Integer> numbers = new LinkedHashSet<>();
// adding elements in hashset
numbers.add(29);
numbers.add(23);
numbers.add(3);
numbers.add(2);
System.out.println("Linked Hashset is : " + numbers);
}
}
The Tree Set In Java
Stores data in sorted order.
The left side data is smaller than the node and the right side data is larger than the node.
Insertion, deletion and searching are not costly.
Does not allow duplication.
It implements Navigable set so it provides more function then hash set or linked set.
package setInterface;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<Integer> numbers = new TreeSet<>();
// adding elements in tree set
numbers.add(29);
numbers.add(23);
numbers.add(3);
numbers.add(2);
numbers.add(92);
System.out.println("Tree Set is : " + numbers);
}
}
`
Methods in Tree Set
To take an example we have created the tree set which has the following below elements.
Tree Set is : [1, 2, 3, 12, 23, 29, 40, 45, 92, 99]
floor() : to find lower than any number say 40, it displays only 1 number and inclusive of the number. for above set it shows 40
lower (): we use lower () to get exclusive that number say 40. for above set it show 29.
ceiling() : to find higher than any number say 40, it displays only 1 number and is inclusive of the number. for above set it shows 40
higher(): we use lower () to get exclusive that number say 40. for the above, set it show 45.
subset() : to get subset between two numbers (3,40). In this lower limit is inclusive and upper limit is exclusive O/P – [3, 12, 23, 29]
tailSet(40) : get all number smaller then a number inclusive O/P – [40, 45, 92, 99]
headSet(40) : get all number greater then a number exclusive O/P – [1, 2, 3, 12, 23, 29]
package setInterface;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<Integer> numbers = new TreeSet<>(Set.of(1, 3, 99, 12, 45, 40));
// adding elements in tree set
numbers.add(29);
numbers.add(23);
numbers.add(3);
numbers.add(2);
numbers.add(92);
System.out.println("Tree Set is : " + numbers);
// to find lower than any number say 40, it display only 1 number
// and inclusive of the number
System.out.println("number lower than 40 : " + numbers.floor(40));
// O/P - 40
// we use lower() to get exclusive of that number
System.out.println("number lower than 40 : " + numbers.lower(40));
// O/P - 29
// to find greater than any number say 40, it display only 1 number
// and inclusive of the number
System.out.println("number lower than 40 : " + numbers.ceiling(40));
// O/P - 40
// we use lower() to get exclusive of that number
System.out.println("number lower than 40 : " + numbers.higher(40));
// O/P - 45
// to get subset between two numbers (3,40)
System.out.println("get subset between two numbers : " + numbers.subSet(3, 40));
// O/P - [3, 12, 23, 29]
// get all number greater then a number exclusive
System.out.println("headSet(40) : " + numbers.headSet(40));
// O/P - [1, 2, 3, 12, 23, 29]
// get all number smaller then a number inclusive
System.out.println("tailSet(40) : " + numbers.tailSet(40));
// O/P - [40, 45, 92, 99]
}
}