Friday, 31 May 2013

Collections Example in JAVA

Please see the comments in below example to understand differences between hashset and treeset as well as hashmap and treemap. Just run this example ,you will get the differences easily.
================================================
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

public class CollectionExample {

@SuppressWarnings("unchecked")
public static void main(String a[]) {

// =============================================
/**
 * List allows Duplicate values
 */
List list = new ArrayList();
list.add("krishna");
list.add("krishna1");
list.add("krishna1");
list.add("krishna");
list.add(new String("anusha"));
list.add(new String("anusha"));
list.add(new Integer(8));
System.out.println(list.toArray().length);
;
System.out.println(list);

// =============================================

/**
 * Vector allows Duplicate values. It is legacy class(Synchronised)
 */
Vector vector = new Vector();
vector.addElement("krishna");
vector.addElement("krishna1");
vector.addElement("krishna11");
vector.addElement("krishna");
vector.addElement(new String("anusha"));
vector.addElement(new String("anusha"));
vector.addElement(new Integer(8));
Enumeration enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());

}

// ==============================================

/**
 * if key is different and content is same for three values then it will
 * print all values as shown below example HashMap allows null as key
 * values(Using EntrySet Method)
 */

Map map = new HashMap();
map.put("one", "krishna");
map.put("one1", "krishna");
map.put("two", "munna");
map.put("three", "balu");
map.put("four", "tiger");
map.put(null, null);
map.put(null, null);
map.put(null, "null key3");
Set set = map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry object = (Map.Entry) iterator.next();
System.out.println(object.getKey() + "*******" + object.getValue());

}

// ==============================================

/**
 * if key is different and content is same for three values then it will
 * print all values as shown below example HashMap allows null as key
 * values(Using KeySet Method)
 */

Map map2 = new HashMap();
map2.put("one", "krishna");
map2.put("one1", "krishna");
map2.put("two", "munna");
map2.put("three", "balu");
map2.put("four", "tiger");
map2.put(null, null);
map2.put(null, null);
map2.put(null, "null key3");
Set set2 = map2.keySet();
Iterator iterator2 = set2.iterator();
while (iterator2.hasNext()) {
Object object = (Object) iterator2.next();
System.out.println(object + "***#############****"
+ map2.get(object));

}

// =============================================
/**
 * if key is different and content is same for three values then it will
 * print all values as shown below example. HashMap allows null as key
 * values(Using KeySet Method).Hash Map supports different types of
 * objects like strings,integers as values and as well as keys.
 */

Map map42 = new HashMap();
map42.put("one", "krishna");
map42.put("one1", "krishna");
map42.put("two", "munna");
map42.put("three", "balu");
map42.put("four", "tiger");
map42.put(null, null);
map42.put(null, null);
map42.put(null, "null key3");
map42.put(new Integer(54), "null key3");// /integer as key
map42.put("integer", new Integer(88));// integer as value
Set set42 = map42.keySet();
Iterator iterator42 = set42.iterator();
while (iterator42.hasNext()) {
Object object = (Object) iterator42.next();
System.out.println(objec+ "****&&&&&&&&&&&&&&&&"+ map42.get(object));

}

// ==============================================

/**
 * if key is same and content is same for three values then it will
 * print last value like shown below TreeMap may supports null values
 * but key must be not null(using KeySet Method)
 */
Map map1 = new TreeMap();
map1.put("one", "krishna");
map1.put("one", "krishna1");
map1.put("two", "munna");
map1.put("three", "balu");
// map1.put(null, "anusha");
map1.put("four", "tiger");
Set set1 = map1.keySet();
Iterator iterator1 = set1.iterator();
while (iterator1.hasNext()) {
String object = (String) iterator1.next();
System.out.println(map1.get(object));

}

//==============================================

/**
 * if key is same and content is same for three values then it will
 * print last value like shown below TreeMap may supports null values
 * but key must be not null(Using EntrySet Method)
 */
Map map11 = new TreeMap();
map11.put("one", "krishna");
map11.put("one", "krishna1");
map11.put("two", "munna");
map11.put("three", "balu");
map11.put("four", "tiger");
Set set11 = map11.entrySet();
Iterator iterator11 = set11.iterator();
while (iterator11.hasNext()) {
Map.Entry object = (Map.Entry) iterator11.next();
System.out.println(object.getKey() + "}}}}}}}}}}}}}}}"
+ object.getValue());

}

// ==============================================
/**
 * if key is same and content is same for three values then it will
 * print last value like shown below TreeMap may supports null values
 * but key must be not null(Using EntrySet Method). Tree Map supports
 * different types of objects like strings,integers but key must be same
 * other wise exception will raise java.lang.ClassCastException:
 * java.lang.String cannot be cast to java.lang.Integer.
 */
