- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.