The LinkedHashMap.entryStream()
method is used to create a stream of the entries in the LinkedHashMap
.This guide will cover the method’s usage with examples, and we will also cover a real-world use case to show how LinkedHashMap can be used effectively.
Table of Contents
- Introduction
entryStream
Method Syntax- Examples
- Creating a Stream from LinkedHashMap Entries
- Using Stream Operations on LinkedHashMap Entries
- Real-World Use Case
- Example: Filtering and Collecting Entries
- Conclusion
Introduction
The LinkedHashMap.entryStream()
method is a conceptual method that leverages the entrySet().stream()
method to provide a stream of the entries contained in the LinkedHashMap
. Streams allow you to perform various operations on collections of data in a functional programming style, which can lead to more readable and concise code.
entryStream() Method Syntax
The syntax for obtaining a stream of entries from a LinkedHashMap
is as follows:
public Set<Map.Entry<K, V>> entrySet().stream()
- The method does not take any parameters.
- The method returns a stream of the entries contained in the map.
Examples
Creating a Stream from LinkedHashMap Entries
You can create a stream from the entries of a LinkedHashMap
and perform various operations on it.
Example
import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Stream; public class EntryStreamExample { public static void main(String[] args) { // Creating a LinkedHashMap with String keys and Integer values LinkedHashMap<String, Integer> people = new LinkedHashMap<>(); // Adding entries to the LinkedHashMap people.put("Ravi", 25); people.put("Priya", 30); people.put("Vijay", 35); // Creating a stream from the entries Stream<Map.Entry<String, Integer>> entryStream = people.entrySet().stream(); // Printing the entries using the stream entryStream.forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue())); } }
Output:
Ravi: 25 Priya: 30 Vijay: 35
Using Stream Operations on LinkedHashMap Entries
You can perform various stream operations such as filtering, mapping, and collecting on the stream of entries.
Example
import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; public class StreamOperationsExample { public static void main(String[] args) { // Creating a LinkedHashMap with String keys and Integer values LinkedHashMap<String, Integer> people = new LinkedHashMap<>(); // Adding entries to the LinkedHashMap people.put("Ravi", 25); people.put("Priya", 30); people.put("Vijay", 35); people.put("Ajay", 40); people.put("Sneha", 45); // Creating a stream from the entries and filtering entries where value > 30 var filteredEntries = people.entrySet().stream() .filter(entry -> entry.getValue() > 30) .collect(Collectors.toList()); // Printing the filtered entries System.out.println("Filtered entries: " + filteredEntries); } }
Output:
Filtered entries: [Vijay=35, Ajay=40, Sneha=45]
Real-World Use Case
Example: Filtering and Collecting Entries
A common real-world use case for LinkedHashMap.entryStream()
is filtering and collecting entries based on specific criteria. For example, let’s consider a scenario where we need to filter entries where the age is greater than a certain value and collect them into a new map.
Example
import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; public class FilteringEntriesExample { public static void main(String[] args) { // Creating a LinkedHashMap to store user ages LinkedHashMap<String, Integer> userAges = new LinkedHashMap<>(); // Adding user ages to the LinkedHashMap userAges.put("Ravi", 25); userAges.put("Priya", 30); userAges.put("Vijay", 35); userAges.put("Ajay", 40); userAges.put("Sneha", 45); // Filtering entries where age is greater than 30 and collecting them into a new map Map<String, Integer> filteredUserAges = userAges.entrySet().stream() .filter(entry -> entry.getValue() > 30) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); // Printing the filtered entries System.out.println("Filtered user ages: " + filteredUserAges); } }
Output:
Filtered user ages: {Vijay=35, Ajay=40, Sneha=45}
In this example, LinkedHashMap.entryStream()
is used to create a stream of entries, filter them based on age, and collect the results into a new map, demonstrating how to perform complex operations on map entries.
Conclusion
The LinkedHashMap.entryStream()
method in Java, conceptually represented by entrySet().stream()
, provides a way to obtain a stream of the entries contained in the LinkedHashMap
. By understanding how to use this method, you can efficiently perform various operations on the entries, making it a versatile tool for functional programming in your Java applications.