Skip to main content

Posts

Showing posts with the label Collections

Tricky Interview Questions in Java Collections

What is difference between ArrayList and vector? Ans) 1) Synchronization - ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(int i) is surrounded with a synchronized block and thus making Vector class thread-safe. 2) Data growth - Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. How can Arraylist be synchronized without using Vector? Ans) Arraylist can be synchronized using: • Collection.synchronizedList(List list) Other collections can be synchronized: • Collection.synchronizedMap(Map map) • Collection.synchronizedCollection(Collection c)

Java Collections Interview Questions -Latest

For any java professional Collections Framework is important subject to be expert in it. Since these concepts are used all most all business logics. Wherver the Java interview happens, interviewer expects you should be 40% of technical skills from Collections framework. So you should be perfect in In this subject. I have collected some of the very important and gunshot questions that have been asked most of the interviews in all major companies. I discussed with many guys who has gone for java interviews and collected these Java Collections Framework Questions & Answers. Just check them out.

How to loop through a Map?

-- Using Map in java is a convenient way to store objects into a collection and identified by their names.  The Map interface provides API of adding and getting a particular object by the name it’s stored with.  However, there is no simple API to loop through all the items in the Map.  An older fashion to do this is to use the java.util.Iterator, for example: public void test () {         Map testMap = new HashMap();         testMap.put("key1", "value1");         testMap.put("key2", "value2");         Iterator it = testMap.keySet() .iterator () ;          while (  it.hasNext ()   )   {              String key =  (String) it.next () ;          ...

What is Difference between int.class and Integer.TYPE?

Class literals have been around since Java 1.1. Everyone knows you can use Integer.class to get the Class object for java.lang.Integer . Under the covers the compiler caches a results of Class.forName("java.lang.Integer") in your class. The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int . There is no different between int.class and Integer.TYPE . The Integer.TYPE and int.class point to the same class object. The Integer.TYPE is the Class instance representing the primitive type int . public class Program { public static void main(String[] args) { System.out.println(int.class == Integer.TYPE); } } The output is "true". Class literals are a way to access the instances of the class Class. A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a `.' and the ...

Why doesn't Iterator work for my collection?

Let's take a look at the following code: class Program { public static void main(String args[]) { ArrayList alist = new ArrayList (); alist.add(new String("A")); alist.add(new String("B")); alist.add(new String("C")); int i = 0; for (Iterator it = alist.iterator(); it.hasNext(); ) { System.out.println(alist.get(i++)); } } } A runtime exception java.lang.IndexOutOfBoundsException is thrown when it goes beyond the end. What is wrong? The code combines the iterator and index. After hasNext() returns true, the only way to advance the iterator is to call next() . But the element is retrieved with get(index) , so the iterator is never advanced. In the above example, the hasNext() will always be true, and eventually the index i for get(index) will beyond the end of ArrayList .

What is the advantage of using an Iterator compared to the get(index) method?

You can navigate or access a List by using the get(index) method or an Iterator. Sometimes the get(index) method is your only option, and sometimes it's slightly faster than an Iterator. Other times, however, it can be much, much slower than an Iterator. For example, a LinkedList is a classic example. This class has a get(index) method but it is very slow. Well, it's not that bad if the list is short, or if you're looking for an item that is close to the beginning or end. But if you need to access the List frequently, you will see a big difference. Let's take a look at the following example: public class TestClass { public static void main(String[] args) { int len = 100000; LinkedList linkedLst = new LinkedList(); ArrayList arrayLst = new ArrayList(); for (int m =0; m!= len; m++) { int x = (int)Math.random(); linkedLst.add(x); arrayLst.add(x); }

Java Collections

What is HashMap and Map? Map is Interface and Hashmap is class that implements this interface. What is the significance of ListIterator? Or What is the difference b/w Iterator and ListIterator? Iterator : Enables you to cycle through a collection in the forward direction only, for obtaining or removing elements ListIterator : It extends Iterator, allow bidirectional traversal of list and the modification of elements Difference between HashMap and HashTable? Can we make hashmap synchronized? 1. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls). 2. HashMap does not guarantee that the order of the map will remain constant over time. 3. HashMap is non synchronized whereas Hashtable is synchronized. 4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. Note on Some Important Terms 1)Syn...