You are on page 1of 19

Collections in Java

The Collection in Java is a framework that provides an architecture to store and manipulate
the group of objects.Java Collections can achieve all the operations that you perform on a
data such as searching, sorting, insertion, manipulation, and deletion.
Java Collection means a single unit of objects. Java Collection framework provides many
interfaces (Set, List, Queue, Deque,) and classes (ArrayList,
Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
Collection Interface
The Collection interface is the interface which is implemented by all the classes in the
collection framework. It declares the methods that every collection will have. In other words,
we can say that the Collection interface builds the foundation on which the collection
framework depends.Some of the methods of Collection interface are Boolean add ( Object
obj), Boolean addAll ( Collection c), void clear(), etc. which are implemented by all the
subclasses of Collection interface.

List Interface
List interface is the child interface of Collection interface. It inhibits a list type data structure
in which we can store the ordered collection of objects. It can have duplicate values.
List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.
1. List <data-type> list1= new ArrayList();
2. List <data-type> list2 = new LinkedList();
3. List <data-type> list3 = new Vector();
4. List <data-type> list4 = new Stack();
There are various methods in List interface that can be used to insert, delete, and access the
elements from the list.
ArrayList
The ArrayList class implements the List interface. It uses a dynamic array to store the
duplicate element of different data types. The ArrayList class maintains the insertion order
and is non-synchronized. The elements stored in the ArrayList class can be randomly
accessed. Consider the following example.
1. boolean add(Object o):
 It adds an element of Specific Object type at the end of Arraylist as no index is
mentioned in the method.
 It returns True if element is successfully added, and returns false if it is not.
2. void add(int index, Object element):
 It adds an element of Specific Object type at the specified index of the Arraylist
as given in the argument of the method.
 It does not return anything as its return type is void.
 If in case the index specified is out of range it throws
an IndexOutOfBoundsException.
3. boolean addAll(Collection c):
 This method adds each element of the Specific collection type at the end of the
arraylist.
 It returns True if collection is successfully added, and returns false if it is not.
 If the collection passed in as an argument is null then it throws Null Pointer
Exception
4. boolean addAll(int index, Collection c):
 This methods add each element of the Specific collection type at the specified index
as mentioned in the argument.
 It returns true if collection is successfully added, and returns false if it is not.
 If the collection passed in as an argument is null then it throws Null Pointer
Exception.The program of this method is:
5. void clear():
 This method remove all the elements of the arraylist.
6. Object clone():
 This method returns the exact same copy of the arraylist object.
7. boolean contains(Object element):
 This method returns true if the calling arraylist object contains the specific element
as given in the argument list, otherwise it returns false.
12. Object remove(int index):
 It deletes the element from the given index from the arraylist.
 It returns an Exception IndexOutOfBoundsException, If index specified is out of range.
13.protected void removeRange(int first, int last):
 It deletes the group of elements from the first to last as mentioned in the argument.
 It includes the first index and excludes the last index
14.int size():
 This method returns the size of the arraylist.
 size() methods start count with 1 not 0.

import java.util.*;
public class Demo
{
public static void main(String[] args)
{
ArrayList<String> l1= new ArrayList<String>();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
l1.add("ddd");
l1.add(3,"pppp");
ArrayList<String> l2= new ArrayList<String>();
l2.add("Sangola");
l2.add("college");
l2.addAll(2,l1);
System.out.println("Hello");
System.out.println(l1);
System.out.println("Given element is in the list" l1.contains("college"));
System.out.println("Array list value from l2 beore remove");
for(String s:l2)
{
System.out.println(s);
}
l2.remove(1);
System.out.println("Array list value from l2 after remove");
for(String s:l2)
{
System.out.println(s);
}
System.out.println("Size of Arraylist is "+l2.size());
}

}
import java.util.*;
class demo
{
public static void main(String args[])
{
ArrayList l1= new ArrayList();
l1.add("sangali");
l1.add("kolhapur");
l1.add("satara");
l1.add("pune");
l1.add(11);
l1.add(12.89);
l1.add('a');
System.out.println(l1);

System.out.println(" Display Arraylist element using iterator ");


Iterator it= l1.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println(" Display Arraylist element using foreach loop");
for(Object s:l1)
{
System.out.println(s);
}
System.out.println(" Display Arraylist element using forloop");
for(int i=0;i<l1.size();i++)
{
System.out.println(l1.get(i));
}
System.out.println(" Display Arraylist element using sort method");
// Collections.sort(l1);
for(Object s:l1)
{
System.out.println(s);
}
}
}
LinkedList
LinkedList implements the Collection interface. It uses a doubly linked list internally to store
the elements. It can store the duplicate elements. It maintains the insertion order and is not
synchronized. In LinkedList, the manipulation is fast because no shifting is required.
1. void addFirst(Object o):
This method add the specified element at the first position of the list.
2.void addLast(Object o):
This method add the specified element at the last position of the list
3.Object getFirst():
This method returns the first element in this linked list
4. Object getLast():
This method returns the last element in this linked list
5.Object remove(int index):
It deletes the element from the given index from the Linked List. It returns
an IndexOutOfBoundsException if index specified is out of range
6. Object set(int index, Object element):
This method replaces the content at index given with the element given in argument list
import java.io.*;
import java.util.*;
public class Demo
{

public static void main(String[] args)


{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(11);
al.add(12);
al.add(13);
al.add(14);
System.out.println("Linked List Element using foreach loop");
for(Object s:al)
System.out.println(s);
System.out.println("Linked List Element using iterable");
Iterator<Integer> i= al.iterator();
while(i.hasNext() )
{
System.out.println(i.next());
}
System.out.println("Linked List Element using for loop");
for(int j=0;j<al.size();j++ )
{
System.out.println(al.get(j));
}

System.out.println(" First Element of linked list is " +al.getFirst() );


System.out.println(" Last Element of linked list is " +al.getLast());
al.addFirst(10);
al.addLast(15);
al.set(5, 20);
System.out.println("Linked List Element after add first and add last");
for(Object s:al)
System.out.println(s);

al.remove();
al.remove(3);
al.removeLast();
System.out.println("Linked List Element after remove");
for(Object s:al)
System.out.println(s);
al.clear();
System.out.println("Linked List Element after clear");
for(Object s:al)
System.out.println(s);
}

}
Vector
Vector uses a dynamic array to store the data elements. It is similar to ArrayList. However,
It is synchronized and contains many methods that are not the part of Collection framework.
1. boolean add(E e)
This method adds element at the end of the vector
2. void add(int index, E element)
This method adds an element at specified index and moves the whole elements one step in
forward direction
3. int capacity()
This method gives the capacity of the vector
4. void clear()
This method clears all the elements in the vector
5. clone clone()
This method gives a duplicate copy of the whole vector
6. boolean contains(Object o)
This method tells whether the vector contains the specified element in the vector, it will
return true if that element is present or false if it is not presen
7.Object firstElement()
This method returns the first element at index 0
8.Object lastElement()
This method returns the last element of the vector
9. Object get(int index)
This method retrieves an element at specified index
import java.util.Vector;

public class Demo_vector


{

public static void main(String[] args)


{
Vector<String> v= new Vector<String>();
v.add("aaa");
v.add("bbb");
v.add("ccc");
System.out.println("Value in vector as follow");
for(String s:v)
{
System.out.println(s);
}
v.add(0,"zzz");
System.out.println("Value in vector after add using index 0");
for(String s:v)
{
System.out.println(s);
}
System.out.println("aaa value contain in vector "+v.contains("aaa"));
System.out.println("first value of vector" +v.firstElement());
System.out.println("last value of vector" +v.lastElement());
System.out.println("In vector at 2 index value is "+v.get(2));
System.out.println("In vector at change value at index 2
"+v.set(2,"sangola"));
System.out.println("Vector value aftee set method");
for(String s:v)
{
System.out.println(s);
}
System.out.println("aaa value is vector " +v.contains("aaa"));
Vector<String> v1= new Vector<String>();
v1=(Vector<String>)v.clone();
System.out.println("Vector value in Vector v1");
for(String s:v1)
{
System.out.println(s);
}

}
Stack
The stack is the subclass of Vector. It implements the last-in-first-out data structure, i.e.,
Stack. The stack contains all of the methods of Vector class and also provides its methods like
boolean push(), boolean peek(), boolean push(object o), which defines its properties.
Method Modifier Method Description
and
Type

empty() boolean The method checks the stack is


empty or not.

push(E item) E The method pushes (insert) an


element onto the top of the stack.

pop() E The method removes an element


from the top of the stack and
returns the same element as the
value of that function.

peek() E The method looks at the top


element of the stack without
removing it.

search(Object int The method searches the specified


o) object and returns the position of
the object.

import java.util.Stack;
import java.io.*;
public class Test {

public static void main(String[] args)


{
// TODO Auto-generated method stub
Stack<Integer> s= new Stack<Integer>();
System.out.println(" Initially Capacity of stack is " +s.capacity());
s.add(11);
s.add(12);
s.add(13);
System.out.println("Valu of Stack");
for(int a:s)
{
System.out.println(a);
}
s.add(1,20);
System.out.println("Value of Stack after add using index");
for(int a:s)
{
System.out.println(a);
}
System.out.println("Capacity of stack is " +s.capacity());
System.out.println("11 value is in stack " +s.contains(11));
System.out.println("First element of stack is " +s.firstElement());
System.out.println("Last element of stack is " +s.lastElement());
System.out.println("using get method element of stack is " +s.get(2));
s.push(15);
s.push(16);
System.out.println("Value of Stack after using push");
for(int a:s)
{
System.out.println(a);
}
System.out.println("element of stack is popped " +s.pop());
System.out.println("peek value from stack " +s.peek());
System.out.println("15 value in stack " +s.contains(15));
}

}
Set Interface
Set Interface in Java is present in java.util package. It extends the Collection interface. It
represents the unordered set of elements which doesn't allow us to store the duplicate items.
We can store at most one null value in Set. Set is implemented by HashSet, LinkedHashSet,
and TreeSet.
Set can be instantiated as:
1. Set<data-type> s1 = new HashSet<data-type>();
2. Set<data-type> s2 = new LinkedHashSet<data-type>();
3. Set<data-type> s3 = new TreeSet<data-type>();
HashSet
HashSet class implements Set Interface. It represents the collection that uses a hash table for
storage. Hashing is used to store the elements in the HashSet. It contains unique items.
Methods of Java HashSet class
Various methods of Java HashSet class are as follows:
SN Modifier & Method Description
Type

1) boolean add(E e) It is used to add the specified element to


this set if it is not already present.

2) void clear() It is used to remove all of the elements


from the set.
3) object clone() It is used to return a shallow copy of this
HashSet instance: the elements
themselves are not cloned.

