Collection

java collections interview questions

java collections interview questions

Set – Java Collections Interview Questions

  1. Which collection is faster in Java?
  2. What is the use of collection in Java?
  3. What is the difference between Java collection and Java collections?
  4. What are the benefits of Java collection framework?
  5. How do you select a collection in Java?
  6. Which collection is better in Java?
  7. How many types of collections are there in Java?
  8. How do you handle a list in Java?
  9. What is difference between Array and ArrayList?
  10. Which list is faster in Java?
  11. Is Java map a collection?
  12. Why is string immutable in Java?

Which collection is faster in Java?

If you need fast access to elements using index, ArrayList should be choice. If you need fast access to elements using a key, use HashMap. If you need fast add and removal of elements, use LinkedList (but it has a very poor seeking performance).

What is the use of collection in Java?

The Java collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them. A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.

What is the difference between Java collection and Java collections?

The Collection is an interface whereas Collections is a utility class in Java. The Set, List, and Queue are some of the subinterfaces of Collection interface, a Map interface is also part of the Collections Framework, but it doesn't inherit Collection interface.

What are the benefits of Java collection framework?

The Java Collections Framework provides the following benefits: Reduces programming effort: By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level "plumbing" required to make it work.

How do you select a collection in Java?

Always start with ArrayList and HashSet and HashMap (i.e. not LinkedList or TreeMap). Type declarations should always be an interface (i.e. List, Set, Map) so if a profiler or code review proves otherwise you can change the implementation without breaking anything.

Which collection is better in Java?

HashSet has slightly better performance than LinkedHashSet , but its iteration order is undefined. TreeSet is ordered and sorted, but slower. TreeMap is ordered and sorted, but slower. LinkedList has fast adding to the start of the list, and fast deletion from the interior via iteration.

How many types of collections are there in Java?

Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

How do you handle a list in Java?

Let's see a simple example of List where we are using the ArrayList class as the implementation.

  1. import java.util.*;
  2. public class ListExample1
  3. public static void main(String args[])
  4. //Creating a List.
  5. List<String> list=new ArrayList<String>();
  6. //Adding elements in the List.
  7. list.add("Mango");
  8. list.add("Apple");

What is difference between Array and ArrayList?

Array is a fixed size data structure while ArrayList is not. One need not to mention the size of Arraylist while creating its object. Even if we specify some initial capacity, we can add more elements. Array can contain both primitive data types as well as objects of a class depending on the definition of the array.

Which list is faster in Java?

Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

Is Java map a collection?

Because a Map is not a true collection, its characteristics and behaviors are different than the other collections like List or Set. A Map cannot contain duplicate keys and each key can map to at most one value. Some implementations allow null key and null value (HashMap and LinkedHashMap) but some does not (TreeMap).

Why is string immutable in Java?

The string is Immutable in Java because String objects are cached in the String pool. ... Mutable String would produce two different hashcodes at the time of insertion and retrieval if contents of String was modified after insertion, potentially losing the value object in the map.

Difference Between Xvid and DVD
Since DVD is a media format, it doesn't really dictate on which format the data stored in it takes. ... Videos encoded in Xvid are not supported by st...
Difference Between Apple Juice and Apple Cider
Apple cider is made from apples that are washed, cut and ground into an "apple mash" similar to applesauce. ... On the other hand, apple juice undergo...
Difference Between Emo and Goth
Emos express themselves in poetry such as Allen Ginsberg's 'Howl'. They also make critiques based on post-punk and punk philosophy. Goth, on the other...