Showing posts with label Collections. Show all posts
Showing posts with label Collections. Show all posts

LinkedHashMap

LinkedHashMap is under Java Collection API and it implements Map interface. LinkedHashMap also works as same as HashMap with only 1 difference. Lets see the properties of LinkedHashMap class in Java

  • It works based on key and value as same as HashMap.
  • It implements Map interface and extends HashMap class, so that it acquires all properties of HashMap class. 
  • It can hold 1 null key and multiple null values. 
  • Only difference between HashMap and LinkedHashMap is interstion order. LinkedHashMap maintains the insertion order where as in HashMap its not. 

Now lets see simple example for LinkedHashMap and how to use it and also lets test the insertion order in both collection classes.


import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class LinkedHashMapTest {

 public static void main(String[] args) {
  
  Map<String, String> hm = new HashMap<String, String>();
  Map<String, String> lhm = new LinkedHashMap<String, String>();
  
  hm.put("1", "one"); 
  hm.put("2", "two");
  hm.put("3", "three");
  hm.put("4", "four");
  
  lhm.put("1", "one"); 
  lhm.put("2", "two");
  lhm.put("3", "three");
  lhm.put("4", "four");
  
  // Printing HashMap values.
  System.out.println("HasnMap Key and Value :::::::::::::::");
  Set<Entry<String, String>> set = hm.entrySet();
  Iterator<Entry<String, String>> itr = set.iterator();
  while(itr.hasNext()){
   Map.Entry<String, String> map = itr.next();
   System.out.println(map.getKey() +" : "+map.getValue());
  }
  
  // Printing LinkedHashMap values.
  System.out.println("HasnMap Key and Value :::::::::::::::");
  Set<Entry<String, String>> set1 = lhm.entrySet();
  Iterator<Entry<String, String>> itr1 = set1.iterator();
  while(itr1.hasNext()){
   Map.Entry<String, String> map1 = itr1.next();
   System.out.println(map1.getKey() +" : "+map1.getValue());
  }
 }
}


OUTPUT:


HasnMap Key and Value :::::::::::::::
3 : three
2 : two
1 : one
4 : four

HasnMap Key and Value :::::::::::::::
1 : one
2 : two
3 : three
4 : four


In above program we have added 4 values in HashMap and LinkedHashMap. Next when we print those key and value we can see the difference between both like HashMap insertion order not maintained while printing. Where as in LinkedHashMap its same as insertion order. 

How to convert list to read-only list in Java?

 
By using Collections class we can convert List to read-only List. This can done by using Collections.unmodifiableList(list) function from Collections class. By this we can't add or remove any element from the List. Not only List we can make other Collection Objects like Map, Set also to read-only as like below.

Collections.unmodifiableMap(map);
Collections.unmodifiableSet(set);
Collections.unmodifiableSortedMap(sortedMap);
Collections.unmodifiableSortedSet(sortedSet);

Lets see small example how to convert list to read-only list.


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReadOnlyList {

 public static void main(String[] args) {
  
  List<String> list = new ArrayList<String>();
  list.add("java");
  list.add("discover");
  list.add("threads");
  list.add("oops");
  list.add("servlet");
  
  System.out.println("LIST : "+list);
  
  // Removing "oops" from from list
  list.remove(3);
  
  System.out.println("LIST : "+list);
    
  // Making list to read-only
  list = Collections.unmodifiableList(list);

  // trying to removing "threds" from list
  list.remove(2);
    
  System.out.println("LIST : "+list);
 }
}


OUTPUT:


LIST : [java, discover, threads, oops, servlet]
LIST : [java, discover, threads, servlet]
Exception in thread "main" java.lang.UnsupportedOperationException


If we seen in above program we have added 5 values in list and we have removed 1 value "oops" from the list. Next we have converted list to read-only list by using unmodifiableList() method. Later when we try to remove "discover" value it throws UnsupportedOperationException because of read-only list. Even we can't add any value to this list. 




Vector and ArrayList

 
In this tutorial we will see about Vector and ArrayList in java. Both Vector and ArrayList shares common data structure "Array" internally.

