import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class SetOperations {
	public static void main(String[] args) {
		Set<Character> set = new HashSet<Character>();
		set.add('a');
		set.add('b');
		set.add('c');
		set.add('d');
		
		Set<Character> set2 = new HashSet<Character>();
		set2.add('e');
		set2.add('d');
		set2.add('c');
		set2.add('f');
		
		Set<Character> set3 = new HashSet<Character>();
		set3.add('f');
		set3.add('e');
		set3.add('b');
		
		// Union of set and set2.
		set.addAll(set2);
		
		Iterator<Character> iter = set.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		System.out.println();
		
		// Intersection of union with set3.
		set.retainAll(set3);
		
		iter = set.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		System.out.println();
		
		// Subtract set2 from intersection.
		set.removeAll(set2);
		
		iter = set.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		System.out.println();
		
		set.add('c');
		set.add('a');
		
		ArrayList<Character> list = new ArrayList<Character>(set);
		iter = list.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		System.out.println();
		
		Collections.sort(list);
		
		iter = list.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		System.out.println();
		
		int index = Collections.binarySearch(list, 'c');
		if (index != -1) {
			System.out.println("Character c found at index " + index);
		}
		
		list.add('c');
		list.add('c');
		
		int count = Collections.frequency(list, 'c');
		System.out.println("List has " + count + " instances of c");
		
		Collections.swap(list, 0, 3);
		
		iter = list.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		System.out.println();
		
		Collections.rotate(list, 3);
		
		iter = list.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
		System.out.println();
		
		Collections.replaceAll(list, 'c', 'f');
		
		char max = Collections.max(list);
		System.out.println("Max value: " + max);
	}
}

