Java Interview Question and Answer on Collection Framework
- Difference between Array and ArrayList
Arrays: is a collection of elements of similar primitive datatypes. While creating array we need to specify the size of the array. In array, We can store objects and primitive values
String arr = new String;
arr = “java”;
Here, string “java” is inserted into the first index position of an array
Arraylist : It is an ordered collection that grows dynamically and in arraylist we can store different types of elements. But it is not required to specify the of arraylist size while creating it, because arrayList grows dynamically. We can store only primitives prior to java 1.5 but From java 1.5 we can store even objects also.
Syntax for adding elements into an arraylist
List s1 = new ArrayList();
- What is vector?
Vector is similar to arrayList used for random access. Vector is a dynamic array like arrayList. vector size can be incremented or decrement when we add or removed elements from vector . The vector is synchronized. vector and HashTable are the only collections are added since 1.0,remaining collections are added from 2.0.
- Difference between arrayList and vector
Both Arraylist and vector grows dynamically with having some differences
1) ArrayList is not a synchronized kind of collection and vector is synchronized.
2) Vector is a legacy collection introduced in 1.0 and ArrayList was introduced in java 2.0. Performance wise it is recommended to use arrayList rather than vector because by default vector is synchronized which reduces performance if only one thread accesses it.
Best java training in Pune for the java certification course. Learn core java and advanced java with best industry experts.
- Define Iterator and methods in Iterator.
This cursor is also called a universal cursor because this cursor can be applied to any Collection Interfaces and it is used to access the elements in the forward direction only. An Iterator is an interface and hence we cannot create an object of it directly. iterator() method gives you an object of the Iterator interface
- Get the iteartor object by calling the iterator method
ArrayList l1=new ArrayList();
2)then Call hasNext() method on iterator object in using till hasNext() returns true.
3) Obtain each element by calling next() inside the loop.
- When do we use HashSet over TreeSet
If we want to search for a unique element in the collection and does not want to sort the element the we go for a hash set
- When do we use Treeset over HashSet?
TreeSet is preferred for the Fast insertion and retrieval of elements. And for sorting elements.
- Explain about Map interface in java.
The map is a collection of elements. Map stores the elements in the form of key and value pairs. And in the map we have to specify elements in the key and value pair A Map contains unique keys whereas values can be duplicated. A map can store values on the basis of key, i.e. key and value pair. Each key and value pair in the map is known as an entry. A Map contains unique keys where values can be duplicated. We can perform a search, update or delete elements on a map on the basis of a key.
- What is the Sortedmap interface?
Sitemap extends the Map interface.SortedMAp is an interface that sorts the entries of the map in ascending order. TreeMAp is an implementation class for the SortedMap interface
- What is HashTable and explain the features of HashTable?
HashTable was available before the collection framework. When the collection framework was started HashTable extends the Dictionary class and Map interface. HashTable offers a convenient way of storing key/ value pairs. HashTable does not allow nulls either keys or values. HashTable is synchronized
- Difference between Hashmap and HashTable?
Hashmap is not synchronized. HashTable is synchronized.
Hashmap allows a maximum of one null key and any number of null values. HashTable does not allow null values.
As Hashmap is not synchronized its performance is faster than Hashtable. The Performance of the hash table is slower when compared to Hashmap because hash table is synchronized.
Hashmap introduced starting from HashTable is even before the collection
- Explain the Enumeration cursor in java.
The enumeration cursor is used to iterate/traverse vector and stack type of collection. It is a legacy cursor in java. The enumeration cursor traverses the collection in the forward direction only.
- Define Generics in java?
Generics s used to achieve datatype safety in the programming and it is also used to apply restrictions on a different type of data to get inserted into the collection. Generic is a parametric type that can be passed as a parameter to the class, method, and interface and it is enclosed in <> brackets. and in these brackets, we need to pass the class name as a parameter always
Call the Trainer and Book your free demo class for now!!!
© Copyright 2020 | Sevenmentor Pvt Ltd.