Synchronized and non-synchronized
By default Vector is Synchronized and ArrayList is not Synchronized. By this for Thread safe we can use Vector and for non-thread safe we can use ArrayList. Even ArrayList can be converted to synchronized by Collections class using "Collections.synchronizedList(list)"


Vector in Collection API
Vector defined in first JDK version and later from Java 2 platform Vector got added with Java Collection API. Vector has been retrofitted to implement List Interface.
Once Vector added in Collection API, older methods in Vector class got updated to implement List Interface. Below are the methods got updated from Older version to new methods.


Old Vector MethodsNew Vector Methods
void addElement(Object)boolean add(Object)
boolean removeElement(Object)boolean remove(Object)
void removeElementAt(int)void remove(int)
void insertElementAt(Object, int)void add(index, Object)
Object elementAt(int)Object get(int)
Enumeration elements()Iterator iterator()
ListIterator listIterator()
void copyInto(Object[])Object[] toArray()
void removeAllElements()void clear()
void setElementAt(int)Object set(int, Object)


Where to use Vector and ArrayList
By performance wise ArrayList will be better in case of non synchronized operations and when we need for thread-safe operation then we need to opt for Vector.

Size
Both Vector and ArrayList will re-size dynamically. Vector will double the size of its array automatically when its full, where as ArrayList will increase by half of its array size.









TreeMap using custom object sorting

 
We know that by default TreeMap will sort the values using key. Suppose if we need to sort the TreeMap using object stored in key part then we need to implement the Comparator interface and we need to @Override compare() method which will sort 2 Objects of key path and will give us the sorted output. 

Below single example will show you how to use custom Object sorting in TreeMap. TreeMap will take "Worker class instance" as key and "String name" as value. Where we need to sort the values using key based on the member variables in the Worker class. 

Class "MyNameComp" which implements the Comparator interface on "Worker" class and used to sort TreeMap based on name or salary. Below example will gives you sort on salary. Suppose if we need output based on name sorted then we need to un-comment "return obj1.getName().compareTo(obj2.getName());"



public class TreeMapUsingObjectSorting {
 
 public static void main(String a[]){
  TreeMap<Worker,String> map = new TreeMap<Worker, String>(new MyNameComp());
  map.put(new Worker("david",5000), "david");
  map.put(new Worker("joy",2000), "joy");
  map.put(new Worker("abel",7000), "abel");
  map.put(new Worker("ruby",9000), "ruby");
  
  for (Map.Entry<Worker, String> entry : map.entrySet()) {
   System.out.println("KEY : "+ entry.getKey() +" \t VALUE : "+entry.getValue());
  }
 }
}




public class Worker{
    
    private String name;
    private int salary;
    
    public Worker(String name, int salary){
        this.name = name;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    /* Called by entry.getKey() 
       Overriding toString() method from super class Object
       Since key is Object we are return our own key value
    */
    public String toString(){
     //return super.toString();
     return "("+this.name+":"+this.salary+")";
    }
}




public class MyNameComp implements Comparator<Worker>{

 @Override
 public int compare(Worker obj1, Worker obj2) {
        
  // Sort TreeMap based on name
  //return obj1.getName().compareTo(obj2.getName());
  
  // Sort TreeMap based on salary
  if(obj1.getSalary() > obj2.getSalary()) return 1;
  else if(obj1.getSalary() < obj2.getSalary()) return -1;
  else return 0;
    } 
}

OUTPUT:


KEY : (joy:2000)   VALUE : joy
KEY : (david:5000)   VALUE : david
KEY : (abel:7000)   VALUE : abel
KEY : (ruby:9000)   VALUE : ruby









Difference between Iterator and Enumeration in Java?

In our last tutorial we have seen difference between Iterator and ListIterator. In this tutorial we will see about difference between Iterator and Enumeration in Java. Both Iterator and Enumeration are used to traverse elements in the Collection Object. Differences are,
  • Enumeration are introduced in older Java version and Iterator introduced in later version with additional features.
  • By Enumeration allows only traverse through elements, where as by Iterator we can traverse and also we can remove elements from the Collection Object.
  • Iterator is more secure and safe compared to Enumeration since its thread safe and won't allow others threads to modify the elements in Collection Object while some Thread is Iterating. On that case it will give ConcurrentModificationException

Finally we can decide if we are going for Read-only then we can for Enumeration else we can go for Iterator.

Mainly there are two types of Iterator in Java and they fail-fast and fail-safe Iterators. fail-fast iterators are those who throw ConcurrentModificationException if collection Object is modified when other Thread iterating on same Object. Fail-safe Iterator works on copying collection Object and won't work on original Object and its safe compared to fail-fast.    

Lets see example for both Iterator and Enumeration in Java



public class IteratorEnumerationTest {
 
