Java provides an interface to store and manipulate data known as Collection Interface. The collection is the super interface for a Set interface that helps store any type of object and manipulate it. Set interface stands out as a Collection that does not allows duplicate data in it, i.e. if d1 and d2 are two data entries in the same Set, then the result of d1.equals(d2) should be false. Almost one null element is allowed in Set. Set models the mathematical set abstraction.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Some of the implementations of sets are HashedSet, LinkedHashSet, or TreeSet as sorted representation.
Below are the examples of Set Interface in Java:
Code:
import java.util.*; public class Main{ public static void main(String[] args) { // Set demonstration using HashSet Set<Integer > hash = new HashSet<Integer>(); hash.add(1); hash.add(4); hash.add(1); hash.add(3); hash.add(2); System.out.print("Set output without duplicates"); System.out.println(hash); } }
Output:
Code:
import java.util.*; public class Main{ public static void main(String[] args) { // Set demonstration using TreeSet Set<Integer> tree = new TreeSet<Integer>(); tree.add(1); tree.add(4); tree.add(1); tree.add(3); tree.add(2); System.out.print("Set output without duplicates and sorted data "); System.out.println(tree); } }
Output:
Methods supported by Set for various data object storage and manipulation.
Usage of methods in Our Code:
Code:
import java.util.LinkedHashSet; public class Main { public static void main(String[] args) { LinkedHashSet<String> linked = new LinkedHashSet<String>(); // Adding element to LinkedHashSet linked.add("Apple"); linked.add("Ball"); linked.add("Cat"); linked.add("Dog"); // Cannot add new element as Apple already exists linked.add("Apple"); linked.add("Egg"); System.out.println("Size of LinkedHashSet: " + linked.size()); System.out.println("Old LinkedHashSet:" + linked); System.out.println("Remove Dog from LinkedHashSet: " + linked.remove("Dog")); System.out.println("Trying Remove Zoo which is not present "+ "present: " + linked.remove("Zoo")); System.out.println("Check if Apple is present=" + linked.contains("Apple")); System.out.println("New LinkedHashSet: " + linked); } }
Output:
HashSet is generally used for search, delete and insert operations. HashSet is faster than TreeSet and uses a hash table. TreeSet whereas used for storing purpose due to its sorted data storage property. TreeSet uses TreeMap from the backend on Java. In order to store sorted data, insert elements into a hashmap and then insert data into a tree to get it sorted.
There are 3 ways to do this:
Code:
import java.util.*; public class Main { public static void main(String[] args) { Set<Integer > hash = new HashSet<Integer>(); hash.add(1); hash.add(4); hash.add(1); hash.add(3); hash.add(2); System.out.print("HashSet"); System.out.println(hash); //adding HashSet as a parameter to TreeSet constructor Set< Integer> treeSet = new TreeSet<>(hash); // Print TreeSet System.out.println("TreeSet: " + treeSet); } }
Output:
Code:
import java.util.*; public class Main { public static void main(String[] args) { Set<Integer > hash = new HashSet<Integer>(); hash.add(1); hash.add(4); hash.add(1); hash.add(3); hash.add(2); System.out.print("HashSet"); System.out.println(hash); //converting HashSet to TreeSet using addAll() method Set<Integer> treeSet = new TreeSet<>(); treeSet.addAll(hash); // Print TreeSet System.out.println("TreeSet: " + treeSet); } }
Output:
Code:
import java.util.*; public class Main { public static void main(String[] args) { Set<Integer > hash = new HashSet<Integer>(); hash.add(1); hash.add(4); hash.add(1); hash.add(3); hash.add(2); System.out.print("HashSet"); System.out.println(hash); //converting HashSet to TreeSet using for each loop Set<Integer> treeSet = new TreeSet<>(); for (Integer i : hash) { treeSet.add(i); } // Print TreeSet System.out.println("TreeSet: " + treeSet); } }
Output:
This is a guide to Set Interface in Java. Here we discuss the Introduction to Set Interface and how it is used to store and manipulate data in Java and its methods. You can also go through our other suggested articles to learn more –
The above is the detailed content of Set Interface in Java. For more information, please follow other related articles on the PHP Chinese website!