search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
1. Background and core issues: Constructing mappings with potential null values
2. Use Stream API to build Map
3. Update the Product object using mapping
4. Handling and precautions for null values ​​in mapping
5. Summary
Home Java javaTutorial Java 8 Stream API: Efficiently construct maps with null-valued keys and update associated objects

Java 8 Stream API: Efficiently construct maps with null-valued keys and update associated objects

Dec 01, 2025 pm 01:42 PM

Java 8 Stream API: Efficiently construct maps with null-valued keys and update associated objects

This article deeply explores the strategy of using the Stream API to build Map in Java 8, especially focusing on how to handle keys that may have null values, and use this map to efficiently update the associated details of the Product object. The article provides detailed Stream pipeline construction methods, usage of Collectors.toMap(), and best practices for updating objects and managing null values ​​in mapping, aiming to provide a professional and practical tutorial.

1. Background and core issues: Constructing mappings with potential null values

In Java development, we often need to build a map (Map) based on a set of data to quickly find and associate information. A common scenario is that we have a list of ProductKeys and need to find and associate a ProductDetail for each ProductKey. However, not all ProductKeys can find corresponding ProductDetails, which means that in the final mapping, the corresponding value of some ProductKeys may be null. In addition, the ProductKey itself may need to pass some logic (such as the findProductKey method) to confirm its validity or obtain the correct instance.

Our goals are:

  1. From a ProductKey list, combine a ProductCode to ProductDetail mapping to generate a Map.
  2. In this process, it is necessary to correctly handle the situation where ProductKey may return Optional.empty() through findProductKey.
  3. In the generated mapping, the value ProductDetail corresponding to ProductKey is allowed to be null.
  4. Finally, this mapping is used to update the ProductDetail property in the Product object.

To achieve this, we will utilize Java 8’s Stream API, specifically the Collectors.toMap() method.

2. Use Stream API to build Map

The key to building a target mapping is to properly design the Stream pipeline to handle ProductKey lookups and potential null values.

Suppose we have the following basic class definition (some Lombok annotations and getters/setters are omitted for simplicity):

 import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
