Set
import java.util.*;
public class S
{
public static void main(String args[])
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(Arrays.asList(new Integer[] {1, 2, 3, 4}));
System.out.println("A: "+a);
Set<Integer> b = new HashSet<Integer>();
b.addAll(Arrays.asList(new Integer[] {5, 2, 6, 4}));
System.out.println("B: "+b);
//Union
Set<Integer> c = new HashSet<Integer>(a);
c.addAll(b);
System.out.println("Union: "+c);
//Intersection
Set<Integer> d = new HashSet<Integer>(a);
d.retainAll(b);
System.out.println("Intersection: "+d);
//Difference
Set<Integer> e = new HashSet<Integer>(a);
e.removeAll(b);
System.out.println("Difference: "+e);
}
}
SortedSet
import java.util.*;
public class S
{
public static void main(String args[])
{
SortedSet<String> s = new TreeSet<String>();
s.add("practice");
s.add("geeksforgeeks");
s.add("quiz");
s.add("code");
System.out.println(s);
//first
System.out.println("First: "+s.first());
//last
System.out.println("Last: "+s.last());
//headSet
System.out.println("Head: "+s.headSet("quiz"));
//subSet
System.out.println("Subset: "+s.subSet("code", "practice"));
//tailSet
System.out.println("Tail: "+s.tailSet("code"));
}
}
NavigableSet
import java.util.*;
public class S
{
public static void main(String args[])
{
NavigableSet<Integer> s = new TreeSet<Integer>();
//add
for(int i = 0; i < 10; i++)
s.add(i);
System.out.println(s);
//descending
System.out.println(s.descendingSet());
//lower
System.out.println(s.lower(4));//3
//floor
System.out.println(s.floor(4));//4
//ceiling
System.out.println(s.ceiling(4));//4
//higher
System.out.println(s.higher(4));//5
//headSet
System.out.println(s.headSet(4));
//subSet
System.out.println(s.subSet(3, 8));
//tailSet
System.out.println(s.tailSet(4));
//pollFirst
System.out.println(s.pollFirst());
System.out.println(s);
//pollLast
System.out.println(s.pollLast());
System.out.println(s);
}
}
HashSet
- Use a hashMap for sorting its objects, key and value have same value, complexity O(1)
- Objects in hashSet are not sorted, order is based on their hash code
- Not keep the insert order
- NULL elements are allowed in HashSet
import java.util.*;
public class S
{
public static void main(String args[])
{
Set<Integer> s = new HashSet<Integer>();
Random r = new Random();
//add
s.add(r.nextInt(100));
s.add(r.nextInt(100));
s.add(r.nextInt(100));
s.add(1);
//contains
if(s.contains(1))
System.out.println("Contains 1 ...");
//size
System.out.println("Size: "+s.size());
//remove
s.remove(1);
System.out.println(s);
}
}
LinkedHashSet
- Maintains the insert order
import java.util.*;
public class S
{
public static void main(String args[])
{
Set<Integer> a = new LinkedHashSet<Integer>();
Random r = new Random();
a.add(r.nextInt(100));
a.add(r.nextInt(100));
a.add(r.nextInt(100));
a.add(1);
a.add(100);
a.add(2);
System.out.println(a);
}
}
TreeSet
- Elements are sorted by key
- Not keep the insert order
- Allow NULL inserted
- Self-balance binary tree, add, remvoe and search take O(logn) time
import java.util.*;
public class S
{
public static void main(String args[])
{
Set<Integer> s = new TreeSet<Integer>();
Random r = new Random();
//add
s.add(r.nextInt(100));
s.add(r.nextInt(100));
s.add(r.nextInt(100));
s.add(1);
s.add(2);
s.add(100);
//contains
if(s.contains(1))
System.out.println("Contains 1 ...");
//size
System.out.println("Size: "+s.size());
//remove
s.remove(1);
System.out.println(s);
}
}
EnumSet
- All elements of each EnumSet instance must be elements of a single enum type
public enum Day
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
}
import java.util.*;
public class DaySet
{
public static void main(String args[])
{
EnumSet<Day> s1 = EnumSet.of(Day.Monday, Day.Tuesday, Day.Sunday);
//toString
System.out.println(s1);
//iterator
Iterator itr = s1.iterator();
while(itr.hasNext())
{
System.out.printf("%s\n", itr.next());
}
//complementOf
EnumSet<Day> s2 = EnumSet.complementOf(s1);
System.out.println(s2);
//allOf
EnumSet<Day> s3 = EnumSet.allOf(Day.class);
System.out.println(s3);
//range
EnumSet<Day> s4 = EnumSet.range(Day.Monday, Day.Friday);
System.out.println(s4);
}
}
Reference