Example & Tutorial understanding programming in easy ways.

Collection framework in java interview questions

Common collection activities include adding objects, removing objects, retrieving objects, and iterating.

Three meanings for "collection":

: epresents the data structure in which objects are stored.
Collection : java.util interface from which Set and List extend.
Collections: A class that holds static collection utility methods.

Few basic operations that are normally used with the collections are:

Add objects to the collection.
Remove objects from the collection.
Find out if an object is in the collection.
Retrieve an object from the collection.
Iterate through the collection.

Four basic types of collections include Lists, Sets, Maps, Queues:

Lists : Ordered, duplicates allowed, have an index.
Sets :  May or may not be ordered and/or sorted; duplicates not allowed.
Maps: keys May or may not be ordered and/or sorted; duplicate keys are not allowed.
Queues :To process Ordered by FIFO or by priority.

List Interface: A List have  index position fore objects.

ArrayList: It is an ordered collection by index but not sorted. It gives you fast iteration and fast random access.
Vector: A Vector is basically an ArrayList, but Vector methods are for thread safety. We normally want to use ArrayList instead of Vector, as the synchronized methods add a performance overhead .

LinkedList: A LinkedList is ordered position, like ArrayList,except the elements are doubly-linked to one another.
A LinkedList may iterate more slowly than an ArrayList, but it's a good choice when you need fast insertion and deletion. After Java 5 LinkedList class has been enhanced to implement the java.util.Queue interface. It now supports the common queue methods: peek(), poll(), and offer().

Set Interface: A Set cares about uniqueness.It does not allow duplicates.

HashSet: It is an unsorted, unordered Set. It uses the hashcode of the object being inserted.
LinkedHashSet: A LinkedHastSet is an ordered version of HashSet that maintains a doubly-linked  List across all elements.
Use this class instead of HashSet when we need care about the iteration order.
TreeSet: The TreeSet is one of the two sorted collections. It guarantees that all the elements will be in ascending order, according to natural order.

Note:- When using HashSet or LinkedHashSet, the objects we add to them must override hashCode(). If they don’t override hashCode(), the default Object hashCode() method will allow multiple objects that might consider "meaningfully equal" to be added to "no duplicates allowed" set.

Map Interface: A Map cares about unique identifiers

HashMap: It is an unsorted, unordered Map.
LinkedHashMap: Like its Set Counterpart, LinkedHashSet, the LinkedHashMap maintains insertion order
TreeMap: The TreeMap is a Sorted map and it is sorted by natural order.

Queue Interface: This class is new with Java 5.
Priority Queue:
The purpose of a PriorityQueue is to create a "priority-in, priority-out“.
Queue as opposed to a typical FIFO queue.

Hashtable: Hashtable counterpart of HashMap

ArrayList: Fast iteration and fast random access.

Vector: It's like a slower ArrayList, but it has synchronized methods.

LinkedList: Good for adding elements

HashSet: Fast access, assures no duplicates, provides no ordering.

LinkedHashSet: No duplicates; iterates by insertion order.

TreeSet: No duplicates; iterates in sorted order.

HashMap: Fastest updates (key/value pairs); allows one null key, many null values.

Hashtable: Like a slower HashMap (as with Vector, due to its synchronized methods). No null values or null keys allowed.

LinkedHashMap: Faster iterations; iterates by insertion order or last accessed; allows one null key, many null values.

TreeMap: A sorted map.

PriorityQueue: A to-do list ordered by the elements' priority.

Collections hold only Objects, but primitives can be autoboxed

Iterate with the enhanced for, or with an Iterator via hasNext() & next().

hasNext() determines if more elements exist; the Iterator does NOT move.
next() returns the next element and moves the Iterator forward.

To work correctly, a Map's keys must override equals() and hashCode().

Queues use offer() to add an element, poll() to remove the head of the queue, and peek() to look at the head of a queue.

Sorting can be in natural order, or via a Comparable or many Comparators.
Implement Comparable using compareTo(); provides only one sort order.


Create many Comparators to sort a class many ways; implement compare().
To be sorted and searched, a List's elements must be comparable.
To be searched, an array or List must first be sorted.

A sort() method. Sort using a Comparator or sort using natural order.

A binarySearch() method. Search a pre-sorted array or List.

Arrays.asList() creates a List from an array and links them together.

Collections.reverse() reverses the order of elements in a List.

Collections.reverseOrder() returns a Comparator that sorts in reverse.

Lists and Sets have a toArray() method to create arrays.

What are Generics?
Generics let you enforce compile-time type safety on Collections
Before Java 5 , no syntax for declaring a type Safe Collection.
After Java 5, the above code written as
List myList=new ArrayList

Generics take care of both ends(the putting in and getting out)
List myList=new ArrayList();
myList.add(“Fred”); // OK, it will hold Strings
myList.add(new Circle()); // compiler error
String s = myList.get(0);
pre-generics, when a String wasn't guarantee
String s = (String)myList.get(0);

Comparable Interface
Used by the Collections.sort() method and the java.utils.Arrays.sort() method to sort Lists and arrays of objects, respectively.
One sort sequence is allowed.
To implement Comparable, a class must implement a single method,compareTo().

Comparator Interface

Read More →
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!