java4all@1986 java. Powered by Blogger.

Important Points in Collections

>> Tuesday, May 24, 2011

To know Collections concept completely  and clearly follow the below steps.
1.Heterogeneous Objects:
                (a)TreeSet and TreeMap doesn't allow Heterogeneous Objects.
2.Insertion Order:
                (b)Only List & it's child Classes ,LinkedHaset ,LinkedHashMap preserved.
3.Duplicate Values: 
                (c)Only List and it's child Classes allow......In Map values are allowed.
4.Display Order:
                (d)For TreeMap and TreeSet as per sorting order.

 5.Null Acceptance:
                (e)HashTable, TreeSet,TreeMap doesn't allow.
6.Synchronized:
                 (f)Vector,Stack,HashTable,properties are Synchronized.

7.Default Sizes:
                  (g)List and Child Classes=10
                      Set and it's child Classes=16 
                      Map & it's child Classes=16.
8.Incermental size/Ratio: 
                    (h)List and it's child Classes=16
                         Set & Map=0.75(loadFactor,fillRatio) 

Read more...

To get duplicate key we use Identity HashMap with Example?

To identify duplicate keys in Identity HasMap JVM uses (==).

PROGRAM:
                  class DuplcateKeys{
                    IdentityHashMap ihm=new IdentityHashMap();
                   ihm.put("10", "BHaskar");
                   ihm.put("10","Ajantha");
                    System.out.println(ihm);
                              }
OUTPUT:
          {10 : Bhaskar,  10:Ajantha}

Read more...

To identify duplicate keys in HasMap?Example

1.To identify duplicate keys in HasMap JVM internally uses equals() Method.
                          
         PROGRAM:
                                class DuplicateKey
                                 {
                                   HashMap hm=new HashMap();
                                   hm.put("10","Bhaskar");
                                   hm.put("10","Ajantha");
                                   System.out.println(hm);
                                          }
                      OUTPUT :
                                      {10 , Ajantha}         

In HashMap  it overrides the duplicate    key of it's values.












Read more...

Get Synchronized Map from Java HashMap ?

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

Read more...

Search with a Comparator

import java.util.Arrays;
import java.util.Comparator;

public class AlphabeticSearch {

  public static void main(String[] args) {
    String[] sa = new String[] { "a""c""d" };
    AlphabeticComparator comp = new AlphabeticComparator();
    Arrays.sort(sa, comp);
    int index = Arrays.binarySearch(sa, sa[10], comp);
    System.out.println("Index = " + index);
  }
///:~

class AlphabeticComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    String s1 = (Stringo1;
    String s2 = (Stringo2;
    return s1.toLowerCase().compareTo(s2.toLowerCase());
  }
///:~

Read more...

Reverse Order Comparator

import java.util.Comparator;
public class ReverseOrder<T> implements Comparator<T>
{
    private Comparator<T> m_comparator;
    
    public ReverseOrder(Comparator<T> comp)
    {
        m_comparator = comp;
    }

public int compare(T arg0, T arg1)
    {
        return -* m_comparator.compare(arg0, arg1);
    }
}

Read more...

Creating a Comparable object

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class Person implements Comparable {
  String firstName, lastName;

  public Person(String f, String l) {
    this.firstName = f;
    this.lastName = l;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public String toString() {
    return "[dept=" + firstName + ",name=" + lastName + "]";
  }

  public int compareTo(Object obj) {
    Person emp = (Personobj;
    int deptComp = firstName.compareTo(emp.getFirstName());

    return ((deptComp == 0? lastName.compareTo(emp.getLastName())
        : deptComp);
  }

  public boolean equals(Object obj) {
    if (!(obj instanceof Person)) {
      return false;
    }
    Person emp = (Personobj;
    return firstName.equals(emp.getFirstName())
        && lastName.equals(emp.getLastName());
  }

  public static void main(String args[]) {
    Person emps[] new Person("Debbie""Degree"),
        new Person("Geri""Grade")new Person("Ester""Extent"),
        new Person("Mary""Measure"),
        new Person("Anastasia""Amount") };
    Set set = new TreeSet(Arrays.asList(emps));
    System.out.println(set);
  }
}
           
        

Read more...

Your own auto-growth Array

public class Array implements java.util.Enumeration,java.io.Serializable
{
    private int current = 0;
    private int size = 10;
    private int grow = 2;
    private int place = 0;
    private Object[] elements = null;
    private Object[] tmpElements = null;

    public Array()
    {
        init();
    }

    public Array(int size)
    {
        setSize(size);
        init();
    }

    public Array(int size,int grow)
    {
        setSize(size);
        setGrow(grow);
        init();
    }

    private void init()
    {
        elements = new Object[size];
    }

    public Object nextElement() throws java.util.NoSuchElementException
    {
        if elements[place!= null && place != current)
        {
            place++;
            return elements[place - 1];
        }
        else
        {
            place = 0;
            throw new java.util.NoSuchElementException();
        }
    }

    public boolean hasMoreElements()
    {
        ifplace < elements.length && current != place )
            return true;
        return false;
    }

    public void setSize(int size)
    {
        this.size = size;
    }

    public int getCurrentSize()
    {
        return current;
    }

    public void rehash()
    {
        tmpElements = new Object[size];
        int count = 0;
        for int x = 0; x < elements.length; x++ )
        {
            ifelements[x!= null )
            {
                tmpElements[count= elements[x];
                count++;
            }
        }
        elements = (Object[])tmpElements.clone();
        tmpElements = null;
        current = count;
    }

    public void setGrow(int grow)
    {
        this.grow = grow;
    }

    public void grow()
    {
        size = size+=(size/grow);
        rehash();
    }

    public void add(Object o)
    {
        ifcurrent == elements.length )
            grow();

        try
        {
            elements[current= o;
            current++;
        }
        catch(java.lang.ArrayStoreException ase)
        {
        }
    }

    public void add(int location,Object o)
    {
        try
        {
            elements[location= o;
        }
        catch(java.lang.ArrayStoreException ase)
        {
        }
    }

    public void remove(int location)
    {
        elements[locationnull;
    }

    public int location(Object othrows NoSuchObjectException
    {
        int loc = -1;
        for int x = 0; x < elements.length; x++ )
        {
            if((elements[x!= null && elements[x== o )||
               (elements[x!= null && elements[x].equals(o)))
            {
                loc = x;
                break;
            }
        }
        ifloc == -)
            throw new NoSuchObjectException();
        return(loc);
    }

    public Object get(int location)
    {
        return elements[location];
    }

    public java.util.Enumeration elements()
    {
        return this;
    }
}
class NoSuchObjectException extends Exception
{

    public NoSuchObjectException()
    {
        super("No such object found.");
    }
}

Read more...

Copy Elements of ArrayList to Java Vector

import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");

    Vector<String> v = new Vector<String>();

    v.add("A");
    v.add("B");
    v.add("D");
    v.add("E");
    v.add("F");
    v.add("G");
    v.add("H");

    System.out.println(v);
    Collections.copy(v, arrayList);
    System.out.println(v);
  }
}


output:
[A, B, D, E, F, G, H]
[1, 2, 3, 4, 5, G, H]

Read more...

Get Enumeration over Java ArrayList

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("D");
    arrayList.add("E");
    arrayList.add("F");

    Enumeration e = Collections.enumeration(arrayList);

    while (e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}

Read more...

Get Enumeration over Java ArrayList

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("D");
    arrayList.add("E");
    arrayList.add("F");

    Enumeration e = Collections.enumeration(arrayList);

    while (e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP