Home > Java > javaTutorial > body text

Set Interface in Java

WBOY
Release: 2024-08-30 16:11:25
Original
458 people have browsed it

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.

Examples to Implement Set Interface in Java

Below are the examples of Set Interface in Java:

1. HashSet

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);
}
}
Copy after login

Output:

Set Interface in Java

2. TreeSet

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);
}
}
Copy after login

Output:

Set Interface in Java

Methods of Set Interface in Java

Methods supported by Set for various data object storage and manipulation.

  • add(Element e): Adds a specified element in the set.
  • addAll(Collection c): Adds all the elements present in the specified collection.
  • clear(): Removes all the elements from the set.
  • contains(Object o): Returns true if Set contains the same object as the specified object.
  • containsAll(Collection c): Returns true if set contains all of the elements in the specified collection.
  • size(): Returns the number of elements in the Set.
  • equals(Object o): It compares and returns true if our object is equal to the object specified.
  • hashCode(): It returns the hashcode value for the set.
  • isEmpty(): It returns true if the set contains no element.
  • iterator(): It returns an iterator on the set, which helps to trace through the complete set.
  • remove(Object o): Removes the specified element from the existing set.
  • removeAll(Collection c): Removes the specified collection from the existing set.
  • toArray(): It returns the particular array containing all the elements as in Set.

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);
}
}
Copy after login

Output:

Set Interface in Java

Converting HashSet to TreeSet

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:

1. Pass the HashSet created

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);
}
}
Copy after login

Output:

Set Interface in Java

2. Using addAll() method

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);
}
}
Copy after login

Output:

Set Interface in Java

3. Using for-each loop

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);
}
}
Copy after login

Output:

Set Interface in Java

Recommended Article

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 –

  1. Layout in Java
  2. Java Compilers
  3. Java Parallel Stream
  4. Java BufferedReader

The above is the detailed content of Set Interface in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!