JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to iterate through LinkedHashMap?


Description:

This example show how to iterate through LinkedHashMap. You always get elements by its insertion order.


Code:
package com.java2novice.linkedhashmap;

import java.util.LinkedHashMap;
import java.util.Set;

public class MyMapIterate {

	public static void main(String a[]){
		
		LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
		lhm.put("one", "This is first element");
		lhm.put("two", "This is second element");
		lhm.put("four", "Element inserted at 3rd position");
		Set<String> keys = lhm.keySet();
		for(String k:keys){
			System.out.println(k+" -- "+lhm.get(k));
		}
	}
}

Output:
one -- This is first element
two -- This is second element
four -- Element inserted at 3rd position
<< Previous Program | Next Program >>

List Of All LinkedHashMap Sample Programs:

  1. LinkedHashMap basic operations
  2. How to iterate through LinkedHashMap?
  3. How to check whether the value exists or not in a LinkedHashMap?
  4. How to delete all entries from LinkedHashMap object?
  5. How to eliminate duplicate user defined objects as a key from LinkedHashMap?
  6. How to find user defined objects as a key from LinkedHashMap?
  7. How to delete user defined objects as a key from LinkedHashMap?
Knowledge Centre
What is adapter class?
An adapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.
Famous Quotations
Success consists of going from failure to failure without loss of enthusiasm.
-- Winston Churchill

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.