Java LinkedHashMap valueStream() Method

The LinkedHashMap.valueStream() method in Java provides a stream of the values contained in the LinkedHashMap.

Table of Contents

  1. Introduction
  2. values().stream() Method Syntax
  3. Examples
    • Creating a Stream from LinkedHashMap Values
    • Using Stream Operations on LinkedHashMap Values
  4. Real-World Use Case
    • Example: Calculating Average Age
  5. Conclusion

Introduction

The LinkedHashMap.values().stream() method is used to obtain a stream of the values 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.

values().stream()() Method Syntax

The syntax for obtaining a stream of values from a LinkedHashMap is as follows:

public Collection<V> values().stream()
  • The method does not take any parameters.
  • The method returns a stream of the values contained in the map.

Examples

Creating a Stream from LinkedHashMap Values

You can create a stream from the values of a LinkedHashMap and perform various operations on it.

Example

import java.util.LinkedHashMap;
import java.util.stream.Stream;

public class ValueStreamExample {
    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 values
        Stream<Integer> valueStream = people.values().stream();

        // Printing the values using the stream
        valueStream.forEach(System.out::println);
    }
}

Output:

25
30
35

Using Stream Operations on LinkedHashMap Values

You can perform various stream operations such as filtering, mapping, and reducing on the stream of values.

Example

import java.util.LinkedHashMap;
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 values and filtering values greater than 30
        var filteredValues = people.values().stream()
            .filter(age -> age > 30)
            .collect(Collectors.toList());

        // Printing the filtered values
        System.out.println("Filtered values: " + filteredValues);
    }
}

Output:

Filtered values: [35, 40, 45]

Real-World Use Case

Example: Calculating Average Age

A common real-world use case for LinkedHashMap.values().stream() is calculating aggregate statistics, such as the average age of people stored in a LinkedHashMap.

Example

import java.util.LinkedHashMap;

public class AverageAgeExample {
    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);

        // Calculating the average age using streams
        double averageAge = userAges.values().stream()
            .mapToInt(Integer::intValue)
            .average()
            .orElse(0.0);

        // Printing the average age
        System.out.println("Average age: " + averageAge);
    }
}

Output:

Average age: 35.0

In this example, LinkedHashMap.values().stream() is used to create a stream of ages, and then the average age is calculated using stream operations, demonstrating how to perform aggregate calculations on the values.

Conclusion

The LinkedHashMap.values().stream() method in Java provides a way to obtain a stream of the values contained in the LinkedHashMap. By understanding how to use this method, you can efficiently perform various operations on the values, making it a versatile tool for functional programming in your Java applications.

Comments