0% found this document useful (0 votes)
34 views4 pages

Arraylist and Vector

The document discusses various differences between common Java collection classes: 1. ArrayList is not synchronized while Vector is. ArrayList increases size by 50% and Vector doubles size. 2. ArrayList uses a dynamic array while LinkedList uses doubly linked lists. ArrayList is better for fetching data and LinkedList for manipulation. 3. Iterator traverses forward only and can be used in multiple collections. ListIterator traverses both ways but only for Lists. 4. Iterator supports all elements while Enumeration only legacy ones. Iterator is also fail-fast and slower. 5. Lists allow duplicates but Sets do not. TreeSet maintains order unlike HashSet. Sets contain values and Maps contain key-value pairs.

Uploaded by

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

Arraylist and Vector

The document discusses various differences between common Java collection classes: 1. ArrayList is not synchronized while Vector is. ArrayList increases size by 50% and Vector doubles size. 2. ArrayList uses a dynamic array while LinkedList uses doubly linked lists. ArrayList is better for fetching data and LinkedList for manipulation. 3. Iterator traverses forward only and can be used in multiple collections. ListIterator traverses both ways but only for Lists. 4. Iterator supports all elements while Enumeration only legacy ones. Iterator is also fail-fast and slower. 5. Lists allow duplicates but Sets do not. TreeSet maintains order unlike HashSet. Sets contain values and Maps contain key-value pairs.

Uploaded by

Bhavna Ghuman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. What Is The Difference Between Arraylist And Vector?

Answer :
ArrayList:

o ArrayList is not synchronized.


o ArrayList is not a legacy class.
o ArrayList increases its size by 50% of the array size.

Vector:

o Vector is synchronized.
o Vector is a legacy class.
o Vector increases its size by doubling the array size.
2. What Is The Difference Between Arraylist And Linkedlist?
Answer :
ArrayList:

o ArrayList uses a dynamic array.


o ArrayList is not efficient for manipulation because a lot of shifting is required.
o ArrayList is better to store and fetch data.

LinkedList:

o LinkedList uses doubly linked list.


o LinkedList is efficient for manipulation.
o LinkedList is better to manipulate data.
3. What Is The Difference Between Iterator And Listiterator?
Answer :
Iterator traverses the elements in forward direction only whereas ListIterator traverses
the elements in forward and backward direction.
Iterator:

o Iterator traverses the elements in forward direction only.


o Iterator can be used in List, Set and Queue.

ListIterator:

o ListIterator traverses the elements in backward and forward directions both.


o ListIterator can be used in List only.
4. What Is The Difference Between Iterator And Enumeration?
Answer :
Iterator:

o Iterator can traverse legacy and non-legacy elements.


o Iterator is fail-fast.
o Iterator is slower than Enumeration.

Enumeration:
o Enumeration can traverse only legacy elements.
o Enumeration is not fail-fast.
o Enumeration is faster than Iterator.
5. What Is The Difference Between List And Set?
Answer :
List can contain duplicate elements whereas Set contains only unique elements.
6. What Is The Difference Between Hashset And Treeset?
Answer :
HashSet maintains no order whereas TreeSet maintains ascending order.
7. What Is The Difference Between Set And Map?
Answer :
Set contains values only whereas Map contains key and values both.
8. What Is The Difference Between Hashset And Hashmap?
Answer :
HashSet contains only values whereas HashMap contains entry(key,value). HashSet
can be iterated but HashMap need to convert into Set to be iterated
9. What Is The Difference Between Hashmap And Treemap?
Answer :
HashMap maintains no order but TreeMap maintains ascending order.
10. What Is The Difference Between Hashmap And Hashtable?
Answer :
HashMap:

o HashMap is not synchronized.


o HashMap can contain one null key and multiple null values.

Hashtable:

o Hashtable is synchronized.
o Hashtable cannot contain any null key or null value.

11. What Is The Difference Between Collection And Collections?


Answer :
Collection is an interface whereas Collections is a class. Collection interface provides
normal functionality of data structure to List, Set and Queue. But, Collections class is
to sort and synchronize collection elements.
12. What Is The Difference Between Comparable And Comparator?
Answer :
Comparable:

o Comparable provides only one sort of sequence.


o It provides one method named compareTo().
o It is found in java.lang package.
o If we implement Comparable interface, actual class is modified.

Comparator:

o Comparator provides multiple sort of sequences.


o It provides one method named compare().
o it is found in java.util package.
o Actual class is not modified.
13. What Is The Advantage Of Properties File?
Answer :
If you change the value in properties file, you don't need to recompile the java class.
So, it makes the application easy to manage.
14. What Does The Hashcode() Method?
Answer :
The hashCode() method returns a hash code value (an integer number). The
hashCode() method returns the same integer number, if two keys (by calling equals()
method) are same.
But, it is possible that two hash code numbers can have different or same keys
15. Why We Override Equals() Method?
Answer :
The equals method is used to check whether two objects are same or not. It needs to
be overridden if we want to check the objects based on property. For example,
Employee is a class that has 3 data members: id, name and salary. But, we want to
check the equality of employee object on the basis of salary. Then, we need to
override the equals() method.
16. How To Synchronize List, Set And Map Elements?
Answer :
Yes, Collections class provides methods to make List, Set or Map elements
as synchronized:

o public static List synchronizedList(List l){}


o public static Set synchronizedSet(Set s){}
o public static SortedSet synchronizedSortedSet(SortedSet s){}
o public static Map synchronizedMap(Map m){}
o public static SortedMap synchronizedSortedMap(SortedMap m){}
17. What Is The Advantage Of Generic Collection?
Answer :
If we use generic class, we don't need typecasting. It is typesafe and checked at
compile time.
18. What Is Hash-collision In Hashtable And How It Is Handled In
Java?
Answer :
Two different keys with the same hash value is known as hash-collision. Two different
entries will be kept in a single hash bucket to avoid the collision
19. What Is The Default Size Of Load Factor In Hashing Based
Collection?
Answer :
The default size of load factor is 0.75. The default capacity is computed as initial
capacity * load factor. For example, 16 * 0.75 = 12. So, 12 is the default capacity of
Map.

You might also like