 public static void main(String[] args) {
  
  ArrayList<String> myList = new ArrayList<String>();
  
  myList.add("one");
  myList.add("two");
  myList.add("three");
  myList.add("four");
  myList.add("five");
  
  Iterator<String> itr = myList.iterator();
  System.out.println("THROUGH ITERATOR : ");
  while(itr.hasNext()){
   System.out.print(itr.next()+", ");   
  }
  
  Vector<String> vec = new Vector<String>(myList);

  Enumeration<String> enu = vec.elements();
  
  System.out.println("\n\nTHROUGH ENUMERATION : ");
  while(enu.hasMoreElements()){
   System.out.print(enu.nextElement()+", ");
  }
 } 
}



OUTPUT:


THROUGH ITERATOR : 
one, two, three, four, five, 

THROUGH ENUMERATION : 
one, two, three, four, five, 










Difference between Iterator and ListIterator in Java?

In this tutorial we will see about difference between java.util.Iterator and java.util.ListIterator interfaces. Both interface are used to traverse values from the collection Object. But the differences are 

  • Iterator can traverse only in forward direction, where as ListIterator can traverse in both (bidirectional). 
  • Iterator can be used to traverse values in List and Set collections, where as ListIterator is used to traverse only in List.
  • ListIterator derived from Iterator interface and contains functions like remove(), previous(), next(), nextIndex(), previousIndex() etc.,


Lets see examples for both Iterator and ListIterator as how its used on Collection Object. 

Iterator Example:


public class IteratorTest {
 
 public static void main(String[] args) {
  List<String> myList = new ArrayList<String>();
  myList.add("java");
  myList.add("collections");
  myList.add("API");
  myList.add("important");
  myList.add("interview");
  myList.add("questions");
  
  System.out.println("LIST BEFORE REMOVE : "+myList);
  
  Iterator<String> itr = myList.iterator();
  while(itr.hasNext()){
   String val = itr.next();
   if(val.equals("API")){
    // Removing "API" value from ArrayList
    itr.remove();
   }   
  }  
  System.out.println("LIST AFTER REMOVE : "+myList);
 } 
}

OUTPUT:


LIST BEFORE REMOVE : [java, collections, API, important, interview, questions]
LIST AFTER REMOVE : [java, collections, important, interview, questions]


ListIterator Example:


public class ListIteratorTest {
 
 public static void main(String[] args) {
  List<String> myList = new ArrayList<String>();
  myList.add("java");
  myList.add("collections");
  myList.add("API");
  myList.add("important");
  myList.add("interview");
  myList.add("questions");
  
  System.out.println("LIST : "+myList);
  
  ListIterator<String> itr = myList.listIterator();
  while(itr.hasNext()){
   String val=itr.next();   
   if(val.equals("API")){
    // moving to previous element
    itr.previous();
    System.out.println("PREVIOUS VALUE : "+myList.get(itr.previousIndex()));
    // moving to forward element
    itr.next();
    System.out.println("NEXT VALUE     : "+myList.get(itr.nextIndex()));
   }
  }
 } 
}

OUTPUT:


LIST : [java, collections, API, important, interview, questions]
PREVIOUS VALUE : collections
NEXT VALUE     : important