0% found this document useful (0 votes)
11 views8 pages

2 Jan 2025

The document provides an overview of various Java collection types including TreeMap, Hashtable, HashMap, LinkedHashMap, Set, List, ArrayList, and Vector, highlighting their characteristics such as order maintenance, synchronization, and handling of null values. It compares these collections based on properties like insertion order, null key/value allowance, and performance in single vs. multi-threaded environments. Additionally, it explains the differences between Comparable and Comparator interfaces used for sorting objects.

Uploaded by

kanti chandrakar
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)
11 views8 pages

2 Jan 2025

The document provides an overview of various Java collection types including TreeMap, Hashtable, HashMap, LinkedHashMap, Set, List, ArrayList, and Vector, highlighting their characteristics such as order maintenance, synchronization, and handling of null values. It compares these collections based on properties like insertion order, null key/value allowance, and performance in single vs. multi-threaded environments. Additionally, it explains the differences between Comparable and Comparator interfaces used for sorting objects.

Uploaded by

kanti chandrakar
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/ 8

TreeMap:

 In TreeMap keys are maintained in ascending order.


 TreeMap uses balanced binary trees algorithm internally.
 Insertion order is not maintained.
 TreeMap is available since jdk1.2V.
 Null keys are not allowed where as null values are accepted.
 Duplicates values are accepted, If duplicate key is there the previous key-
value will be replaced.
♪ HashMap doesn’t maintain insertion order
♪ Linked HashMap maintains Insertion Order
♪ TreeMap Maintains Ascending order of keys
Hashtable
 Hashtable is available since jdk1.0V.

 Insertion order is not preserved.

 Heterogeneous objects are allowed for both keys and values.

 Null key (or) null value is not allowed.

 It allows duplicate values with unique keys.

 Every method present inside Hashtable is synchronized

 Default capacity is 11 & load factor is 0.75.

Hashtable h=new Hashtable();

Hashtable h=new Hashtable(int initialcapacity);


HashMap Vs LinkedHashMap Vs TreeMap Vs Hashtable

Property HashMap LinkedHashMap TreeMap Hashtable

Not Maintained
Insertion order Not Maintained (But keys are Not
Maintained sorted in Maintained
ascending order)

Null
Keys/Values Allowed Allowed Not Allowed Not Allowed
(Null Values are
allowed)

Synchronization Not Not Not Synchronized


Synchronized Synchronized Synchronized
Important Differences in
‘Collections’
Set (Vs) List
Set List

A set represents a collection of elements A List represents ordered collection of


Order of the elements may change in the elements. List preserves the order of
set. elements in which they are entered.

Set will not allow duplicate values to be List will allow duplicate values.
stored.
Accessing elements by their index Accessing elements by index is possible
(position number) is not possible in case in lists.
of sets.
ArrayList (Vs) Vector
ArrayList Vector

ArrayList object is not synchronized Vector object is synchronized by


by default. default.
Incase of a single thread, using In case of multiple threads, using
ArrayList is faster than the Vector. Vector is advisable. With a single
thread, Vector becomes slow.

ArrayList increases its size every time Vector increases its size every time by
by 50 percent (half). doubling it.
HashMap (Vs) HashTable
HashMap HashTable
HashMap object is not synchronized by Hashtable object is synchronized by
default. default.
In case of a single thread, using HashMap In case of multiple threads, using
is faster than the Hashtable. Hashtable is advisable, with a single
thread, Hashtable becomes slow.
HashMap allows null keys and null values Hashtable does not allow null keys or
to be stored. values.
Comparable (Vs) Comparator
Comparable Comparator

It is an Interface present in the java.lang It is an Interface present in the java.util


package package

It is used for defining the natural sorting It is used to compare objects in a way that
order of the Object might not align with the natural ordering
[Custom sorting order]

Uses compareTo() Uses compare()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy