InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How is WeakHashMap differs from other HashMap? |
|
Answer» WeakHashMap is using the WeakReference class, first, we need to understand how WeakReference works, WeakReference is a class which HOLDS the reference and GC captured that reference eagerly and does the garbage collection. Below is the code snippet for WeakReference. Integer prime = 1; WeakReference<Integer> soft = new WeakReference<Integer>(prime); prime = NULL;In this case, prime no gets garbage collected easily. The WeakHashMap has utilized the WeakReference CLASSES, below code structure of WeakHashMap shows about the utilizing of WeakReference. The WeakHashMap extends the WeakReference classes private static class ENTRY<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { V value; final int hash; Entry<K,V> next; /** * Creates new entry. */ Entry(Object key, V value, ReferenceQueue<Object> queue, int hash, Entry<K,V> next) { super(key, queue); this.value = value; this.hash = hash; this.next = next; } @SuppressWarnings("unchecked") public K getKey() { return (K) WeakHashMap.unmaskNull(get()); }Key is stored as a WeakReference in ReferenceQueue, whenever the JVM get the key its add in the ReferenceQueue, so as soon as WeakHashMap check any key and if it found is deletes it from the table, this operation is called every time before any get operation. Predominantly the WeakHashMap is given better memory performance, one can use this whenever we have to implement the custom cache or need to add a volume of the record in the Map. |
|
| 2. |
How the ConcurrentHashMap works, how it provides the thread safety? |
|
Answer» The concurrentHashMap works on the principle of segmentation, which divides the HashMap into segments and each SEGMENT allocated to each THREAD and LOCKED for same so other thread will not interfere with each other, the default size of segments in each ConcurrentHashMap is 32. Below is the internal code structure of ConcurrentHashMap. This below code works as an internal class of ConcurrentHashMap. PROTECTED static final class Segment { protected int count; // The count is a COUNTER which update is synchronized protected synchronized int getCount() { return this.count; } protected synchronized void synch() {} } /** Segment Array declaration **/ public final Segment[] segments = new Segment[32]; |
|
| 3. |
What is CopyOnWriteArrayList? How it is different from ArrayList in Java? |
|
Answer» CopyOnWriteArrayList CREATES a clone of the actual list, for EVERY successive update operation will update internally to the actual list without generating the concurrent modification EXCEPTION, and prevent to throw the concurrent modification exception. HOWEVER, to use this cost more on memory consumption because for every update operation there is one clone copy gets created, we have to use wisely when there is frequent read operation, it is better to use when the same list is shared by a number of thread, whereas ArrayList doesn’t allow any thread safety. However whenever any modification takes place while traversing in the ARRAY lIst its throws the concurrent modification exception. |
|
| 4. |
When to use the Queue and Stack? What is the possible use case? |
|
Answer» Stack and Queue work on the principal of First in Last Out and First in First out. The best use case of Queue is in the MESSAGING SERVICES, where we implement the messaging container, which allows one message to get input and another message get output from the queue. The queue has the ability to also act as a buffer to store the elements for the temporary time. Whereas Stack best use is to perform prefix, POSTFIX and infix operation in the Tree. Below is the code example of Stack: import java.util.Stack; public class StackExample { public static void main(String a[]){ Stack<Integer> stack = new Stack<>(); System.out.println("Empty stack : " + stack); // Checking whether the stack is empty? System.out.println("Empty stack : " + stack.isEmpty()); // Exception in THREAD "main" java.util.EmptyStackException // Adding the elements in the queue stack.push(1001); stack.push(1002); stack.push(1003); stack.push(1004); System.out.println("Non-Empty stack : " + stack); // Getting the elements from the queue System.out.println("Non-Empty stack: Pop Operation : " + stack.pop()); System.out.println("Non-Empty stack : After Pop Operation : " + stack); // Checking whether the elements in the queue. System.out.println("Non-Empty stack : search() Operation : " + stack.search(1002)); System.out.println("Non-Empty stack : " + stack.isEmpty()); } }Below is the code example of Queue: import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<>(); // Adds elements {0, 1, 2, 3, 4} to queue for (int i=0; i<5; i++) queue.add(i); // DISPLAY contents of the queue. System.out.println("Elements of queue-"+q); // To remove the head of queue. int removedele = queue.remove(); System.out.println("removed element-" + removedele); System.out.println(queue); // To view the head of queue int head = queue.peek(); System.out.println("head of queue-" + head); // Rest all methods of collection interface, // Like size and contains can be used with this // implementation. int size = queue.size(); System.out.println("Size of queue-" + size); } } |
|
| 5. |
When to use the Iterator and Enumeration? What is the possible use case? |
|
Answer» Iterator and Enumerator are two different classes in the collection api, which is used to perform the traversing to a collection object. The use case is whenever there is a NEED to traverse the elements and there is no need to add and update the elements in the list than its good to go with the Enumerator, an enumerator is basically a legacy class which is a SUPPORT to all the legacy classes Vector, Stack, HashTable. Whereas Iterator is an advanced way to handle the traversing when there is a requirement to modify the elements in the collection object than we can use the Iterator. Iterator can support all kinds of Collection class like List, Map and Set. Iterator can provide fail-safe and fall fast according to the corresponding collection class. Below is the code snippet for Enumeration import java.util.*; public class EnumerationIteration { public static void MAIN(String args[]) { Vector Barista = new Vector(); Barista.add("Chipotele"); Barista.add("Starbucks"); Barista.add("Dunkin Donuts"); // Creating the enumeration from Vector Enumeration enumerate = Barista.elements(); // Traversing to the elements using hasMoreElements() method. while(enumerate.hasMoreElements()) { // System.out.println(enumerate.nextElement()); } } } // Below is the code snippet for Iterator import java.util.*; public class EnumerationIteration { public static void main(String args[]) { Vector foodChain = new Vector(); foodChain.add("Mc Donalds"); foodChain.add("Subway"); foodChain.add("Chipotele"); System.out.println("USA Food SHOPS:"); Iterator it = foodChain.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } } |
|
| 6. |
How can you use Comparable and Comparator interfaces to sort collections? |
|
Answer» Comparable is part of java.lang package, which is used to compare another object from itself, the way it works that the class needs to implement the comparable interface and need to override the compare method which takes another object as a parameter, this scenario is when we use it to implement natural comparison with another object, it could be any user-defined class.The return type of compareTo() method is int type which could return three different values (-1, 0, 1) Below is the code snippet for same. /* This class implements the Comparable interface which uses as a comparison between two objects. * Class has override the compareTo() method */ class StudentComparableDemo implements Comparable<StudentComparableDemo>{ int rollno; String name; int age; StudentComparableDemo(int rollno,String name,int age){ this.rollno=rollno; this.name=name; this.age=age; } //This method performs the comparison of object of Student using the attribute the age which COMPARES with the age of other student object. public int compareTo(StudentComparableDemo ST){ if(age==st.age) // If age of both the object is same it will return 0 return 0; else if(age>st.age) return 1; else return -1; } }The comparator is an interface of java.util package which predominantly works on collection library, the best use case is to compare the two different collection object. The sorting can perform using Collections utility class. Below is the code representation. import java.util.*; import java.lang.*; import java.io.*; // This class to represent an Employee domain object which eventually works store the employee domain information. class Employee { int empid; String name, address; // CONSTRUCTOR public Employee(int empid, String name, String address) { this.empid = empid; this.name = name; this.address = address; } // Used to print Employee details in main() public String toString() { return this.empid + " " + this.name + " " + this.address; } } /* This classes responsible for performing the compare and sorting the employee result according to the empid */ class SortbyId implements CompemployeeListator<Employee> { // Used for sorting in ascending order of // roll number public int compemployeeListe(Employee a, Employee b) { return a.empid - b.empid; } } /* This classes responsible to performs the compare and sorting the employee result according to the empid */ class Sortbyname implements CompemployeeListator<Employee> { // Used for sorting in ascending order of // roll name public int compemployeeListe(Employee a, Employee b) { return a.name.compemployeeListeTo(b.name); } } // The Driver class perform the operation on sorting the employee object class Main { public static void main (String[] employeeListgs) { employeeListrayList<Employee> employeeList = new employeeListrayList<Employee>(); employeeList.add(new Employee(111, "bbbb", "LONDON")); employeeList.add(new Employee(131, "aaaa", "nyc")); employeeList.add(new Employee(121, "cccc", "jaipur")); System.out.println("Unsorted"); for (int i=0; i<employeeList.size(); i++) System.out.println(employeeList.get(i));Collections are the utility class which has the sort method takes an input of list of the employee and implemented comparator class use to perform the sorting. Collections.sort(employeeList, new SortbyId()); System.out.println("\nSorted by empid"); for (int i=0; i<employeeList.size(); i++) System.out.println(employeeList.get(i)); Collections.sort(employeeList, new Sortbyname()); System.out.println("\nSorted by name"); for (int i=0; i<employeeList.size(); i++) System.out.println(employeeList.get(i)); } } |
|
| 7. |
What is the situation in which fail-fast and fail-safe iterators works? What are the issues to use the fail-safe along with the example? |
|
Answer» Fail fast and fail-safe is one of the prominent features of collection iterator framework, which works on the principle of modification of a record in the collection object while iterating over it, HOWEVER, both have a different way to handle. Fail Fast iterator: The fail-fast iterator checks, If there is modification happening while iterating over the elements, it will check and throw the exception called a ConcurrentModificationException. Typically the fail-fast iterator implements the volatile counter whenever the iterator gets instantiated this counter embedded to the iterator and in the case of any modification it will increment the counter if it is greater than one it will throw the exception. Below is the code snippet for same. public class FailFastDemo{ public static void main(String[] args) { Map<String, String> capital = new HashMap<String, String>(); capital.put("Delhi", "INDIA"); capital.put("Moscow", "Russia"); capital.put("New YORK", "USA"); Iterator iterator = capital.keySet().iterator(); while (iterator.hasNext()) { System.out.println(capital.get(iterator.next())); // in this code we are modifying the element to Map // exception will be thrown on next call // of next() method. capital.put("Istanbul", "Turkey"); } } }Fail-safe Iterator: Fail safe iterator is a part of the java.util.concurrent api, it works on the principal of the CLONING, actually in this case it creates the clone of the object and iterate over it, if any modification happens, it wouldn’t touch to the actual object and continue to iterate without failure. One issue with the fail-safe iterator is, it doesn’t guarantee that updated elements will be reflected in the iteration. And another issue is it CONSUMES more memory than fail fast because it requires to create the clone, whereas failing fast works only original one. The CopyOnWriteArrayList and ConcurrentHashMap is the example of fail-safe iterator. Below is the code snippet for same. ConcurrentHashconcurrentHashMap<String, Integer> concurrentHashMap = new ConcurrentHashconcurrentHashMap<>(); concurrentHashMap.put("First", 10); concurrentHashMap.put("Second", 20); concurrentHashMap.put("Third", 30); concurrentHashMap.put("Fourth", 40); Iterator<String> iterator = concurrentHashMap.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); concurrentHashMap.put("Fifth", 50); } |
|
| 8. |
What is the purpose of the initial capacity and load factor parameters of a HashMap? What are their default values? How is the threshold plays the role to decide the size? |
|
Answer» Initial capacity in HashMap plays a vital ROLE in the allocation of memory as we know that HashMap works on the key-value pair, that means the HashMap needed a large AMOUNT of memory to maintain the key and value, to overcome these challenges the HashMap works on the principal to allocate the optimum size, whenever the HashMap get instantiated, that is CALLED Initial capacity, which is usually a 16. Now let’s focus on load factor, so there is a concept of threshold value, which is always a 0.75 size of the total capacity, the significance of this is whenever any new record gets inserted in the HashMap, the HashMap COMPUTES the allocated size of the HashMap, then it checks whether this size reaches the threshold value then HashMap increased the size of 75% from the CURRENT size to augmented the storage for next incoming record. |
|
| 9. |
What is the time complexity of putting and getting an element from such structure? Which Map implementation provide better performance? |
|
Answer» The time complexity of HASHMAP to getting the elements is usually from O(N) to O(Log(n)), when there is no collision OCCURS and the worst case is O(n) to O(1), because when collision occurs and HashMap needs to travel till the end of the linked list. Now in the case of putting the elements in HashMap, is always O(1). Below classes is the implementation of Map interface.
However, as PER the MEMORY performance basis, the WeakHashMap gives better performance. But it doesn’t provide the thread safety if one needs to achieve the thread safety, to achieve the thread safety one needs to use the ConcurrentHashMap because it provides thread safety without using synchronization. |
|
| 10. |
What is collision problem? How HashMap performs collision detection and collision prevention when the index position found for the same key, what are the algorithms performed to avoid this situation? |
|
Answer» The COLLISION problem is, whenever the hash function returns the same INDEX position for the different key, then a collision occurs. The collision detection technique is also called collision detection. Now, let's move to the collision prevention technique. The HashMap uses below algorithms to resolve the collision problem.
|
|
| 11. |
How does HashMap work and how is implemented in Java? |
|
Answer» Hashmap compose with an array of node and Node is an object of a class which has following below object:
Let’s see how it all together works, first will approach to hashing process, which uses a hashing algorithm to generate index position-taking as an input of hashkey value of the corresponding object. Let’s look on below code snippet: Class KeyDemo { String key; KeyDemo(String key){ This.key = key; } @Override public int hashCode() { return (int)key.charAt(0); } @Override public booleanequals(Object OBJ) { return key.equals((String)obj); } }The moment, when we passing the key object in the put method of hashmap like below Map map = new HashMap(); map.put(key, value);The HashMap calling the hashcode() method of object and get the 10 digit numeric value, which eventually passing to the hashing algorithms which is like below syntax f(h(hashCode (key) % array_length)) = {index position} of hashmap, ,where x is a hashcode value, according to the index position the hashmap insert the value object in the corresponding bucket. HashMap uses the linkedlist to store the value object. Below is the implementation of HashMap, where K is the Key and V is the Value, usually the initial capacity is always 16, whenever the map get instantiated and it's computed every time when its reach to a threshold value which is 0.75, public class Map<K, V> { private Entry<K, V>[] buckets; private static final int INITIAL_CAPACITY = 1 << 4; // 16 private int size = 0; public Map() { this(INITIAL_CAPACITY); } public Map(int capacity) { this.buckets = new Entry[capacity]; } public VOID put(K key, V value) { Entry<K, V> entry = new Entry<>(key, value, null); int bucket = getHash(key) % getBucketSize(); Entry<K, V> EXISTING = buckets[bucket]; if (existing == null) { buckets[bucket] = entry; size++; } else { // compare the keys see if key already exists while (existing.next != null) { if (existing.key.equals(key)) { existing.value = value; return; } existing = existing.next; } if (existing.key.equals(key)) { existing.value = value; } else { existing.next = entry; size++; } } }The retrieval process of HashMap is, it has to compute the hashcode whenever the get method call which along with the key, using the key, hashmap computes the index position and equal() plays the vital role to identify the correct object from the bucket or LinkedList. Below is the code snippet for same. public V get(K key) { Entry<K, V> bucket = buckets[getHash(key) % getBucketSize()]; while (bucket != null) { if (bucket.key.equals(key)) { return bucket.value; } bucket = bucket.next; } return null; }There is a collision issue happening which is usually taken care of by collision detection and collision prevention. |
|
| 12. |
What is the difference between Comparable and Comparator interface? |
Answer»
A comparable object is capable of comparing itself with another object. The class itself must implement the java.lang.Comparable interface to COMPARE its instances. Comparable has compareTo(Object o) to sort the objects Consider a Product class that has members like, productId, productName, ManufacturerYear. Suppose we wish to sort a LIST of Products based on year of productId. We can implement the Comparable interface with the Product class, and we override the method compareTo() of Comparable interface.
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface. Comparator has compare(Object o1, Object o2) to sort the objects. class Product implements Comparable<Product> { private int productRating; private String productName; private int mfgYear; private double price; // USED to sort movies by mfgYear public int compareTo(Product pr) { return this.mfgYear - pr.mfgYear; } // Constructor public Product(int pr, String pn, int mY, double price) { this.pr = pr; this.pn = pn; this.mY = mY; this.price = price; } // GETTER methods for accessing private data public double getPrice() { return price; } public String getProductName() { return productName; } public int getMfgYear() { return mfgYear; } public int getProductRating() { return productRating; } } // Class to compare Products by ratings class ProductRatingCompare implements Comparator<Product> { public int compare(Product m1, Product M2) { if (m1.getProductRating() < m2.getProductRating()) return -1; if (m1.getProductRating() > m2.getProductRating()) return 1; else return 0; } } // Class to compare Products by product name class ProductNameCompare implements Comparator<Product> { public int compare(Product m1, Product m2) { return m1.getProductName().compareTo(m2.getProductName()); } } |
|
| 13. |
What are the different ways to iterate a List? |
|
Answer» We can ITERATE a List in two ways:
Here is the code SNIPPET: public static void main(STRING[] args) { List<String> stringList = NEW ArrayList<>(); //using for-each loop for(String str : stringList){ System.out.println(str); } //using iterator Iterator<String> it = stringList.iterator(); while(it.hasNext()){ String obj = it.next(); System.out.println(obj); } } |
|
| 14. |
What is the difference between Iterator and ListIterator? |
|
Answer» We can traverse the elements in a LIST using Iterator in a forward direction. Using LISTITERATOR, we can traverse the elements in the forward and backward direction both. Iterator can be USED in these collection types: List, Set, and Queue. ListIterator can be used in List collection only. Iterator has the following FUNCTIONALITIES:
ListIterator has the following functionalities:
Iterator can only perform remove operation while traversing the elements in a collection. If we TRY to add elements, it will throw ConcurrentModificationException. ListIterator can perform add, remove operation while traversing the elements in a collection. We won’t get any exception while adding element using ListIterator. |
|
| 15. |
What are the different interfaces are there in Collection framework? |
Answer»
List is a collection of unordered elements. It contains duplicate elements also.
Set is a collection of ORDERED elements. It doesn’t contain any duplicate elements.
Queue interface available in java.util package and EXTENDS Collection interface
Map is a collection of (key,value) PAIR. |
|
| 16. |
How to convert a HashMap to an ArrayList? |
Answer»
|
|
| 17. |
Write a program to sort a map based on value? |
|
Answer» You can see the below code snippet: // class to sort hashmap by values public class SortByValue{ public static HashMap<String, Integer> sortByValue (HashMap<String, Integer> hm) { List<MAP.Entry<String, Integer> > list = new LinkedList<Map.Entry<String, Integer> >(hm.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o1.getValue()).COMPARETO(o2.getValue()); } }); HashMap<String, Integer> tempMap = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> element : list) { tempMap.put(element.getKey(), element.getValue()); } return tempMap; } // Driver Code public static void main(String[] args) { HashMap<String, Integer> hm = new HashMap<String, Integer>(); // enter DATA into hashmap hm.put("COMPUTER Science", 93); hm.put("Physics", 88); hm.put("Chemistry", 69); hm.put("Database", 95); hm.put("Operating System", 90); hm.put("Networking", 79); Map<String, Integer> map = sortByValue(hm); // print the sorted hashmap for (Map.Entry<String, Integer> en : map.entrySet()) { System.out.println("Key = " + en.getKey() + ", Value = " + en.getValue()); } } } |
|
| 18. |
What are the ways to convert an array of strings to list and list of strings to array of strings? |
Answer»
Arrays class of java.util package contains asList() which helps to convert an array of strings to List. This method bridges between array-based and collection-based APIS. The returned List is serializable and implements RandomAccess
|
|
| 19. |
What is the difference between Stack and Queue? |
|
Answer» Stack is a data structure which follows the principle of LIFO (Last In Fast Out) OR FILO (First In Last Out), i.e element inserted at last will be the first to be removed from the stack. Queue is a data structure which follows the principle of FIFO (First In First Out), i.e. element inserted at first to the list will be the first to be removed from the list. For Stack, insertion and deletion of an element are TERMED as PUSH and POP while for Queue, insertion, and deletion of an element are termed as ENQUEUE and DEQUEUE. In JAVA, Stack is a class present in java.util package and Queue is an interface present in java.util package. |
|
| 20. |
What is an Iterator? Explain with an Example. |
|
Answer» Iterator is an interface. It is PRESENT in java.util package. It has the following functionalities:
|
|
| 21. |
Where to use ArrayList and where to use LinkedList? |
|
Answer» In your program, if you try to insert the elements and retrieve the elements in the same order, then you should use LinkedList. Complexity for inserting an element is O(n) in case of LinkedList. If you don't NEED to MAINTAIN the insertion or deletion order, then you can use ArrayList. Also, the complexity of insertion and deletion in an ArrayList is O(1) as this data structure is indexed. An element can directly be accessed in ArrayList using index. Code Snippet: Insertion in an ArrayList can be DONE in 2 ways: add(E e) => Which appends the element at the end of the list. add(int index, E e) => Add the element at the specified position in the list ArrayList<String> arrayList = new ArrayList<String>(); arrayList .add("Programming"); arrayList .add(1, "Java"); arrayList .add("PYTHON"); arrayList .remove(1); // Remove value at index 1, considering the indexing starts from 0Insertion/ deletion in LinkedList: LinkedList linkedListObj = new LinkedList(); linkedListObj.add("Artificial Intelligence"); linkedListObj.add("Blocked Chain"); linkedListObj.add("ACADEMIC"); linkedListObj.remove("Blocked Chain"); |
|
| 22. |
What is the difference between SET and LIST Interface? |
|
Answer» LIST is an ordered collection of elements and hence it maintains insertion order of elements while Set doesn't maintain any ORDERING of the elements. List allows DUPLICATE elements while Set doesn't allow any elements. List is index-based, i.e.we can access elements in List based on Index, whereas we can’t access Set using index. Concrete implementation of List INTERFACE are ArrayList, LinkedList, etc. Concrete implementation of Set implementations are HashSet, LinkedHashSet, TreeSet etc. we can insert any NUMBER of null values in List. But we can have only a single null value at most in Set. ListIterator can be used to traverse a List in both directions (forward and backward) however it cannot be used to traverse a Set. Using Iterator, we can traverse the elements in Set and List. |
|
| 23. |
How is the HashSet differ from theTreeSet? What is the use case, were HashSet and Treeset fits? |
|||||||||||||||||||||||||||
Answer»
|
||||||||||||||||||||||||||||
| 24. |
How ArrayList and LinkedList differ from each other? What is the possible use case where ArrayList and LinkedList fits? |
||||||||||||||||||||||||||||||
Answer»
|
|||||||||||||||||||||||||||||||
| 25. |
What is the difference between the HashMap, TreeMap and LinkedHashMap? |
||||||||||||||||||||||||||||||||||||||||
Answer»
|
|||||||||||||||||||||||||||||||||||||||||
| 26. |
Describe various implementations of the Map interface and their use case differences and map hierarchy diagram? |
|
Answer» 1. HASHMAP:
2. LinkedHashMap Linked HashMap is an implementation of the map interface along with the hashing algorithms it also supports the ordering according to the insertion order. Code Snippet: Map map = new LinkedHashMap(); Use Case: LinkedHashMap is useful whenever we need to maintain the ordering of keys to match the ordering of insertion. along with that when we need to handle the caching mechanism. 3. TreeMap TreeMap is an implementation of the map interface, where it follows the Tree data structure which is RedBlack Tree implementation and follows the natural order Code Snippet: Map map = new TreeMap(); Use case: One can use the TreeMap when we need the natural ordering, or we need to sort the record alphabetically or ascending and descending order. |
|
| 27. |
What is a Map interface, why it’s not a part of collection framework and when to use? |
|
Answer» A map is an object that maps keys to VALUES. The map works on key-value pair principal. A map doesn't allow to contain duplicate keys. Each key can map to at most one value. HashMap, TreeMap, ConcurentHashMap are the implementation of the map. The map has three kind of collection like key, values and key values and it doesn’t IMPLEMENT the collection interface that is the reason the Map interface doesn’t fall in the collection HIERARCHY. The order of the map depends on the specific implementation classes. Code Snippet: Map map = new HashMap();. The best use case is when we need to SPECIFY the pairing between the object and store accordingly. |
|
| 28. |
Is there any other interface, which has some specific use case? |
Answer»
|
|
| 29. |
What are the basic interfaces of Java Collections Framework and how to instantiate this interface? |
|
Answer» 1. Set
2. List
3. Queue
|
|
| 30. |
What are the key benefits of using the Java Collections Framework? |
|
Answer» Collection framework provides all the implementation of available data STRUCTURE, which reduces the development time. All the classes are growable classes, that means we don’t need to PROVIDE the size at compile time. Later release Collection framework also starts supporting the Generics. Collection framework also starts supporting the thread collection class like ConcurrentHashMap and CopyOnWriteArrayList. Code quality also getting improved by USING well-tested collection API. Reduce code maintenance by using the collection framework. Collection API also provides the UTILITY class called Collections. Enhance the REUSABILITY and interpretability the code base. |
|
| 31. |
What is Collection? What is a Collection Framework? |
|
Answer» The collection is an API, INTRODUCED in JDK 5. Initially, the collection got released with some of few classes with Vector, Stack, Hash table, ARRAY. The collection is an API which has got various implementation of data structure LIST, Set, Stack, Queue, Map. The available implementation classes can group the N no of objects without specifying the SIZE. Collection API provides the growable object. All these classes are a growable class which increases the size at runtime. |
|