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 create a LinkedHashMap using collections in java

how to create a LinkedHashMap using collections in java

Previous Home Next

 

In this example we will create a LinkedHashMap.

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 we have created a class named CollectionExample and created a object of the LinkedHashMap class using its default constructor as,

LinkedHashMap lhm = new LinkedHashMap();
 The above statementc onstructs an empty  LinkedHashMap instance with a default capacity (16) and load factor (0.75).
and added some elements to it using the put(Object key, Object value) method on the object of the LinkedHashMap object. The general form of the put() method is ,

put(Object key, Object value)
This method adds the specified value with the specified key in the LinkedHashMap.

In the given example we have created an obejct of the LinkedHashMap by using the default constructor of the LinkedHashMap. Then we have added some elements to this object by using the put(Object key, Object value) method on it. 
Fianlly we have displayed the contents of the 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


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

}


}

The output of the above example is given as: 

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
Previous Home Next