Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Java Collections

The document discusses Java collections and compares List vs Set, ArrayList vs HashSet, and HashMap vs HashSet. [1] Java collections provide ways to access and organize data in storage and include interfaces like List, Set, and Collection as well as classes like ArrayList and HashSet. [2] Lists allow duplicates and maintain element order while sets do not allow duplicates or maintain order. [3] ArrayList is backed by an array and allows positional access while HashSet uses a HashMap to not allow duplicates and does not provide positional access.

Uploaded by

Martin De Luca
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Java Collections

The document discusses Java collections and compares List vs Set, ArrayList vs HashSet, and HashMap vs HashSet. [1] Java collections provide ways to access and organize data in storage and include interfaces like List, Set, and Collection as well as classes like ArrayList and HashSet. [2] Lists allow duplicates and maintain element order while sets do not allow duplicates or maintain order. [3] ArrayList is backed by an array and allows positional access while HashSet uses a HashMap to not allow duplicates and does not provide positional access.

Uploaded by

Martin De Luca
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Table of Contents

Java Collections ...................................................................................................................................... 2


List vs Set ................................................................................................................................................ 3
ArrayList vs HashSet ............................................................................................................................... 3
How HashSet Internally Works in Java ................................................................................................... 4
HashMap vs HashSet .......................................................................................................................... 4
Java Collections
Ways to access/organize data in the storage.

The Collections class is a utility class having static methods for doing operations on objects of classes
which implement the Collection interface. For example, Collections has methods for finding the max
element in a Collection.

The Collection interface defines methods common to structures which hold other objects. List and Set
are subinterfaces of Collection, and ArrayList and HashSet are examples of concrete collections.

Classes that implements Collection (I) methods, can take advantage of the static Class Collections
(C) methods.
List vs Set

is an ordered sequence of elements whereas Set is a distinct list of elements


List
which is unordered.

List<E>:

An ordered collection (also known as a sequence). The user of this interface has
precise control over where in the list each element is inserted. The user can access
elements by their integer index (position in the list), and search for elements in the
list.

Set<E>:

A collection that contains no duplicate elements. More formally, sets contain no


pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.
As implied by its name, this interface models the mathematical set abstraction.

List Set
Duplicates YES NO
Order ORDERED DEPENDS ON
IMPLEMENTATION
Positional Access YES NO

ArrayList vs HashSet

ArrayList HashSet
Implements List (Interface) Set (Interface)
Ordered YES (Preserve the order of the NO (Unordered)
elements)
Synchronized NO (not meant to be used in multi- NO (not meant to be used in
threading and concurrent environment) multi-threading and concurrent
environment)
Iterable YES (Iterator, ForEach) YES (Iterator, ForEach)
Fail-Fast YES (Will throw YES (Will throw
ConcurrentModificationException if is ConcurrentModificationException
modified structurally once Iterator has if is modified structurally once
been created) Iterator has been created)
Duplicates YES NO
Backed By Array HashMap
Access Index Based: You can retrieve object Object Based: Elements is
Method by index. accessed directly from the
Object Reference.
How HashSet Internally Works in Java

Not many programmer know that HashSet is internally implemented using HashMap in Java.
But, now a curious Java developer can question that, how come HashSet uses HashMap, because
you need a key value pair to use with Map, while in HashSet we only store one object.

HashMap allows duplicate values and this property is exploited while implementing HashSet in Java.
Since HashSet implements Set interface it needs to guarantee uniqueness and this is achieved by
storing elements as keys with same value always.

HashMap vs HashSet

HashMap Hash Set


HashMap is an implementation of HashSet is an implementation of Set
Map interface Interface
HashMap Stores data in form of HashSet Store only objects
key-value pair
Put method is used to add element Add method is used to add element is Set
in map
In hash map hashcode value is Here member object is used for calculating
calculated using key object hashcode value which can be same for two
objects so equal () method is used to check
for equality if it returns false that means
two objects are different.
HashMap is faster than HashSet HashSet is slower than Hashmap
because unique key is used to
access object

You might also like