4) boolean contains(Object It is used to return true if this set


o) contains the specified element.

5) boolean isEmpty() It is used to return true if this set


contains no elements.

6) Iterator<E> iterator() It is used to return an iterator over the


elements in this set.

7) boolean remove(Object It is used to remove the specified


o) element from this set if it is present.

8) int size() It is used to return the number of


elements in the set.
import java.util.*;

public class Test {

public static void main(String[] args)


{
HashSet<Integer> h= new HashSet<Integer>();
h.add(11);
h.add(13);
h.add(12);
h.add(11);
System.out.println("Hashset Element ");
for(int a:h)
{
System.out.println(a);
}
System.out.println("11 value is in hashset " +h.contains(11));
HashSet<Integer> h1= new HashSet<Integer>();

System.out.println("Is HashSet is empty " +h.isEmpty());


h1=(HashSet<Integer>)h.clone();
System.out.println("Hashset Element from hash set h1 ");
for(int a:h1)
{
System.out.println(a);
}
Iterator<Integer > i= h.iterator();
System.out.println("Value from hash set using iterator");
while(i.hasNext())
System.out.println(i.next());
h.remove(12);
System.out.println("Hashset Element after remove");
for(int a:h)
{
System.out.println(a);
}

}
LinkedHashSet
LinkedHashSet class represents the LinkedList implementation of Set Interface. It extends
the HashSet class and implements Set interface. Like HashSet, It also contains unique
elements. It maintains the insertion order and permits null elements. Various methods of
Java LinkedHashSet class are as follows:
SN Modifier & Method Description
Type

1) boolean add(E e) It is used to add the specified element to


