Using set in Java makes it easy to save the desired type in a variable in a collection type . The main application is in the display list.
set is an element that does not contain duplicate elements. collection. more specifically, the set does not contain elements that satisfy e1.equals (E2) to E1 and E2, and contains a maximum of one null element. As its name implies, this interface mimics the mathematical set abstraction.
The following is an example of using Set in Java
Import java.util.*;
public class Test {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Set set=new HashSet ();
Set.add ("abc");
Set.add ("CDE");
Set.add ("EFG");
Set.add ("FGH");
Set.add ("abc"); //Duplicate abc,set will automatically remove it
System.out.println ("size=" + set.size ());
List List = new ArrayList ();
List.add ("abc");
List.add ("AAA");
List.add ("fff");
Set.addall (list); //Add the values in the list to set and remove the duplicate ABC
System.out.println ("size=" + set.size ());
for (Iterator it = Set.iterator (); It.hasnext ();)
{
System.out.println ("value=" +it.next (). toString ());
}
}
}
Use of the Set class interface in Java