Tutorials

Java Collections Interview Questions


Java Collections Interview Questions 3


Is Map part of collection interface hierarchy?

No, Map is not a part of collections hierarchy.

If you closely look at the collection hierarchy tree, you can see that Map is not included in it. Refer the below diagram.

collection hierarchy

 

 

Here List, Set and Queue interfaces all extends Collection interface. 

But Map interface doesn't extend Collection interface. So it is not a part of Collection interface hierarchy.


Which are the synchronized classes in collection?

Following are the synchronized classes in java:

  • Vector (obsolete),
  • HashTable,
  • BlockingQueue,
  • ConcurrentMap

How to make unsynchronized List, Set or Map synchronized?

To synchronize a 'list' write,

Collections.SynchronizedList(list)

 

To synchronize a 'set' write,

Collections.SynchronizedSet(set)

 

To synchronize a 'map' write,

Collections.SynchronizedMap(map)


What is iterator?

Iterator is an interface in java.

Iterator is used in java to traverse over collection elements. Using iterator you can traverse collection elements in only forward direction.

 

Given below is an example that illustrates use of iterator:

package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class ItrTraversal {

	public static void main(String[] args) {
		//create Array List
		ArrayList<String> javaRadarList = new ArrayList<String>();
		//Add elements to ArrayList
		javaRadarList.add("Java");
		javaRadarList.add("Java");
		javaRadarList.add("Spring");
		javaRadarList.add("Hibernate");
		javaRadarList.add("EJB");

		//creating iterator to traverse over javaRadarList
		Iterator<String> listIterator = javaRadarList.iterator();

		while(listIterator.hasNext()){
			System.out.println(listIterator.next());
		}
	}

}

 

 


 

How can you traverse collection list in both forward and backward directions?

We can traverse collection list in both forward and backward directions using ListIterator.

ListIterator has two methods:

  1. hasNext() {iterator also has this method, see the exampe in Q-4 of this page}
  2. hasPrevious()

Using this two it can navigate in both the direction. 

But remember list iterator can only be used with list elements.

 



Please Share this page
Views : 55
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!