Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to pass LinkedHashMap to some other collection as a parameter using collections in java

how to pass LinkedHashMap to some other collection as a parameter using collections in java

Previous Home Next

 

In this example we will be passing the LinkedHashMap as a parameter to some other collection that extends the Map interface.

For creating a LinkedHashMap first of all we have to import the java.util package in which the java.util.LinkedHashMap class is contained. In this example we have created a class named CollectionExample in which we have created a LinkedHashMap and added some elements to it
 In this example, for adding the contents of the LinkedHashMap to some other Map collection we have used the putAll(Map map) method on the object of the  Map collection. The general form of the putAll() method is,

void putAll(Map map)
The above method copies all of the mappings from the specified map to the current Map. In case of the same keys, the value at the key will get replaced.

In the given example we have created a LinkedHashMap and added some elements to it by using the put(Object key, Object value) method on its object.
We have also created a Hashtable named ht and added add the elements of the LinkedHashMap to it by using the putAll(Map map) method on its object and passing LinkedHashMap object as an argument to it.
Then again, we have added all the elements of the Hashtable to the LinkedHashMap by using the putAll(Map map) method on it.
Finally we have displayed the elements of the Hashtable and LinkedHashMap.

 
package r4r.co.in;
import java.util.*;


public class CollectionExample {

/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
LinkedHashMap<Integer, Integer> lhm=new LinkedHashMap<Integer, Integer>(); // creating a new LinkedHashMap
lhm.put(1, new Integer(7)); // adding elements to this LinkedHashMap
lhm.put(2, new Integer(5));
lhm.put(3, new Integer(8));
lhm.put(4, new Integer(2));
lhm.put(5, new Integer(9));
lhm.put(6, new Integer(52));
System.out.println("The contents of the LinkedHashMap are as follows: ");
Set set=lhm.entrySet(); // creating a set of the contents of the LinkedHashMap
Iterator itr=set.iterator(); // invoking the iterator on the set created
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using the Map.Entry interface
Integer key = map.getKey(); // getting the keys
Integer value = map.getValue(); // getting the values
System.out.print("KEY: "+key); // printing the keys
System.out.print(" "+"VALUE: "+value); // printing the values
System.out.println();
}
lhm.remove(3); // removing the key 3 element
lhm.remove(2); // removing the key 2 element
System.out.println("The contents of the LinkedHashMap after removal of items are: ");
set=lhm.entrySet();
itr=set.iterator();
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next();
Integer key = map.getKey();
Integer value = map.getValue();
System.out.print("KEY: "+key);
System.out.print(" "+"VALUE: "+value);
System.out.println();
}
System.out.println(lhm.containsKey(2)); // checking whether the LinkedHashMap contains 2 as a key or not
System.out.println(lhm.containsValue(8)); // checking whether the LinkedHashMap contains 8 as an element or not

Hashtable<Integer, Integer> ht= new Hashtable<Integer, Integer>(); // creating a new Hashtable ht
ht.putAll(lhm); // adding the LinkedHashMap lhm to this Hashtable
ht.put(3, new Integer(17)); // adding more elements to the Hashtable
ht.put(2, new Integer(70));
System.out.println("The contents of the Hashtable is as follows: ");
for(int i=1; i<=ht.size(); i++)
{
System.out.println("KEY = "+i+" VALUE = "+ht.get(i));
}
lhm.putAll(ht); // adding the contents of the Hashtable again to the LinkedHashSet lhm
System.out.println("Now the contents of the LinkedHashMap after adding the Hashtable collection to it is: ");
for(int i=1; i<=lhm.size(); i++)
{
System.out.println("KEY = "+i+" VALUE = "+lhm.get(i)); // displaying the contents of the LinkedHashMap
}

}

catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}

}


}

The output of the above given example will be displayed as follows:

The contents of the LinkedHashMap are as follows:
KEY: 1  VALUE: 7
KEY: 2  VALUE: 5
KEY: 3  VALUE: 8
KEY: 4  VALUE: 2
KEY: 5  VALUE: 9
KEY: 6  VALUE: 52
The contents of the LinkedHashMap after removal of items are:
KEY: 1  VALUE: 7
KEY: 4  VALUE: 2
KEY: 5  VALUE: 9
KEY: 6  VALUE: 52
false
false
The contents of the Hashtable is as follows:
KEY = 1 VALUE = 7
KEY = 2 VALUE = 70
KEY = 3 VALUE = 17
KEY = 4 VALUE = 2
KEY = 5 VALUE = 9
KEY = 6 VALUE = 52
Now the contents of the LinkedHashMap after adding the Hashtable collection to it is:
KEY = 1 VALUE = 7
KEY = 2 VALUE = 70
KEY = 3 VALUE = 17
KEY = 4 VALUE = 2
KEY = 5 VALUE = 9
KEY = 6 VALUE = 52

Previous Home Next