
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
LinkedHashMap and LinkedHashSet in Java
LinkedHashMap
Hash table and linked list implementation of the Map interface, with predictable iteration order. Let us see an example −
Example
import java.util.*; public class Demo { public static void main(String args[]){ LinkedHashMap<Integer, String> my_set; my_set = new LinkedHashMap<Integer, String>(); my_set.put(67, "Joe"); my_set.put(90, "Dev"); my_set.put(null, "Nate"); my_set.put(68, "Sara"); my_set.put(69, "Amal"); my_set.put(null, "Jake"); my_set.put(69, "Ral"); my_set.entrySet().stream().forEach((m) ->{ System.out.println(m.getKey() + " " + m.getValue()); }); } }
Output
67 Joe 90 Dev null Jake 68 Sara 69 Ral
A class named Demo contains the main function, where an instance of the LinkedHashMap is created. Elements are added into this hash map using the ‘put’ function, in the ‘’’integer, string’’’ format. A ‘forEach’ loop is used to iterate over the hash map and elements are displayed on the console.
LinkedHashSet
Hash table and linked list implementation of the Set interface, with predictable iteration order. Let us see an example −
Example
import java.util.*; public class Demo { public static void main(String args[]){ LinkedHashSet<String> my_set; my_set = new LinkedHashSet<String>(); my_set.add("Joe"); my_set.add("Dev"); my_set.add("Nate"); my_set.add("Sara"); my_set.add("Amal"); my_set.add("Jake"); my_set.add("Ral"); Iterator<String> my_itr = my_set.iterator(); while (my_itr.hasNext()){ System.out.println(my_itr.next()); } } }
Output
Joe Dev Nate Sara Amal Jake Ral
A class named Demo contains the main function, where an instance of the LinkedHashSet is created. Elements are added into this LinkedHashSet using the ‘add’ function. An iterator is defined that can be used to iterate over the hash set elements. These elements are displayed on the console.
- Related Articles
- Difference Between LinkedList and LinkedHashSet in Java
- Difference Between HashMap and LinkedHashMap in Java
- Difference between TreeMap, HashMap, and LinkedHashMap in Java
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- Get Size of Java LinkedHashSet
- Difference between TreeMap, HashMap and LinkedHashMap in Java programming
- Get Size of Java LinkedHashMap
- Remove specified element from Java LinkedHashSet
- Remove all elements from Java LinkedHashSet
- Fetch key-valuepair from Java LinkedHashSet
- Iterate through elements of Java LinkedHashSet
- Add elements to LinkedHashMap collection in Java
- Iterate through the values of Java LinkedHashMap in Java
- Remove a value from Java LinkedHashMap
- Remove all values from Java LinkedHashMap