static class ProductKey {
    @EqualsAndHashCode.Include
    private Long productCode;
    @EqualsAndHashCode.Include
    private Long productDetailCode; // Assume this field may be null, or not used for lookup}

@Data
static class Product {
    @EqualsAndHashCode.Include
    private ProductKey productKey;
    private ProductDetail productDetail; // may initially be null
}

@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
static class ProductDetail {
    @EqualsAndHashCode.Include
    private Long productCode;
    private String description;
    private BigDecimal price;
    private String category;
}

// Auxiliary method, simulates searching for ProductKey, may return Optional.empty()
public static Optional<productkey> findProductKey(Long productCode, List<productkey> productKeys) {
    return productKeys.stream()
            .filter(productKey -&gt; productCode.equals(productKey.getProductCode()))
            // takeWhile(productKey -&gt; productKey != null) is redundant here,
            // Because the filter has ensured that productKey is not null, and Optional.findFirst() will handle the empty stream.findFirst();
}

// Auxiliary method, simulates converting the ProductDetail list into Map<long productdetail>
public static Map<long productdetail> mapProductCodeToProductDetail(List<productdetail> productDetailList) {
    return productDetailList.stream()
            .collect(Collectors.toMap(
                    ProductDetail::getProductCode,
                    Function.identity(),
                    (existing, replacement) -&gt; existing // Handle the situation of duplicate productCode));
}</productdetail></long></long></productkey></productkey>

Now, let's build Map:

 // Simulate data initialization List<productkey> productKeyList = List.of(
        new ProductKey() {{ setProductCode(101L); setProductDetailCode(1L); }},
        new ProductKey() {{ setProductCode(102L); setProductDetailCode(2L); }},
        new ProductKey() {{ setProductCode(103L); setProductDetailCode(3L); }}, // Assume that 103 does not have a corresponding ProductDetail
        new ProductKey() {{ setProductCode(101L); setProductDetailCode(4L); }} // Simulate repeated productCode, but different ProductKey);

List<productdetail> productDetailRawList = List.of(
        new ProductDetail() {{ setProductCode(101L); setDescription("Detail A"); setPrice(BigDecimal.valueOf(10.0)); }},
        new ProductDetail() {{ setProductCode(102L); setDescription("Detail B"); setPrice(BigDecimal.valueOf(20.0)); }}
);

// First, convert the original ProductDetail list into a Map with productCode as the key to facilitate searching Map<long productdetail> productDetailMap = mapProductCodeToProductDetail(productDetailRawList);

//Construct Map<productkey productdetail>
Map<productkey productdetail> prodDetailByKey = productKeyList.stream()
    // 1. Call findProductKey to simulate search and convert each ProductKey into Optional<productkey>
    .map(productKey -&gt; findProductKey(productKey.getProductCode(), productKeyList))
    // 2. Filter out the empty Optional and only retain elements where ProductKey is successfully found.filter(Optional::isPresent)
    // 3. Extract the actual ProductKey object from Optional.map(Optional::get)
    // 4. Use Collectors.toMap() to collect.collect(Collectors.toMap(
        Function.identity(), // keyMapper: ProductKey itself is the key we want productKey -&gt; productDetailMap.get(productKey.getProductCode()), // valueMapper: Get ProductDetail from productDetailMap according to productCode of ProductKey
        (existing, replacement) -&gt; existing // mergeFunction: handles the situation where duplicate ProductKeys cause key conflicts in the ProductKey list. Here we choose to retain the old value));

System.out.println("Generated mapping (prodDetailByKey):");
prodDetailByKey.forEach((key, value) -&gt; System.out.println(" Key: " key.getProductCode() ", Value: " (value != null ? value.getDescription() : "null")));</productkey></productkey></productkey></long></productdetail></productkey>

Code analysis:

  • productKeyList.stream() : Create a stream containing all ProductKeys.
  • .map(productKey -> findProductKey(productKey.getProductCode(), productKeyList)) : This step converts each ProductKey in the stream into an Optional. The findProductKey method is responsible for finding the matching ProductKey in the original productKeyList based on productCode.
  • .filter(Optional::isPresent) : Filter out those elements whose findProductKey cannot find the corresponding ProductKey (that is, return Optional.empty()). This ensures that only valid ProductKeys are processed further.
  • .map(Optional::get) : Extract the actual ProductKey object from the non-empty Optional.
  • .collect(Collectors.toMap(...)) : This is the core collection operation.
    • Function.identity() : As a keyMapper, it means that the current element in the stream (that is, the ProductKey object itself) will serve as the key of the Map.
    • productKey -> productDetailMap.get(productKey.getProductCode()) : As a valueMapper, it finds the corresponding ProductDetail from the pre-built productDetailMap according to the productCode of ProductKey. If there is no entry for the productCode in productDetailMap, the get() method will return null, and this null value will be stored in the final prodDetailByKey mapping.
    • (existing, replacement) -> existing : As mergeFunction, used to handle when multiple stream elements are mapped to the same key (that is, the presence of equals() in productKeyList determines that the same ProductKey). Here we choose to keep the first encountered value.

3. Update the Product object using mapping

Once we have a Map, updating the Product list becomes very straightforward. For such operations with side effects (modifying existing objects), using Iterable.forEach() is usually clearer and more efficient than Stream's map().collect().

 // Simulate Product list List<product> products = List.of(
        new Product() {{ setProductKey(new ProductKey() {{ setProductCode(101L); setProductDetailCode(1L); }}); }},
        new Product() {{ setProductKey(new ProductKey() {{ setProductCode(102L); setProductDetailCode(2L); }}); }},
        new Product() {{ setProductKey(new ProductKey() {{ setProductCode(103L); setProductDetailCode(3L); }}); }} // 103 has no corresponding ProductDetail
);

System.out.println("\nProduct list before update:");
products.forEach(p -&gt; System.out.println(" ProductKey: " p.getProductKey().getProductCode() ", Detail: " (p.getProductDetail() != null ? p.getProductDetail().getDescription() : "null")));

// Use forEach loop to update the ProductDetail of the Product object
products.forEach(product -&gt; {
    ProductDetail detail = prodDetailByKey.get(product.getProductKey());
    product.setProductDetail(detail);
});

System.out.println("\nUpdated Product list:");
products.forEach(p -&gt; System.out.println(" ProductKey: " p.getProductKey().getProductCode() ", Detail: " (p.getProductDetail() != null ? p.getProductDetail().getDescription() : "null")));</product>

Code analysis:

  • products.forEach(...) : Traverse each Product object in the products list.
  • prodDetailByKey.get(product.getProductKey()) : Use the ProductKey of the Product object as the key to obtain the corresponding ProductDetail from the previously constructed prodDetailByKey mapping. If the ProductKey does not exist in the mapping, or its corresponding value is null, the get() method will return null.
  • product.setProductDetail(detail) : Set the obtained ProductDetail (may be null) into the Product object.

4. Handling and precautions for null values ​​in mapping

  • The meaning of null returned by Map.get() : When we obtain the value from a Map through the get(key) method, if the key is not included in the Map or the value corresponding to the key is null, the get() method will return null. In our scenario, prodDetailByKey.get(product.getProductKey()) will return null if there are no details corresponding to product.getProductKey().getProductCode() in productDetailMap. This means that the productDetail property of the Product object will be set to null, which is exactly how we handle the "Product has no ProductDetail" situation.

  • Remove null values ​​from an existing map : If in some scenarios you want to remove all null entries from an existing map, you can use the following method:

     import java.util.Objects;
    // ...
    // Suppose productDetailMap contains some entries with null values ​​productDetailMap.values().removeIf(Objects::isNull);
    // Or if you want to remove key-value pairs, you can iterate over the entrySet
    productDetailMap.entrySet().removeIf(entry -&gt; entry.getValue() == null);

    Note, however, that this is typically a cleanup operation that occurs after the Map is built, not how potential null values ​​are handled during the build process. During the build of the above tutorial, Collectors.toMap allows null to be stored as a value.

  • ProductKey's equals() and hashCode() : As the key of the Map, the ProductKey class must correctly implement the equals() and hashCode() methods. Lombok's @EqualsAndHashCode annotation (and specifying onlyExplicitlyIncluded = true) is a convenient solution. It ensures that only fields marked by @EqualsAndHashCode.Include participate in the calculation of these two methods, which is crucial for the correct behavior of the Map.

  • Correct use of Optional : Optional is to avoid returning null directly and force the caller to deal with the "value may not exist" situation. In Stream pipelines, it is often used to indicate the existence of intermediate results and to safely extract values ​​through filter(Optional::isPresent) and map(Optional::get).

5. Summary

With Java 8's Stream API and Collectors.toMap(), we can build complex maps elegantly and efficiently, even if the keys or values ​​may involve Optional or null. The key is to understand how the various operators (map, filter) of the Stream pipeline work together, and how the keyMapper, valueMapper and mergeFunction parameters of Collectors.toMap() define the construction logic of the map. When updating object properties, Iterable.forEach() often provides more concise and intuitive code for operations with side effects. Correct handling of equals() and hashCode() and understanding the semantics of Optional are the basis for ensuring the correctness of these operations.

The above is the detailed content of Java 8 Stream API: Efficiently construct maps with null-valued keys and update associated objects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure Spark distributed computing environment in Java_Java big data processing How to configure Spark distributed computing environment in Java_Java big data processing Mar 09, 2026 pm 08:45 PM

Spark cannot run in local mode, ClassNotFoundException: org.apache.spark.sql.SparkSession. This is the most common first step of getting stuck: even the dependencies are not correct. Only spark-core_2.12 is written in Maven, but spark-sql_2.12 is not added. SparkSession crashes as soon as it is built. The Scala version must strictly match the official Spark compiled version - Spark3.4.x uses Scala2.12 by default. If you use spark-sqljar of 2.13, the class loader cannot directly find the main class. Practical advice: Go to mvnre

How to safely map user-entered weekday string to integer value and implement date offset operation in Java How to safely map user-entered weekday string to integer value and implement date offset operation in Java Mar 09, 2026 pm 09:43 PM

This article introduces a concise and maintainable way to map the weekday string (such as "Monday") to the corresponding serial number (1-7), and use the modulo operation to realize the forward and backward offset of any number of days (such as Monday plus 4 days to get Friday), avoiding lengthy if chains and hard-coded logic.

What is exception masking (Suppressed Exceptions) in Java_Multiple resource shutdown exception handling What is exception masking (Suppressed Exceptions) in Java_Multiple resource shutdown exception handling Mar 10, 2026 pm 06:57 PM

What is SuppressedException: It is not "swallowed", but actively archived by the JVM. SuppressedException is not an exception loss, but the JVM quietly attaches the secondary exception to the main exception under the premise that "only one exception must be thrown" for you to verify afterwards. It is automatically triggered by the JVM in only two scenarios: one is that the resource closure in try-with-resources fails, and the other is that you manually call addSuppressed() in finally. The key difference is: the former is fully automatic and safe; the latter requires you to keep it to yourself, and it can be written as shadowing if you are not careful. try-

How to use Homebrew to install Java on Mac_A must-have Java tool chain for developers How to use Homebrew to install Java on Mac_A must-have Java tool chain for developers Mar 09, 2026 pm 09:48 PM

Homebrew installs the latest stable version of openjdk (such as JDK22) by default, not the LTS version; you need to explicitly execute brewinstallopenjdk@17 or brewinstallopenjdk@21 to install the LTS version, and manually configure PATH and JAVA_HOME to be correctly recognized by the system and IDE.

How to correctly implement runtime file writing in Java applications (avoiding JAR internal write failures) How to correctly implement runtime file writing in Java applications (avoiding JAR internal write failures) Mar 09, 2026 pm 07:57 PM

After a Java application is packaged as a JAR, data cannot be written directly to the resources in the JAR package (such as test.txt) because the JAR is essentially a read-only ZIP archive; the correct approach is to write variable data to an external path (such as a user directory, a temporary directory, or a configuration-specified path).

Complete tutorial on reading data from file and initializing two-dimensional array in Java Complete tutorial on reading data from file and initializing two-dimensional array in Java Mar 09, 2026 pm 09:18 PM

This article explains in detail how to load an integer sequence in an external text file into a Java two-dimensional array according to a specified row and column structure (such as 2500×100), avoiding manual assignment or index out-of-bounds, and ensuring accurate data order and robust and reusable code.

What is the underlying principle of array expansion in Java_Java memory dynamic adjustment analysis What is the underlying principle of array expansion in Java_Java memory dynamic adjustment analysis Mar 09, 2026 pm 09:45 PM

ArrayList.add() triggers expansion because grow() is called when size is equal to elementData.length. The first add allocates 10 capacity, and subsequent expansion is 1.5 times and not less than the minimum requirement, relying on delayed initialization and System.arraycopy optimization.

A concise method in Java to compare whether four byte values ​​are equal and non-zero A concise method in Java to compare whether four byte values ​​are equal and non-zero Mar 09, 2026 pm 09:40 PM

This article introduces several professional solutions for efficiently and safely comparing multiple byte type return values ​​(such as getPlayer()) in Java to see if they are all equal and non-zero. We recommend two methods, StreamAPI and logical expansion, to avoid Boolean and byte mis-comparison errors.

Related articles