Normally set is defined by curly braces "{}"
Example:
{index,middle,ring,pinky,thum} - Hand Fingers types
{1,12,33,43}
It will remove duplicate elements and retain unique element.
We can assign only distinct or unique element as set element
Set Type
-HashSet
-TreeSet
Set has mainly two methods
-add() - add the elements
-get() - get the elements
Map VS Set
Set
data is called Elements
one element
Map
data is called key-value pair
key and value (all values based on keys)
Common things Set and Map
-Does not allow duplicate elements
Example of Set Java Code..
//import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class SetExample {
public static void main(String[] args) {
// Create HashSet | TreeSet
//HashSet is not considered the order
//TreeSet is considered the order
//Set set =new HashSet();
Set set =new TreeSet();
set.add("MonDay");
set.add("TuesDay");
set.add("WednesDay");
set.add("ThursDay");
set.add("FriDay");
set.add("SaturDay");
set.add("SunDay");
Iterator i = set.iterator();
while (i.hasNext()){
System.out.println(i.next());
}
}
}
No comments:
Post a Comment