0% found this document useful (0 votes)
174 views

34 Java Collections Interview Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
174 views

34 Java Collections Interview Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 21
‘2122, 1:58 PM Java Collections Interview Questions (2023) javatpoint Home Interview Questions ava sau Pythor JavaSerip Angula JaveW7 root rrr) eee ie SCROLL TO TOP Bites |nps:iiwwjavalpoin comijava-colectonsnteriew-questions at‘2122, 1:58 PM Java Collections Interview Questions (2023) javatpoint 34 Java Collections Interview Questions In Java, collection interview questions are most asked by the interviewers, Here is the list of the most asked collections interview questions with answers. 1) What is the Collection framework in Java? Collection Framework is a combination of classes and interface, which is used to store and manipulate the data in the form of objects. It provides various classes such as ArrayList, Vector, Stack, and HashSet, etc. and interfaces such as List, Queue, Set, etc. for this purpose. 2) What are the main differences between array and collection? Array and Collection are somewhat similar regarding storing the references of objects and manipulating the data, but they differ in many ways. The main differences between the array and Collection are defined below: © Arrays are always of fixed size, ie., a user can not increase or decrease the length of the array according to their requirement or at runtime, but In Collection, size can be changed dynamically as per need. © Arrays can only store homogeneous or similar type objects, but in Collection, heterogeneous objects can be stored. © Arrays cannot provide the ?ready-made? methods for user requirements as sorting, searching, etc, but Collection includes readymade methods to use. 3) Explain various interfaces used in Collection framework? Collection framework implements various interfaces, Collection interface and Map interface (ava.utilMap) are the mainly used interfaces of Java Collection Framework. List of interfaces of Collection Framework is given belo |nps:iiwwjavalpoin comijava-cotectonsnteriew-questions‘2122, 1:58 PM Java Collections Interview Questions (2023) javatpoint 1. Collection interface: Collection (java.util.Collection) is the primary interface, and every collection must implement this interface. Syntax: public interface Collection extends Iterable Where represents that this interface is of Generic type 2. List interface: List interface extends the Collection interface, and it is an ordered collection of objects. It contains duplicate elements. It also allows random access of elements. Syntax: | public interface List extends Collection 3. Set interface: Set (java.util.Set) interface is a collection which cannot contain duplicate elements. It can only include inherited methods of Collection interface Syntax: interface Set extends Collection [= Queue interface: Queue (java.util. Queue) interface defines queue data structure, which stores the elements in the form FIFO (first in first out) -nps:ihwwjavalpoin comijava-colectonsnterview-questions a1‘2122, 1:58 PM Java Collections Interview Questions (2023) javatpoint Syntax: public interface Queue extends Collection 4, Dequeue interface: it is a double-ended-queue. It allows the insertion and removal of elements from both ends. it implants the properties of both Stack and queue so it can perform LIFO (Last in first out) stack and FIFO (first in first out) queue, operations. Syntax: public interface Dequeue extends Queue 5. Map interface: A Map (java.util.Map) represents a key, value pair storage of elements. Map interface does not implement the Collection interface. It can only contain a unique key but can have duplicate elements, There are two interfaces which implement Map in java that are Map interface and Sorted Map. 4) What is the difference between ArrayList and Vector? No, ArrayList Vector 1) ArrayList is not synchronized. Vector is synchronized. 2) ArrayList is not a legacy class. Vector is a legacy class. 3) ArrayList increases its size by 50% of the Vector increases its size by doubling the array array size. size. 4) ArrayList is not ?thread-safe? as it is not Vector list ic ?thread-cafe? ac it? extends Queue list=new Arraylist(); list.add(‘ankit’); list.add(“nippun"); System.out.printin(list.size(); 28) How to convert ArrayList to Array and Array to ArrayList? We can convert an Array to ArrayList by using the asList() the static method of Arrays class and accepts the List obje Arrays.asList(item) |nps:iiwwjavalpoin comijava-colectonsnteriew-questions va‘2am, 154M Java Collections Itervew Questions (2023)-javatpoin We can convert an ArrayList to Array using toArray) method of the ArrayList class. Consider the following syntax to convert the ArrayList to the List object List_object:toArray(new String[List_objectsize(]) 29) How to make Java ArrayList Read-Only? We can obtain java ArrayList Read-only by calling the Collections.unmodifiableCollection() method When we define an ArrayList as Read-only then we cannot perform any modification in the collection through add(), remove() or set() method. 30) How to remove duplicates from ArrayList? There are two ways to remove duplicates from the ArrayList. © Using HashSet: By using HashSet we can remove the duplicate element from the ArrayList, but it will not then preserve the insertion order. © Using LinkedHashSet: We can also maintain the insertion order by using LinkedHashSet instead of HashSet. The Process to remove duplicate elements from ArrayList using the LinkedHashSet: © Copy all the elements of ArrayList to LinkedHashSet. © Empty the ArrayList using clear) method, which will remove all the elements from the list. © Now copy all the elements of LinkedHashset to ArrayList 31) How to reverse ArrayList? To reverse an ArrayList, we can use reverse() method of Collections class, Consider the following example import java.util ArrayList; import java.util.Collection; import java.util.Collections; import java.util.terator, import java.util.List; |nps:iiwwjavalpoin comijava-colectonsnterview-questions swt‘arena, 1:58PM Java Colectons Interview Questions (2023) javatpoint public class ReverseArrayList { public static void main(String{] args) { List list = new ArrayList0; listadd(10); listadd(50); listadd(30); Iterator i = listiterator(); System.out printin("printing the list..." while(,hasNext() System.out printin(inextQ); } Iterator i2 = listiterator(); Collections.reverse(list); System.out printin("printing list in reverse order. (i2.hasNextQ) System.out printin(i2.nextQ); Output printing the list 10 printing list in reverse order 50 10 |nps:iiwwjavalpoin comijava-colectonsnteriew-questions‘2122, 1:58 PM Java Collections Interview Questions (2023) javatpoint 32) How to sort ArrayList in descending order? To sort the ArrayList in descending order, we can use the reverseOrder method of Collections class. Consider the following example. import java.util ArrayList; import java.util,Collection; import java.util.Collections; import java.util, Comparator, import java.util.|terator; import java.util.List; public class ReverseArrayList ( al] args) { List list = new Arraylist0; list.add(10); listadd(50); listadd(30); listadd(60); listadd(20); list.add(90); public static void main(St Iterator i = listiteratord; System.out printin("printing the list..."); while(ihasNext() { System.out printin(inextQ); Comparator cmp = Collections.reverseOrder(); Collections sort(list,cmp); System.out printin(’printing list in descending order. Iterator i2 = list.iterator(; while(i2.hasNext0) { |nps:ihwwjavalpoin comijava-cotectons-nteriew-questions ret‘2122, 1:5 PM Java Collections Interview Questions (2023) javatpoint System.out printin(i2.next0); Output printing the list 10 58 30 60 20 printing 1 90 in descending order. 68 50 30 20 33) How to synchronize ArrayList? We can synchronize ArrayList in two ways. © Using Collections synchronizedList) method © Using CopyOnWriteArrayList 34) When to use ArrayList and LinkedList? LinkedLists are better to use for the update operations w search operations. |nps:iiwwjavalpoin comijava-cotectonsnteriew-questions wet2122122, 11:54 PM Java Collections Interview Questions (2023) javatpoint

You might also like