Collection Framework
Characteristics of CFW or Advantages?
1. CFW provides high
performance to java applications
2. CFW provides
adaptability
3. CFW provides
Extendibility
4.CFC provides Some
data structures like stacks, ques,linkedlist etc…………..
It organizes the data
either row or column but not both. or data is not organizing in the form of
(Key,Value)
1D/single CFW interfaces?
1.java .
util.Collection;
2.java
.util.List;
3.java.util.Set;
4.java.util.SortedSet;
In which package CFW API is available?
Java.util.*;
What is CFW variable?
The objects of Collection, List ,Set and SortedSet
knowns as CFW variable
What is the diff b/w collection and
Collections?
Collection is an
interface.Collections is a class.
Which of collection class will not
allow duplicate values?
Set of collection class
will not allow duplicate values and also SortedSet of collection class
But Collection of
collection class and List of collection class allows duplicate values
What are
the Limitations of Arrays?
An array is an
indexed collection of fixed number of homogeneous data elements.
The
main limitations of Object Arrays are
1)
Arrays are fixed in size i.e once we created an array there is no
chance of increasing or
decreasing
it’s size based on our requirement.
2)
Arrays can hold only homogeneous data elements.
Ex:-
Student
[] s = new Student [600];
s[0] =
new Student;----------valid
s[1] =
new Integer(10); X
s[2] =
"raju"; X
We can
resolve this problem by using object Arrays.
Object
[] o = new Object [600];
o[0] =
new Student;----------------valid
o[1] =
new Integer(10);--------------valid
o[2] =
"raju";-----------------valid
i.e By using Object
Arrays we can hold heterogeneous data elements.
To
resolve the above problems of arrays, Sun people has introduced ‘collections
concept’
Collections
are grow able in nature i.e based on requirement we can increase or decrease
the size.
Collections
can hold heterogeneous data elements also.
Every
collection class has implemented based on some data structure
Hence
for every requirement ready made method support is possible.
We can
represent all the elements in some sorting order. We can prevent duplicate
object insertion
by
default.
What is difference b/w Arrays and Cs?
Arrays Cs
1.Arrays are in fixed
size 1.Collections
are not fixed in size
2.memoery allocations
are not good 2.memory allocations
are good
3.Arrays can only hold homogeneous 3.Collections can hold heterogeneous data
elements data
elements
4.Arrays doesn’t having
any data 4.Every collection
class has built by using
Strcture
implementation some data
structure
5.Arrays
can hold objects and
5.collections can hold only objects but
Primitives
not primitives
What is Collections Frame
Work?
It
defines group of classes and interfaces which can be used for representing a
collection of Objects as single entity.
Java: Collection Frame Work
C++: STL(standard Template Library) container
What are the 7 key
Interfaces of collection Frame work?
1. Collection
2. List
3. Set
4. SortedSet
5. Queue
6. Map
7. Sorted Map
What is a Collection interface?
Ø This
interface is the root interface for entire collection framework.
Ø This
interface can be used for representing a group of objects as single entity.
Ø This
interface defines the most common general methods which can be applicable for
any collection object.
Ø There
is no concrete class which implements collection interface directly
Ø It allows ordered set of objects.
Ø It displays the data Random order
Ø Retrieve the data only in forward direction
What is the Difference
Between Collection and Collections?
Collection
is an interface available in java.util package for representing a
group of objects as single entity.
Collections
is an utility class available in java.util package and defines
several utility methods for collection implemented class object
What is the List interface?
Ø This
can be used for representing a group of individual objects as a single entity
where insertion order is preserved and duplicate objects allowed.
Ø
This is child interface of
collection.
Ø The
following classes implemented List interface directly.
Array List,
Linked List, Vector and Stack
Ø It displays the data sequential order
Ø Retrieve the data in forward direction and backsword
direction
What is set interface?
Ø
This can be used for representing
a group of individual objects where duplicate objects are not allowed and
insertion operation is not preserved.
Ø This
is the child interface of collection
Ø hashset
and linkedHashSet are the classes which
implements Set interface directly.
Ø It displays the data Random order
Ø Retrieve the data only in forward direction
What is Sorted Set interface?
Ø This
can be used for representing a group of individual and unique objects. Where
all the objects are inserted in some sorting order.
Ø It is the child interface of Set interface
Ø TreeSet
is the implemented class of SortedSet
Ø Retrieve the data only in forward direction and backward
direction
What is Queue interface?
Ø It
is the child interface of collection it has introduced in 1.5 versions this
interface can be used for representing a group of individual objects prior to
processing.
Ø All
the above interfaces(collection, List, Set, SortedSet, Queue) can be used for
representing a group of individual objects.
Ø If
u want to represent a group of objects as key value pair than we can’t use
above interfaces.
Ø To
handle this requirement sun people has introduced map interface.
What is Map
Interface?
Ø This
can be used for representing a group of objects as key value pairs. Both key
and value are Objects only.
Student Name StudentRollNo
PhoneNumber contactdetails
Word
meaning
IP Address Domain-name
Ø Map
interface is not child interface of collection
What is SortedMap interface?
Ø This can be used
for representing a group of objects as key value pairs where all the entries
are arranged in some sorting order of keys.
Ø TreeMap is the
class which implements SortedMap interface.
Ø The following
are legacy Characters in collection Frame Work.
1)
vector.-----------concrete class
2)
stack------------concrete class
3)
Hashtable-----concrete class
4)
properties------concreet class
5)
Dictionary--------abstract class
6)
Enumeration-------interface
What is unidirectional and bidirectional objects?
Collection and Set organizes data only
forward direction but not backward direction so these are called unidirectional
objects
List and SortedSet organizes data both
forward direction backward direction so these are called bidirectional objects
How do you add an element 60 at the end of List interface
object?
List l=new List();
l.add(l.size(),newInteger(60));
Collection
Interface methods
This can be used for representing
a group of individual objects as a single entity. This interface defines the
most common general methods.
Which can be applicable for any collection implemented class
1)boolean add(Object obj)
This method is used to add any
type of value(like int ,string….) to the
collection
2) boolean addAll(Collection c)
It is used to add the content of
one collection variable to the another collection variable.It returns true
doesn’t add the content of another variable.It returns false adds the content
of one collection to another collection
3) boolean remove(Object obj)
4) boolean removeAll(Collection c)
(Removes particular group of
objects.)
5) boolean retainAll(Collection c)
(Removes all the elements except
those present in ‘c’)
6) void clear()
(Removies all objects.)
7) boolean contains(Object obj)
Checks object is there or not.It
returns true the element is found in the collection.It returns false the
element is not present in Collection
8) boolean contains (Collection c)
9) boolean isEmpty()
This is used to find a collection
variable having entries or not.It returns true any collection doesn’t have entries.It returns
false any collection variable contains some entries
10) int size()
This is used to find the size of
any collection
11) Object [] toArray()
It is used to retrieve the data from the any
collection framework variable in the form an array of objects of
java.lang.Object
12) Iterator iterator()
This method is used to
retrieve object one by one in any collection variable.It contains the next() and
hasNext() methods
What
is List interface?
This can be used for representing
a group of individual objects where insertion order is preserved and duplicate
objects are allowed. By means of index we can preserve insertion order and we
can differentiate duplicate objects.
List
Interface defines the following methods
1) Boolean add(Object obj)
Collection (1.2)
2)
boolean add(int index, Object obj)
3)
boolean addAll(Collection c)
4)
boolean addAll(int index, Collection c)
5)
boolean remove(Object obj)
6)
Object remove(int index)
7)
Object set(int index, Object new)
Old
object. It replaces with the existing objected located at specified index with
the new
object.
And it returns object.
8)
Object get(int index)
9) int
indexOf(Object obj)
10) int
lastIndexOf(Object obj)
11)
ListIterator listIterator()
What is ArrayList?
Ø The underlying data Structure for ArrayList() is
resizable Array or “Growable Array”.
Ø Duplicate
objects are allowed.
Ø Insertion
order is preserved.
Ø Heterogeneous
objects are allowed.
Ø ‘null’
insertion is possible.
Constructors of Array List
ArrayList
l = new ArrayList()
Creates
an empty ArrayList object with default intitial capacity 10.
When
ever ArrayList reaches its max capacity a new ArrayList Object will be created
with new capacity.
Capacity=(current
capacity*3/2)+1;
ArrayList
l = new ArrayList(int initial capacity)
Creates
an empty ArrayList Object with the specified initial capacity.
ArrayList
l = new ArrayList(Collection c)
For inter conversion
between collection objects.
What is the default initial capacity
of ArrayList?
The default initial
capacity of ArrayList is 10.
What are the common interfaces
implemented by every collection?
Usually
the collection objects can be used for data transport purpose and hence every
collection implemented class already implemented serializable and cloneable
interfaces.
What is the limitation ArrayList?
ArrayList
is not recommended if the frequent operation is insertion or deletion in the
middle.
To
handle this requirement we should go for linked list.
What is LinkedList?
Ø The
underlying Data Structure for linked list is doubly linked list.
Ø Duplicate
objects are allowed.
Ø Insertion
order is preserved.
Ø Heterogeneous
objects are allowed.
Ø ‘null’
insertion is possible.
Ø Implements
List, Queue, serializable, clonealbe Interfaces But not RandomAccess.
What is the use of LinkedList compared
to ArrayList?
Ø LinkedList
is the best choice if our frequent operation is insertion or deletion in the
middle(no shift operations are required)
Ø LinkedList
is the worst choice if our frequent operation is retrieval operation.
Ø LinkedList
class usually used for implementing stacks and Queues to provide support for
this requirement,
Ø LinkedList
class contains the following specific methods.
1. void addFirst(Object obj)
2. void addLast(Object obj)
3. Object removeFirst()
4. Object removeLast()
5. Object getFirst()
6.
Object getLast()
What are the constructers of LinkedList?
1. LinkedList l = new
LinkedList()
2.
LinkedList l = new LinkedList(Collection c)
What is Vector class?
Ø The
underlying Data structure for the vector is resizable array or growable array.
Ø Insertion
order is preserved.
Ø Duplicate
objects are allowed.
Ø ‘null’
insertion is possible.
Ø Heterogeneous
objects are allowed.
Ø Best
choice if the frequent operation is retrieval.
Worst
choice if the frequent operation is insertion or deletion in the middle.
Vector class
implemented serializable, cloneable and RandomAccess Interfaces
What is the Difference between vector and ArrayList?
Vector
1) All
methods of vector are synchronized
2)
vector object is thread safe
3)
performance is low
4) 1.0
version(Legacy)
5.)Worst
choice if the frequent operation is insertion or deletion in the middle.
ArrayList
1) no method is
synchronized
2) ArrayList object is
not thread safe by default
3) performance is High
4) 1.2 version(non
Legacy)
5.)ArrayList is the
worst choice if u want to perform insertion or deletion in the middle.
What are the methods in Vector class?
For adding objects.
add(Object
obj)
add(int
index, Object obj)
addElement(Object
obj)
For removing Objects
remove(Object
obj)
removeElement
(Object obj)
remove(int
index)
removeElementAt(int
index)
clear()
removeAllElements
()
For Accessing Elements
Object
get(int index)
Object
elementAt(int index)
Object
firstElement();
Object
lastElement();
Other
Methods
int
size();
int
capacity();
enumeration elements();
constructors
Vector
v = new Vector();
Creates
an empty vector object with default initial capacity 10, when ever vector
reaches it’s max capacity a new vector object will be created with.
New capacity=current
capacity*2;
What is mean by stack?
It is
the child class of Vector contains only one constructor.
Stack s
= new Stack();
Methods
Object push(Object obj);
For
inserting an object to the stack
Object pop();
It
removes and returns top of the stack.
Object peak();
Returns
the top of the stack with out removal of object.
int search(Object obj);
If the
specified object is available it returns its offset from top of the stack
If the
object is not available then it returns -1.
boolean empty();
returns true if the
stack is empty otherwise false.
What are the cursors available in
collection framework?
Or
How are the retrieval objects in
collection framework?
From
the collection object to retrieve object we can use the following 3 cursors.
1.
Enumeration
2. Interator
3. ListIterator
What is Enumaration (1.0)?
It is a predefined
interface the functionality of this interface to retrieve the data from only in
legacy collection classes in forward direction only
It is similar to
iterator interface. It contains two methods
1.Boolean hasMoreElements()
2. Object NextElement ()
Limitations of Enumeration
1. It
is applicable only for legacy classes and it is not a universal cursor.
2.
While iterating the elements by using enumeration we can perform only read
operation and we can’t perform any modify/removal operations.
To
overcome these problems we should go for Iterator
What is an Iterator(1.2)?
It
is a predefined interface the functionality of this interface also to restive
the data from any collection framework variable only in forward direction
That’s
why it called universal cursor. it contains three methods. to overcome the
problems of Enumaration interface we can use Iterator interface
Limitations of Iterator
1. Enumeration
and Iterator are single directional cursors. They can always move to words
forward direction only.
2. By
using Iterator we can perform read and remove operations. And we can’t perform
any replace or addition of new objects
To
over come these limitations we should go for list Iterator interface ().
What is ListIterator(1.2)?
It
is predefined interface the functionality of this interface is also to retrieve
the data from any collection both forward and backward directions
To
overcome the problems of Enumaration and Iterator we can use ListIterator.By
using this we can add, remove, replace and read the data.it contains nine
methods
1. boolean hasNext();
2. boolean
hasPrevious();
3. Object next();
4. Object previous();
5. int nextIndex();
If there is no next
element it returns the size of the list
6. int previousIndex();
If there is no previous
element it returns -1
7. void remove();
8. void set(Object new)
9. void set(Object new)
What is the difference b/w Enuaration
and Iterator?
Iterator
1. Iterator object is
not synchronized
2. Iterator is
applicable for any collection (legacy and non legacy)
3. By sing this we can
read and remove the objects
4. It is a universal
cursor object
Enumaration
1. Enumaration object
is synchronized
2. It is applicable for
any non legacy collection classes
3. By sing this we can
read the objects
4. It is not a
universal cursor object
What are the legacy collection classes
and interfaces present in collection framework?
The following are the
legacy collection classes present in collection framework.
1. Vector
2. Stack
3. HashTable
4. Dictionary
5. Properties
The following is the
legacy collection interfaces present in collection framework.
1. Enumaration
What is the diff b/w
new and legacy collection framework?
The
major difference b/w these two is new collection framework classes and
interfaces belongs to non-synchronized (provides concurrency) where as legacy
collection framework classes and interfaces are synchronized(provides sequential)
What is Set Interface?
Ø This
can be used for representing a group of Individual objects where insertion order
is not preserved and duplicate objects are not allowed.
Ø Set
interface is child interface of Collection.
Ø This interface doesn’t contain any new method and we
have to use only collection Interface methods.
What is HashSet?
Ø The
underlying Data Structure for HashSet is Hashtable.
Ø Insertion
order is not preserved and it is based on has code of the Object.
Ø Duplicate
objects are not allowed. Violation leads to no CompileTimeError or
RuntimeError, add method simply returns false.
Ø ‘null’
insertion is possible.
Ø Heterogeneous
objects are allowed.
Ø HashSet is the best choice if the frequent operation
is Search Operation
Constructors
1. HashSet h = new HashSet ()
Creates an empty HashSet
object with default initial value 16 and default fill ratio 0.75
2. HashSet h = new HashSet(int
initialcapacity)
3. HashSet h = new HashSet(int
initialCapacity, float fillratio)
Here fillratio is 0 or 1.
4. HashSet h =
new HashSet (Collection)
If we are trying to insert duplicate
values in Set what will happen?
Ø Duplicate
objects are not allowed. Violation leads to no CompileTimeError or
RuntimeError, add method simply returns false.
What is LinkedHashSet?
It is the child
class of HashSet. It is Exactly similar to HashSet. Except the following
differences.
HashSet
1. Its equivalent data
structure is HashTable
2. Displays the data in
random order
3. Introduced in 1.2
version
LinkedHashSet
1. Its equivalent data
structure is HashTable and LinkedList
2. Displays the data in
order
3. Introduced in 1.4
version
What is SortedSet?
Ø This can be used
for representing a group of individual objects where duplicate objects are not
allowed.
Ø Insertion order
is not preserved but all the elements are inserted according to some sorting
order of elements. The sorting order may be default natural sorting order or
customized sorting order.
SortedSet Interface
contains the following more specific methods
1.
Object
first()
Returns first element in the
SortedSet
2.
Object
last()
3.
SortedSet
headSet(Object obj)
Returns the SortedSet contains
the elements which are less than object obj.
4.
SortedSet
tailSet(Object obj)
Returns the SortedSet whose
elements are greater than or equal to object obj
5.
SortedSet
subSet(Object obj1, Object obj2)
Returns the SortedSet whose
elements are >= obj1 but < Obj2
6.
Comparator
comparator();
Returns the comparator object
describes the underlying sorting technique.
If you are using
default(Assending) natural sorting order it returns null.
Observe the following Example.
Ex:
100
120
130
140
150
160
|
first() 100
last() 160
headSet(140) {100,120,130}
tailSet(140) {140,150,160}
subset (130,150){130,140}
comparator () null
What is TreeSet?
Ø The underlying
Data structure for the TreeSet is Balanced tree.
Ø Duplicate
objects are not allowed. If we are trying to insert duplicate object we won’t
get any compile time error or Run time error, add method simply returns false.
Ø Insertion order
is not preserved but all the elements are inserted according to some sorting
order.
Ø Heterogeneous objects
are not allowed, violation leads to Run time error saying class cast Exception
Constructors
1. TreeSet t = new TreeSet();
Creates an empty TreeSet Object
where the sorting order is default natural sorting
order.
2. TreeSet t= new
TreeSet(Comparator c)
Creates an empty TreeSet Object
where the Sorting order is specified by comparator object.
3. TreeSet t= new
TreeSet(Collection c)
4. TreeSet t= new
TreeSet(SortedSet s)
Creates TressSet Object for a
given SortedSet.
What is Comparable Interface?
It is available in
java.lang package. This interface contains only one method compareTo()
public int
compareTo(Object obj)
obj1.compareTo(obj2)
Note:
while Inserting the objects into the TreeSet JVM
internally uses compareTo() method if we are depending on natural sorting
order.
Sometimes we have to
define our own customized sorting order, then we should go for comparator
Interface.
What is Comparator?
It is an interface present in
java.lang.util. the purpose of this is it will sort the objects ascending and
descending order
.
By using comparator
object we can define our own customized sorting.
This interface defines the following 2
methods.
1) public int
compare(Object obj1, Object obj2)
returns –ve if obj1 has
to come before obj2
+ve if obj1 has to come
after obj2
0 if obj1 and obj2 are
equal.
2) public boolean
equals(Object obj)
When ever we are
implementing comparator interface compulsory we should provide implementation
for compare method. Implementing equals method is optional because it is
already available from object class through inheritance
What is Map interface?
The Map
interface provides three collection views, which allow a map's contents to be
viewed as a set of keys, collection of values, or set of key-value mappings.
The order of a map is defined as the order in
which the iterators on the map's collection views return their elements.
Map implemented by HashMap and TreeMap
What
are the classes we have to implement the map interface?
A
number of classes implement the Map interface, including HashMap, TreeMap,
LinkedHashMap,WeakHashMap, ConcurrentHashMap, and Properties. The most
generally useful class is HashMap.
Map interface methods
Here are some of the most useful
Map methods.
Adding key-value pairs to a map
Object obj = m.put(key, val) Creates mapping from key
to val. It returns the
previous value (or null)associated with the key.
m.putAll(map2) adds all key-value entries from
another map, map2.
Removing key-value pairs from a map
m.clear()
Removes all elements from a Map
Object obj = m.remove(key) Deletes mapping from key to anything. Returns previous value
(or null) associated with the key.
Retrieving information from the map
Boolean b = m.containsKey(key) Returns true if m contains a key key
Boolean
b =
m.containsValue (Val) Returns true if m contains Val as one of the values
Object
obj =
m.get(key) Returns value corresponding to key, or null if there is no mapping.
If null has been stored as a value, use containsKey to see if there is a mapping.
Boolean
b =
m.isEmpty() Returns true
if m contains no mappings.
Integer
i =
m.size() Returns number
of mappings in m.
Retrieving all keys, values, or
key-value pairs (necessary for iteration)
Set set
= m.entrySet () Returns set of
Map.Entry values for all mappings.
Set
set =
m.keySet () Returns Set
of keys.
col = m.values() Returns a
Collection view of the values in m.
Map.Entry interface methods
Each element is a map has a key and value. Each key-value pair is saved in a java.util.Map.Entry
object.
A set of these map entries can be
obtained by calling a map's entrySet() method. Iterating over a map is done by
iterating over this set.
Assume in the following table
that me is a Map. Entry object.
Object
obj =
me.getKey() Returns the
key from the pair.
Object
obj =
me.getValue (key) Returns the value from the Map
pair.
Object
obj =
me.setValue (val) This is an optional operation and may not be
supported by all Map.Entry objects. Sets the value of the pair, which modifies
the Map which it belongs to. Returns the orginal value.
The interface methods can be
broken down into three sets of operations: altering, querying and providing Alternative views
The alteration operation allows you to add and remove key-value
pairs from the map. Both the key and Value can be null. However you should not
add a Map to itself as a key or value.
Object put (Object key,
Object value)
Object remove(Object key)
void putAll(Map t)
void clear()
The query operations allow
you to check on the contents of the map
Object get(Object key)
boolean containsKey(Object
key)
boolean containsValue(Object
value)
int size()
Boolean isEmpty()
The set methods allow you to work with the group of keys or values as a
collection
Set keySet()
Collection values()
Set entrySet()
What is HashMap?
HashMap
is not sorted or ordered. If you just need a Map, and you don’t care about the
order of the elements while iterating through it, you can use a HashMap. That
keeps it simple.
The
values can be null. But the key should ne unique, so you can have one null
value for key. (Allows only one null key).
HashMap class
constructors
In addition to implemented the Map interface methods, HashMap has
the following constructors.
HashMap map= new HashMap ()
Creates a new HashMap with default initial
capacity 16 and load factor 0.75.
HashMap map= new HashMap (initialCapacity)
Creates a new HashMap with the
specified initial int capacity.
HashMap map = new HashMap(initialCapacity,loadFactor)
Creates a new HashMap with the
specified capacity which will not exceed a specified (float) load factor.
HashMap map = new HashMap(mp)
Creates a new HashMap with elements from the Map mp
HashMap is
implemented with a hash table. Access time is O(1). Entries are unsorted
What is LinkedHashMap?
LinkedHashMap will keep the order
in which the elements are inserted into it. If you will be adding and removing
elements a lot, it will be better to use HashMap, because LinkedHashMap will be
slower to dothose operations. But, you can iterate faster using LinkedHashMap.
So, if you will be iterating heavily, it may be a good idea to use this.
What is WeakHashMap?
A map of weak keys that
allow objects referred to by the map to be released; designed to solve certain
types of problems. If no references outside the map are held to a particular
key, it may be garbage collected.
What is IdentityHashMap?
A hash map that uses == instead
of equals ( ) to compare keys. Only for solving special types of problems
not for general use.
What is Hashtable?
Hashtable is almost the same as
HashMap. The main differences are:
1. Hashtable does not let you
have null value for key.
2. The key methods of Hashtable
are synchronized. So, they may take a longer time to execute, compared to HashMap’s
methods.
What is SortedMap?
If you have a SortedMap (of
which TreeMap is the only one available), the keys are guaranteed to be
in sorted order, which allows additional functionality to be provided with
these methods in the SortedMap interface:
Comparator
comparator( ): Produces
the comparator used for this Map, or null for natural ordering.
Object firstKey(
): Produces
the lowest key.
Object lastKey(
): Produces
the highest key.
SortedMap
subMap(fromKey, toKey): Produces a view of this Map with keys from fromKey,
inclusive,to toKey, exclusive.
SortedMap headMap
(toKey): Produces
a view of this Map with keys less than toKey.SortedMap
tailMap(fromKey): Produces a view of this Map with keys greater than
or equal to fromKey.
SortedMap interface methods
The SortedMap interface is used by TreeMap and adds additional
methods to reflect that a TreeMap is sorted.
comp = comparator()
Returns Comparator used to compare keys. null if natural ordering used(eg,
String).
obj = firstKey() Key
of first (in sorted order) element.
obj = lastKey() Key
of last (in sorted order) element.
smp = headMap(obj) Returns SortedMap of all elements less than obj.
smp = tailMap(obj) Returns SortedMap of all elements greater than or equal to obj.
smp = subMap(fromKey,toKey)
Returns SortedMap of all elements greater than or equal to fromKey and
less than toKey.
What is TreeMap?
TreeMap is a sorted Map and will
be sorted by the natural order of the elements. If you want, you can define a
custom sort order by means of a Comparator passed to the constructor. So, when
you need a custom sort order, it is clear which class you should be using!
TreeMap class constructors
TreeMap implements the Map and SortedMap interface methods. In constrast to HashMap, TreeMap
keeps the balanced binary tree in sorted order by key.
If the key has a natural order (eg, String)
this is ok, but often you will supply a Comparator object that tells how two
keys compare. It has the following constructors.
SortedMap tmap = new TreeMap()
Creates new TreeMap. Keys sorted
by natural order.
SortedMap tmap = new TreeMap (comp)
Creates new TreeMap using Comparator comp to sort keys.
SortedMap tmap = new TreeMap (mp)
new TreeMap from Map mp using natural ordering.
SortedMap tmap = new TreeMap(smp)
Creates new TreeMap from SortedMap smp using key ordering from
smp.
j
.TreeMap is
implemented as a balanced binary tree. Access time is O(log N). Entries are
sorted.
What is the internal concept of
ArrayList?
Arrays, type single
dimensional
What is the internal design of
HashMap?
Arrays
what is the internal design of Hash
Set
HashMap