Friday, October 31, 2014

Java Synchronized List Pro



Get Synchronized List from Java Array List

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GetSynchronizedListFromArrayListExample
 {
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
List list = Collections.synchronizedList(arrayList);
    }
}

Get Synchronized Map from Java Hash Map

import java.util.HashMap;
import java.util.Collections;
import java.util.Map;
public class GetSynchronizedMapFromHashMapExample
 {
public static void main(String[] args)
{
HashMap hashMap = new HashMap(); Map map = Collections.synchronizedMap(hashMap);
   }
}

Get Synchronized Map from Java Tree Map

import java.util.TreeMap;
import java.util.Collections;
import java.util.Map;
public class GetSynchronizedMapFromTreeMapExample
{
 public static void main(String[] args)
{
TreeMap treeMap = new TreeMap();
 Map map = Collections.synchronizedMap(treeMap);
    }
}

Get Synchronized Set from Java Hash Set

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

Java Hashset Pro



Find maximum element of Java Array List

import java.util.ArrayList;
import java.util.Collections; 
public class FindMaximumOfArrayListExample
{
public static void main(String[] args)
 {
ArrayList arrayList = new ArrayList();
arrayList.add(new Integer("327482"));
arrayList.add(new Integer("13408"));
arrayList.add(new Integer("802348"));
arrayList.add(new Integer("345308"));
arrayList.add(new Integer("509324"));
Object obj = Collections.max(arrayList);
System.out.println("Maximum Element of Java ArrayList is : " + obj);
    }
}

Find maximum element of Java Hash Set

import java.util.HashSet;
import java.util.Collections;  
public class FindMaximumOfHashSetExample
{
 public static void main(String[] args)
{
HashSet hashSet = new HashSet();
hashSet.add(new Long("923740927"));
hashSet.add(new Long("4298748382"));
hashSet.add(new Long("2374324832"));
hashSet.add(new Long("2473483643"));
hashSet.add(new Long("32987432984"));
Object obj = Collections.max(hashSet);
System.out.println("Maximum Element of Java HashSet is : " + obj);
   }
}

Find maximum element of Java Vector

import java.util.Vector;
import java.util.Collections;
public class FindMaximumOfVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add(new Double("324.4324"));
v.add(new Double("345.3532"));
v.add(new Double("342.342"));
v.add(new Double("357.349"));
v.add(new Double("23.32453"));
Object obj = Collections.max(v);
System.out.println("Maximum Element of Java Vector is : " + obj);
   }
}

Get Enumeration over Java Array List

import java.util.Enumeration;
import java.util.ArrayList;
import java.util.Collections;
public class GetEnumerationOverArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
Enumeration e = Collections.enumeration(arrayList);
System.out.println("Enumerating through Java ArrayList");
while(e.hasMoreElements())
System.out.println(e.nextElement());
   }
}

Get Enumeration over Java Hash Set

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Collections;
public class GetEnumerationOverHashSetExample
{
public static void main(String[] args)
{
HashSet hashSet = new HashSet();
hashSet.add("A");
hashSet.add("B");
hashSet.add("D");
hashSet.add("E");
hashSet.add("F");
Enumeration e = Collections.enumeration(hashSet);
System.out.println("Enumerating through Java HashSet");
while(e.hasMoreElements())
System.out.println(e.nextElement());
   }
}

Get Enumeration over Java Vector

import java.util.Enumeration;
import java.util.Vector;
import java.util.Collections;
public class GetEnumerationOverVectorExample
{
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");
Enumeration e = Collections.enumeration(v);
System.out.println("Enumerating through Java Vector");
while(e.hasMoreElements())
System.out.println(e.nextElement());
  }
}