this set if it is not already present.

2) void clear() It is used to remove all of the elements


from the set.

3) object clone() It is used to return a shallow copy of this


HashSet instance: the elements
themselves are not cloned.

4) boolean contains(Object It is used to return true if this set


o) contains the specified element.

5) boolean isEmpty() It is used to return true if this set


contains no elements.

6) Iterator<E> iterator() It is used to return an iterator over the


elements in this set.

7) boolean remove(Object It is used to remove the specified


o) element from this set if it is present.
8) int size() It is used to return the number of
elements in the set.

import java.util.*;
public class Test {

public static void main(String[] args)


{
LinkedHashSet<String> l=new LinkedHashSet<String>();
l.add("Sangali");
l.add("Satara");
l.add("kolhapur");

System.out.println("Element of Linked hash set is ");


for(String a:l)
{
System.out.println(a);
}
System.out.println("Element is contain in Linked hash set "
+l.contains("Sangali"));
Iterator i= l.iterator();
System.out.println("linked hast set element using iterator");
while(i.hasNext())
{
System.out.println(i.next());
}
System.out.println("Element is removed using remove method
"+l.remove("Satara"));
System.out.println("Size of Linked Hash Set is "+l.size());
l.clear();
System.out.println("Element of Linked hash set after clear ");
for(String a:l)
{
System.out.println(a);
}
LinkedHashSet<String> h1= new LinkedHashSet<String>();
h1=(LinkedHashSet<String>)l.clone();
System.out.println("Element of Linked hash set after clone ");
for(String a:h1)
{
System.out.println(a);
}

}
TreeSet
Java TreeSet class implements the Set interface that uses a tree for storage. Like HashSet,
TreeSet also contains unique elements. However, the access and retrieval time of TreeSet is
quite fast. The elements in TreeSet stored in ascending order.
1. boolean add(Object o):
This methods adds element in the object, which will automatically stores all the elements in
increasing order
2. boolean addAll(Collection c):
This method adds all the elements of one object to another
3. void clear():
This method removes all of the elements from this obje
4. clone clone()
This method gives a duplicate copy of the whole vector
5. boolean contains(Object o)
This method tells whether the vector contains the specified element in the vector, it will
return true if that element is present or false if it is not present
import java.util.*;
public class Test {

public static void main(String[] args)


{
TreeSet<Integer> t= new TreeSet<Integer>();

t.add(11);
t.add(12);
t.add(13);
t.add(14);
System.out.println("TreeSet Element using foreach loop");
for(Object s:t)
System.out.println(s);
System.out.println("TreeSet Element using iterable");
Iterator<Integer> i= t.iterator();
while(i.hasNext() )
{
System.out.println(i.next());
}
t.remove(3);
System.out.println("TreeSet Element after remove");
for(Object s:t)
System.out.println(s);
System.out.println("11 value is present in treeset " +t.contains(11));
t.clear();
System.out.println("TreeSet Element after clear");
for(Object s:t)
System.out.println(s);
}

Map Interface
A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is
known as an entry. A Map contains unique keys.
A Map is useful if you have to search, update or delete elements on the basis of a key.
Java HashMap
Java HashMap class implements the Map interface which allows us to store key and value
pair, where keys should be unique. If you try to insert the duplicate key, it will replace the
element of the corresponding key. It is easy to perform operations using the key index like
updation, deletion, etc. HashMap class is found in the java.util package.
HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It allows us to
store the null elements as well, but there should be only one null key. Since Java 5, it is
denoted as HashMap<K,V>, where K stands for key and V for value. It inherits the
AbstractMap class and implements the Map interface.
Points to remember
o Java HashMap contains values based on the key.
o Java HashMap contains only unique keys.
o Java HashMap may have one null key and multiple null values.
o Java HashMap is non synchronized.
o Java HashMap maintains no order.
o The initial default capacity of Java HashMap class is 16 with a load factor of 0.75
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneabl
e, Serializable
HashMap Methods in JAVA:
1. Object put(Object key, Object value):
This method adds the key value pair to the HashMap object,
2. int size():
This method returns the size of the HashMap
3. void clear():
This method clears all the key value pairs in the HashMap
4. Object clone():
This method returns the exact copy of the HashMap
5. boolean containsKey(Object key):
This method checks the presence of specified key in the HashMap, it will return true if the
key is present and false if the mentioned key is not present
6. boolean containsValue(Object value):
This method checks the presence of specified value in the HashMap, it will return true if the
value is present and false if the mentioned value is not present
7. Set entrySet():
This method returns a Set view of the mappings contained in the HashMap
8.Object get(Object key):
This method returns the value corresponding to the specified key
9.boolean isEmpty():
This method as the name suggests checks whether the HashMap is empty or not, It will
return true if it is empty, or false if it is not empty,
10. Set keySet():
This method returns a Set view of the keys contained in the HashMap
11. putAll(Map m):
This method copies all the key value pair form one hashmap to another
12. Object remove(Object key):
This method as the name specifies removes the key value pair, corresponding to the
mentioned key in the argument list
13. Collection values():
This method returns the collection of all the values in the hashmap

import java.util.HashMap;
import java.util.Map;

public class Test


{
public static void main(String[] args)
{
HashMap<Integer,String> h= new HashMap<Integer,String>();
h.put(1,"pen");
h.put(2, "Pencil");
h.put(3, "Notebook");
h.put(4, "Eraser");
System.out.println("Element of HashMap is ");
for(Map.Entry m:h.entrySet())
{
System.out.println("Key is " +m.getKey()+" Value is " +m.getValue());
}
System.out.println("Conatins key method apply for hashmap is " +h.containsKey(1));
System.out.println("Conatins Values method apply for hashmap is "
+h.containsValue("Pen"));
System.out.println("Entry key method apply for hashmap is " +h.entrySet());
System.out.println("get method apply for hashmap is " +h.get(2));
System.out.println("keyset key method apply for hashmap is " +h.keySet());

HashMap<Integer,String> h1= new HashMap<Integer,String>();


h1.putAll(h);
System.out.println("using putall method apply for hashmap1 is " +h1.keySet());
System.out.println("Element of HashMap1 is ");
h1.remove(1);
for(Map.Entry m:h1.entrySet())
{
System.out.println("Key is " +m.getKey()+" Value is " +m.getValue());
}
h1.remove(1);
System.out.println("values method apply for hashmap1 is " +h1.values());
}
}
Java TreeMap class
Java TreeMap class is a red-black tree based implementation. It provides an efficient means
of storing key-value pairs in sorted order.
The important points about Java TreeMap class are:
o Java TreeMap contains values based on the key. It implements the NavigableMap
interface and extends AbstractMap class.
o Java TreeMap contains only unique elements.
o Java TreeMap cannot have a null key but can have multiple null values.
o Java TreeMap is non synchronized.
o Java TreeMap maintains ascending order.
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>,
1. Object put(Object key, Object value):
This method adds the key value pair to the TreeMap object,
2. int size():
This method returns the size of the TreeMap
3. void clear():
This method clears all the key value pairs in the TreeMap
4. Object clone():
This method returns the exact copy of the TreeMap
5. boolean containsKey(Object key):
This method checks the presence of specified key in the TreeMap, it will return true if the
key is present and false if the mentioned key is not present
6. boolean containsValue(Object value):
This method checks the presence of specified value in the TreeMap, it will return true if the
value is present and false if the mentioned value is not present
7. Set entrySet():
This method returns a Set view of the mappings contained in the TreeMap
8.Object get(Object key):
This method returns the value corresponding to the specified key
9.boolean isEmpty():
This method as the name suggests checks whether the TreeMap is empty or not, It will return
true if it is empty, or false if it is not empty,
10. Set keySet():
This method returns a Set view of the keys contained in the TreeMap
11. putAll(Map m):
This method copies all the key value pair form one TreeMap to another
12. Object remove(Object key):
This method as the name specifies removes the key value pair, corresponding to the
mentioned key in the argument list
13. Collection values():
This method returns the collection of all the values in the TreeMap

import java.util.TreeMap;
import java.util.Map;

public class Test


{
public static void main(String[] args)
{
TreeMap<Integer,String> t= new TreeMap<Integer,String>();
t.put(1,"pen");
t.put(2, "Pencil");
t.put(3, "Notebook");
t.put(4, "Eraser");
System.out.println("Element of TreeMap is ");
for(Map.Entry m:t.entrySet())
{
System.out.println("Key is " +m.getKey()+" Value is " +m.getValue());
}
System.out.println("Conatins key method apply for TreeMap is " +t.containsKey(1));
System.out.println("Conatins Values method apply for TreeMap is "+t.containsValue("Pen"));
System.out.println("Entry key method apply for TreeMap is " +t.entrySet());
System.out.println("get method apply for TreeMap is " +t.get(2));
System.out.println("keyset key method apply for TreeMap is " +t.keySet());

TreeMap<Integer,String> t1= new TreeMap<Integer,String>();


t1.putAll(t);
System.out.println("using putall mettod apply for TreeMap1 is " +t1.keySet());
System.out.println("Element of TreeMap1 is ");
t1.remove(1);
for(Map.Entry m:t1.entrySet())
{
System.out.println("Key is " +m.getKey()+" Value is " +m.getValue());
}
t1.remove(1);
System.out.println("values method apply for TreeMap1 is " +t1.values());
//t1.clear();
TreeMap <Integer,String> t2= new TreeMap<Integer,String>();
t2=(TreeMap<Integer, String>)t1.clone();
System.out.println("Element of TreeMap2 is ");
for(Map.Entry m:t2.entrySet())
{
System.out.println("Key is " +m.getKey()+" Value is " +m.getValue());
}
}
}
Java LinkedHashMap class
Java LinkedHashMap class is Hashtable and Linked list implementation of the Map interface,
with predictable iteration order. It inherits HashMap class and implements the Map
interface.
Points to remember
o Java LinkedHashMap contains values based on the key.
o Java LinkedHashMap contains unique elements.
o Java LinkedHashMap may have one null key and multiple null values.
o Java LinkedHashMap is non synchronized.
o Java LinkedHashMap maintains insertion order.
o The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
LinkedHashMap class declaration
Let's see the declaration for java.util.LinkedHashMap class.
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
1. Object put(Object key, Object value):
This method adds the key value pair to the LinkedHashMap object,
2. int size():
This method returns the size of the LinkedHashMap
3. void clear():
This method clears all the key value pairs in the LinkedHashMap
4. Object clone():
This method returns the exact copy of the LinkedHashMap
5. boolean containsKey(Object key)
This methods checks whether the key given in the argument list is present or not in the
LinkedHashMap entries
6. boolean containsValue(Object value)
This methods checks whether the value given in the argument list is present or not in the
LinkedHashMap entries
7. Object get(Object key)
This method returns the value corresponding to the given key in the argument list
8. Object remove(Object key):
This method as the name specifies removes the key value pair, corresponding to the
mentioned key in the argument list

import java.util.LinkedHashMap;
import java.util.Map;

public class Test {

public static void main(String[] args)


{
LinkedHashMap<Integer,String> l= new LinkedHashMap<Integer,String>();
l.put(1,"Pen");
l.put(2,"Pencile");
l.put(3,"Eraser");
l.put(4,"Book");
System.out.println("Element from Linkedhash Map is");
for(Map.Entry<Integer,String> s:l.entrySet())
{
System.out.println("key is "+s.getKey()+" Value is "+s.getValue());
}
System.out.println("Size of LinkedHash Map is " +l.size());
LinkedHashMap<Integer,String> l1= new LinkedHashMap<Integer,String>();
l1=(LinkedHashMap<Integer,String>)l.clone();
System.out.println("Element from LinkedhashMap 1 is");
for(Map.Entry<Integer,String> s:l.entrySet())
{
System.out.println("key is "+s.getKey()+" Value is "+s.getValue());
}
System.out.println("Value is remove from linkedHashMap1 is "+l1.remove(3));
System.out.println("Value is in linkedHashMap is "+l.containsKey(11));
System.out.println("Value is in linkedHashMap is "+l.containsValue("Pen"));
System.out.println("Value is in linkedHashMap is "+l.entrySet());
l.replace(1,"Marker");
System.out.println("Element from Linkedhash Map is after replace method");
for(Map.Entry<Integer,String> s:l.entrySet())
{
System.out.println("key is "+s.getKey()+" Value is "+s.getValue());
}
l.clear();
}

You might also like