Java Collections Interview Questions
Java Collections Interview Questions
Collections
Interfaces
We
will
discuss
about
different
collection
interfaces
along
with
their
purpose.
Refer
to
this
youtube
videos
(https://www.youtube.com/watch?v=GnR4hCvEIJQ
&
https://www.youtube.com/watch?v=6dKGpOKAQqs)
for
more
details.
What
are
the
important
methods
that
are
declared
in
the
Collection
Interface?
Most
important
methods
declared
in
the
collection
interface
are
the
methods
to
add
and
remove
an
element.
add
method
allows
adding
an
element
to
a
collection
and
delete
method
allows
deleting
an
element
from
a
collection.
size()
methods
returns
number
of
elements
in
the
collection.
Other
important
methods
defined
as
part
of
collection
interface
are
shown
below.
interface
Collection<E>
extends
Iterable<E>
{
boolean
add(E
paramE);
boolean
remove(Object
paramObject);
int
size();
boolean
isEmpty();
void
clear();
boolean
contains(Object
paramObject);
boolean
containsAll(Collection<?>
paramCollection);
boolean
addAll(Collection<?
extends
E>
paramCollection);
boolean
removeAll(Collection<?>
paramCollection);
boolean
retainAll(Collection<?>
paramCollection);
Iterator<E>
iterator();
//A
NUMBER
OF
OTHER
METHODS
AS
WELL..
}
Get
method
allows
to
get
a
value
from
the
Map
based
on
the
key.
V
get(Object
paramObject);
POPULAR
VIDEOS
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
Java
Interview
:
A
Guide
for
Experienced:
https://www.youtube.com/watch?v=0xcgzUdTO5M
Collections
Interview
Questions
1:
https://www.youtube.com/watch?v=GnR4hCvEIJQ
Collections
Interview
Questions
2:
https://www.youtube.com/watch?v=6dKGpOKAQqs
Collections
Interview
Questions
3:
https://www.youtube.com/watch?v=_JTIYhnLemA
Collections
Interview
Questions
4:
https://www.youtube.com/watch?v=ZNhT_Z8_q9s
Collections
Interview
Questions
5:
https://www.youtube.com/watch?v=W5c8uXi4qTw
What
is
difference
between
Map
and
SortedMap?
SortedMap
interface
extends
the
Map
interface.
In
addition,
an
implementation
of
SortedMap
interface
maintains
keys
in
a
sorted
order.
Methods
are
available
in
the
interface
to
get
a
ranges
of
values
based
on
their
keys.
public
interface
SortedMap<K,
V>
extends
Map<K,
V>
{
Comparator<?
super
K>
comparator();
SortedMap<K,
V>
subMap(K
fromKey,
K
toKey);
ArrayList
Refer
to
this
video
-
https://www.youtube.com/watch?v=_JTIYhnLemA
for
more
details
about
ArrayList.
Let
us
look
at
a
few
important
interview
questions.
Code
like
below
is
permitted
because
of
auto
boxing.
5
is
auto
boxed
into
Integer
object
and
stored
in
ArrayList.
integers.add(5);//new
Integer(5)
Add method (by default) adds the element at the end of the list.
Other
option
to
sort
collections
is
by
creating
a
separate
class
which
implements
Comparator
interface.
Example
below:
Other
is
to
use
toArray()
function.
Example
below.
This
creates
an
array
of
Objects.
Object[]
numbers1ObjArray
=
numbers1.toArray();
System.out.println(Arrays
.toString(numbers1ObjArray));
//[one,
two,
three,
four]
Collections
Following
set
of
videos
deal
with
collections
interview
questions
in
great
detail
:
Video1,
Video2
&
Video3
Can
you
give
examples
of
classes
that
implement
the
Set
Interface?
HashSet,
LinkedHashSet
and
TreeSet
implement
the
Set
interface.
These
classes
are
described
in
great
detail
in
the
video
-
https://www.youtube.com/watch?v=W5c8uXi4qTw.
What
is
a
HashSet?
HashSet
implements
set
interface.
So,
HashSet
does
not
allow
duplicates.
However,
HashSet
does
not
support
ordering.
The
order
in
which
elements
are
inserted
is
not
maintained.
HashSet
Example
Set<String>
hashset
=
new
HashSet<String>();
hashset.add("Sachin");
System.out.println(hashset);//[Sachin]
hashset.add("Dravid");
System.out.println(hashset);//[Sachin,
Dravid]
Lets
try
to
add
Sachin
to
the
Set
now.
Sachin
is
Duplicate.
So
will
not
be
added.
returns
false.
hashset.add("Sachin");//returns
false
since
element
is
not
added
System.out.println(hashset);//[Sachin,
Dravid]
explained
in
detail
in
this
video
What
is
a
HashMap?
HashMap
implements
Map
interface
there
by
supporting
key
value
pairs.
Lets
look
at
an
example.
HashMap
Example
Map<String,
Cricketer>
hashmap
=
new
HashMap<String,
Cricketer>();
hashmap.put("sachin",
new
Cricketer("Sachin",
14000));
hashmap.put("dravid",
new
Cricketer("Dravid",
12000));
hashmap.put("ponting",
new
Cricketer("Ponting",
11500));
hashmap.put("bradman",
new
Cricketer("Bradman",
9996));
If
existing
key
is
reused,
it
would
replace
existing
value
with
the
new
value
passed
in.
//In
the
example
below,
an
entry
with
key
"ponting"
is
already
present.
//Runs
are
updated
to
11800.
hashmap.put("ponting",
new
Cricketer("Ponting",
11800));
//gets
the
recently
updated
value
System.out.println(hashmap.get("ponting"));//Ponting
11800
We
will
now
insert
a
Cricketer
with
key
dravid.
In
sorted
order,dravid
comes
before
sachin.
So,
the
value
with
key
dravid
is
inserted
at
the
start
of
the
Map.
treemap.put("dravid",
new
Cricketer("Dravid",
12000));
System.out.println(treemap);
//{dravid=Dravid
12000,
sachin=Sachin
14000}
We
will
now
insert
a
Cricketer
with
key
ponting.
In
sorted
order,
ponting
fits
in
between
dravid
and
sachin.
treemap.put("ponting",
new
Cricketer("Ponting",
11500));
System.out.println(treemap);
//{dravid=Dravid
12000,
ponting=Ponting
11500,
sachin=Sachin
14000}
treemap.put("bradman",
new
Cricketer("Bradman",
9996));
System.out.println(treemap);
//{bradman=Bradman
9996,
dravid=Dravid
12000,
ponting=Ponting
11500,
sachin=Sachin
14000}
lowerKey
method
finds
the
highest
key
lower
than
specified
key.
floorKey
method
finds
the
highest
key
lower
than
or
equal
to
specified
key.
Corresponding
methods
for
finding
lowest
key
higher
than
specified
key
are
higher
and
ceiling.
A
few
examples
using
the
Map
created
earlier
below.
//Find
the
highest
key
which
is
lower
than
25
System.out.println(numbersTreeMap.lowerKey(25));//5
//Find
the
highest
key
which
is
lower
than
or
equal
to
25
System.out.println(numbersTreeMap.floorKey(25));//25
//Find
the
lowest
key
higher
than
25
System.out.println(numbersTreeMap.higherKey(25));//35
//Find
the
lowest
key
higher
than
or
equal
to
25
System.out.println(numbersTreeMap.ceilingKey(25));//25
What
is
a
PriorityQueue?
PriorityQueue
implements
the
Queue
interface.
//Using
default
constructor
-
uses
natural
ordering
of
numbers
//Smaller
numbers
have
higher
priority
PriorityQueue<Integer>
priorityQueue
=
new
PriorityQueue<Integer>();
Videos
We
have
created
more
than
20
videos
to
help
you
understand
these
topics
and
become
an
expert
at
them.
You
can
watch
these
videos
for
free
on
YouTube.
Visit
our
website
http://www.JavaInterview.in
for
complete
list
of
videos.
We
answer
the
top
200
frequently
asked
interview
questions
on
the
website.
Register
here
for
more
updates
:
https://feedburner.google.com/fb/a/mailverify?uri=RithusTutorials
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
Java
Interview
:
A
Guide
for
Experienced:
https://www.youtube.com/watch?v=0xcgzUdTO5M
Collections
Interview
Questions
1:
https://www.youtube.com/watch?v=GnR4hCvEIJQ
Collections
Interview
Questions
2:
https://www.youtube.com/watch?v=6dKGpOKAQqs
Collections
Interview
Questions
3:
https://www.youtube.com/watch?v=_JTIYhnLemA
Collections
Interview
Questions
4:
https://www.youtube.com/watch?v=ZNhT_Z8_q9s
Collections
Interview
Questions
5:
https://www.youtube.com/watch?v=W5c8uXi4qTw