CodesJava

Easy learning with example program codes

how to search a key in linkedhashmap in java?


LinkedHashMap

LinkedHashMap extends HashMap class and implements the Map interface. It contains the elements in key-value pair form. It maintains insertion order for its elements. It not allowed duplicate key. A LinkedHashMap can have only one null key and multiple null values.

We can use containsKey() method to search a key in treemap in java.

containsKey(Object k): Returns true if this map contains specified key otherwise returns false.
Syntax: public boolean containsKey(Object k)

Example

package com.codesjava;
 
import java.util.LinkedHashMap;
 
public class Test {
	public static void main(String args[]){
		LinkedHashMap<Integer, String> linkedHashMap = 
				new LinkedHashMap<Integer, String>();
 
		//Add objects to the HashSet.
		linkedHashMap.put(4, "Roxy");
		linkedHashMap.put(2, "Sunil");
		linkedHashMap.put(5, "Sandy");
		linkedHashMap.put(1, "Munish");
		linkedHashMap.put(3, "Pardeep");
 
		//Print the LinkedHashMap object.
		System.out.println("LinkedHashMap elements:");
		System.out.println(linkedHashMap);
 
		System.out.println("LinkedHashMap contains key 4:" + linkedHashMap.containsKey(4));
	}
}

Output

LinkedHashMap elements:
{4=Roxy, 2=Sunil, 5=Sandy, 1=Munish, 3=Pardeep}
LinkedHashMap contains key 4:true
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki