Thursday, October 30, 2014

Java Treeset Pro



Get Synchronized Set from Java Tree Set

import java.util.TreeSet;
import java.util.Collections;
import java.util.Set;
public class GetSynchronizedSetFromTreeSetExample
{
public static void main(String[] args)
{
TreeSet treeSet = new TreeSet(); 
Set set = Collections.synchronizedSet(treeSet);
    }
}

Iterate Over Unmodifiable Collection

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class UnmodifiableCollection
{
public static void main(String args[])
{
List list = new ArrayList();
list.add("This");
list.add("is");
list.add("Unmodifiable Collection");
System.out.println("Element added to list: " + list.get(2));
Collection immutableCol = Collections.unmodifiableCollection(list);
Iterator iterator = immutableCol.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
      }
   }
}

Perform Binary Search on Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class BinarySearchArrayListExample
{
 public static void main(String[] args)
{
ArrayList arrayList = new ArrayList(); arrayList.add("1");
arrayList.add("4");
arrayList.add("2");
arrayList.add("5");
arrayList.add("3");
Collections.sort(arrayList);
System.out.println("Sorted ArrayList contains : " + arrayList);
int index = Collections.binarySearch(arrayList,"4");
System.out.println("Element found at : " + index);
   }
}

Perform Binary Search on Java Vector

import java.util.Vector;
import java.util.Collections;
public class BinarySearchVectorExample
 {
public static void main(String[] args)
{
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
Collections.sort(v);
System.out.println("Sorted Vector contains : " + v);
int index = Collections.binarySearch(v,"D");
System.out.println("Element found at : " + index);
   }
}

Replace All Elements Of Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class ReplaceAllElementsOfArrayListExample
{
 public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("D");
System.out.println("Before replacement, ArrayList contains : " + arrayList);
Collections.fill(arrayList,"Replaced");
System.out.println("After replacement, ArrayList contains : " + arrayList);
   }
}

Replace All Elements Of Java Vector

import java.util.Vector;
import java.util.Collections;
public class ReplaceAllElementsOfVectorExample
{
public static void main(String[] args)
 {
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("D");
System.out.println("Before replacement, Vector contains : " + v);
Collections.fill(v,"REPLACED");
System.out.println("After replacement, Vector contains : " + v);
  }
}

Replace all occurrences of specified element of Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class ReplaceAllArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("A");
arrayList.add("C");
arrayList.add("D");
System.out.println("ArrayList Contains : " + arrayList);
Collections.replaceAll(arrayList, "A","Replace All");
System.out.println("After Replace All, ArrayList Contains : " + arrayList);
   }
}

Replace all occurrences of specified element of Java Vector

import java.util.Vector;
import java.util.Collections;
public class ReplaceAllVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("A");
v.add("C");
v.add("D");
System.out.println("Vector Contains : " + v);
Collections.replaceAll(v, "A","Replace All");
System.out.println("After Replace All, Vector Contains : " + v);
    }
}

Reverse order of all elements of Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class ReverseOrderArrayListExample
{
 public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before Reverse Order, ArrayList Contains : " + arrayList);
Collections.reverse(arrayList);
System.out.println("After Reverse Order, ArrayList Contains : " + arrayList);
    }
}

Reverse order of all elements of Java Vector

import java.util.Vector;
import java.util.Collections;
public class ReverseOrderVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("C");
v.add("D");
v.add("E");
System.out.println("Before Reverse Order, Vector Contains : " + v);
Collections.reverse(v);  
System.out.println("After Reverse Order, Vector Contains : " + v);
    }
}

Shuffle elements of Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class ShuffleElementsOfArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before shuffling, ArrayList contains : " + arrayList);
Collections.shuffle(arrayList);
System.out.println("After shuffling, ArrayList contains : " + arrayList);
   }
}

No comments:

Post a Comment