- Which is root class/interface in Collection Hierarchy?
Collection interface is root interface in hierarchy.
- Which are collection implementation classes you have worked on?
- What is Time Complexity to search an element in ArrayList?
To search an element in ArrayList is O(1) as it internally uses array to store element.
- What is Time Complexity to add an element in ArrayList?
At end – O(1)
At beginning – O(n) – As lots of shift operation need to do
- Difference between Iterator and ListIterator
Iterator is used for traversal in forward direction where ListIterator provides traversal in both direction.
- Difference between ArrayList and LinkedList
ArrayList is based on array data structure whereas LinkedList uses doubly linked list data structure. ArrayList is best suited for searching whereas LinkedList can be used for modifying the list. Time complexity to search an element in ArrayList is O(1) and in LinkedList it is O(n).
- Provide Implementation of LinkedList data structure
By this question they want to know you data structure skill. You can implement LinkedList class with some operations like add,remove and traverse etc.
- Describe the best suited situation for choosing a Collection implementation?
Best way to choose a collection
- What are legacy collection classes/interfaces?
Class – Dictionary,HashTable,Vector,Properties,Stack,Vector. Interface – Enumeration
- Internal working of HashMap
HashMap works on hashing mechanism where it calculates hashcode for each element and find the bucket(array) index for storing the element. The put(key,value) method is used for storing an element in map. In this method it invokes hashcode() method on key and find the bucket for storing the provided value. If it finds the bucket having an element then it invokes equals() method to check the identity of element and if both the element are different it store the given element next to the other element in linked list data structure. From Java 8 after certain threshold it convert into Tree. Same goes for get(key), to find the element in Map. It first calculate the hashcode for the given key and find the bucket index if it finds there are more than one element and it invokes equal method to verify the identity. So everytime you use a hash collection implement both the hashcode() and equals() methods.
- What does hashcode() method do?
The hashCode() method is used with Hash based collections HashMap, HashTable, HashSet to find bucket number which is the address of the element inside the set/map.When you use contains() method it will take the hash code of the element, then look for the bucket where hash code points to. If more than 1 element is found in the same bucket (multiple objects can have the same hash code), then it uses the equals() method to evaluate if the objects are equal, and then decide if contains() is true or false, or decide if element could be added in the set or not.
- What does equals() method do?
Object class provides the implementation of equals() method where it compares the references of the objects. If we want different implementation of this method then we need to provide in our class.
- What happens if hashcode() method is overidden and don’t override equals() method?
When you use contains() method it will take the hash code of the element, then look for the bucket where hash code points to. If more than 1 element is found in the same bucket (multiple objects can have the same hash code), then it uses the equals() method to evaluate if the objects are equal, and then decide if contains() is true or false, or decide if element could be added in the set or not.
- What happens if hashcode() method return constant value and equlas() method returns false?
As we know hashcode() method is used to find the bucket number if same bucket is returned to objects then it checks the objects equality by invoking equals() method if overridden. If constant value is returned from hashcode() and equals() return false then each element will be stored to hash based collection. Read More
- What happens if hashcode() method random value and equals() method returns false?
As hashcode() method first checked and if different then equals() method never invoked and object will in different bucket.
- Difference between ArrayList and Array
Array is static data structure whereas ArrayList is growable array. Incase of array we cannot add element beyond the capacity.
- Difference between ArrayList and Vector
ArrayList is non synchronized whereas Vector is synchronized data structure. Means we cannot use ArrayList is in case of multi-threaded environment. Both ArrayList and Vector internally use array for storing elements,each time a new element is added beyond the capacity a new array created. ArrayList grows by 50% where as Vector by 100% .
- How to sort ArrayList elements?
For sorting ArrayList elements by implementing Comparable & Comparator interfaces. If we store our own class elements then we have to implement comparable interface and implement compareTo() method.
ArrayList<String> list = new ArrayList<>(); list.add("Gaurav"); list.add("Saurav") Collections.sort(list);
- How to sort HashMap<Customer,Order> by customerId field?
We can use TreeMap to sorting the elements. For this we have to implement Comparable interface in Customer class and provide the implementation for compareTo() method.
- How HashSet works internally?
HashSet is collection for storing unique elements. It uses HashMap to store its elements. All the values stored as key and key can’t be duplicate. You can add one null key and as many null values.
- Difference between LinkedHashSet and HashSet?
Both implements Set interface. LinkedHashSet maintains the insertion order whereas HashSet does not maintain. LinkedHashSet internally uses doubly linkedlist for storing its elements whereas HashSet uses HashMap to store its elements.
- What is Stream API?
Stream API is collection of interfaces and classes which was added to Java 8 release. It facilitates to works with Collection API. It does not change the source data structure. Stream provides two kind of operations – Intermediate and Terminal. All Intermediate operations (filter,map) are lazy means nothing happen until a terminal operation performed. Terminal Operation is used to find the result from Stream it could be a collection or a single value.
- Find list of employee whose salary greater than 10,000 from List<Employee> using Stream API.
List<Employee> employees = new ArrayList<>(); Employee e1 = new Employee(1,"Name",10000); employees.add(e1); List<Employee> filteredEmployees = employees.stream() .filter(emp->emp.getSalary()>10000) .collect(Collector.toList());
- Find all distinct employee from List<Employee> in ascending order
Stream API provides distinct() method for finding unique elements. For this we need to provide implementation of hashcode() and equals() methods. Find Unique elements from ArrayList using Stream API
- What are functional interfaces used in Stream API?Predicate,BiPredicate,Function,BiFunction,Consumer,BiConsumer,Supplier etc mostly used in Stream API operations.
Stream API Examples