Map map111 = new TreeMap();
map111.put("one", "krishna");
map111.put("one", "krishna1");
map111.put("two", "munna");
map111.put("three", "balu");
map111.put("four", new Integer(9));// integer as value
// map111.put(new Integer(88), new Integer(99));//integer as key not
// support in Tree Map
Set set1411 = map111.entrySet();
Iterator iterator4111 = set1411.iterator();
while (iterator4111.hasNext()) {
Map.Entry object = (Map.Entry) iterator4111.next();
System.out.println(object.getKey()+ "}}}}}}************}}}"+ object.getValue());

}

// ===============================================

/**
 * Tree set supports same types of objects. If we add String it will
 * throw exception java.lang.Integer cannot be cast to java.lang.String
 * Set support duplicate values while adding but it does not iterate
 * through duplicates.
 */

TreeSet set111 = new TreeSet();
set111.add(new Integer(1111));
set111.add(new Integer(155551));
set111.add(new Integer(111));
set111.add(new Integer(1111));// Duplicate value
// set111.add("krishna");
Iterator iterator22 = set111.iterator();
while (iterator22.hasNext()) {
System.out.println("%%%%%%%@@@@@@"+ iterator22.next());

}

// ==============================================

/**
 * Set does not allow duplicates but if we are trying to add it will
 * supports but it can not iterate through duplicates and also it check
 * the address not the content for duplication. In below example content
 * is same but are adding three different instances of class
 *
 */
Set set1111 = new HashSet();
  // We have BeanExample implementation below of this class.
BeanExample beanExample = new BeanExample();
beanExample.setUserName("krishna");
beanExample.setPassword("krishna");
set1111.add(beanExample);
BeanExample beanExample1 = new BeanExample();
beanExample1.setUserName("krishna");
beanExample1.setPassword("krishna");
set1111.add(beanExample1);
BeanExample beanExample11 = new BeanExample();
beanExample11.setUserName("krishna");
beanExample11.setPassword("krishna");
set1111.add(beanExample11);

System.out.println("@@@@@@@@@@@@@@@@@@@" + set1111.size());
Iterator iterator111 = set1111.iterator();
while (iterator111.hasNext()) {
// System.out.println(iterator.next());
BeanExample beanExample2 = (BeanExample) iterator111.next();
String userName = beanExample2.getUserName();
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$" + userName);
}

// ==============================================

/**
 * Tree set will iterate the values in ascending order. Tree set does
 * not supports different types of objects.
 */
Set set22 = new TreeSet();
set22.add("munna");
set22.add("krishna");
set22.add("anusha");
set22.add("kusuma");
Iterator iterator21 = set22.iterator();
while (iterator21.hasNext()) {
System.out.println(iterator21.next());
}

// ==============================================

/**
 * Hash set supports different types of objects. Set support duplicate
 * values while adding but it does not iterate through duplicates.
 */
HashSet stringSet = new HashSet();
String string = "krishna";
Integer integer = new Integer(8);
stringSet.add(integer);
stringSet.add(integer);
stringSet.add(new Integer(9));
System.out.println("###########################" + stringSet.size());
Iterator iterator1111 = stringSet.iterator();

while (iterator1111.hasNext()) {
System.out.println("%%%%%%%%%%%%%%%%%%%%" + iterator1111.next());
}
stringSet.add(string);
stringSet.add(string);
Iterator stringIterator = stringSet.iterator();
while (stringIterator.hasNext()) {
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
+ stringIterator.next());
}

// ==============================================
/**
 * Filtering the list based on data
 */
List<String> stringsList = new ArrayList<String>();
stringsList.add("BalaKrishna");
stringsList.add("Krishna");
stringsList.add("anusha");
stringsList.add("BalaKrishna");
stringsList.add("Krishna");
stringsList.add("anusha");
Iterator<String> stringIerator = stringsList.iterator();
String previousString = null;
String presentString = null;
String matchedString = null;
while (stringIerator.hasNext()) {
previousString = (String) stringIerator.next();
presentString = (String) stringIerator.next();
if (previousString.equalsIgnoreCase(presentString)) {
matchedString = presentString;
}
System.out.println("++++++++++++++++++++++++++++++"+matchedString);

}
// ==============================================
}
}


In order to run this(above) class we have to write below class(Dependency see colour).

//=================================================

package com.sample.java.testing;

public class BeanExample {

private String userName = null;
private String password = null;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
//======================================================

No comments:

Post a Comment