You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除含内部列表的对象列表重复项并合并内部列表(基于给定POJO)

Deduplicate Transmission Objects and Merge Page Lists in Java

Hey there! Let's solve this problem where you're getting duplicate Transmission objects in your database response, and need to deduplicate them while merging their associated Page lists. Here's a step-by-step solution tailored to your POJO structure.

First, Let's Recap Your POJOs

Just to make sure we're on the same page, here are your class definitions (with added constructors/getters/setters for practical use):

class Response {
    private List<Transmission> transmission;

    public Response() {}
    public Response(List<Transmission> transmission) {
        this.transmission = transmission;
    }

    public List<Transmission> getTransmission() {
        return transmission;
    }

    public void setTransmission(List<Transmission> transmission) {
        this.transmission = transmission;
    }
}

class Transmission {
    private String transmission_id;
    private String transmission_name;
    private String transmission_type;
    private List<Page> page;

    public Transmission() {}
    public Transmission(String transmission_id, String transmission_name, String transmission_type, List<Page> page) {
        this.transmission_id = transmission_id;
        this.transmission_name = transmission_name;
        this.transmission_type = transmission_type;
        this.page = page;
    }

    // Getters and Setters
    public String getTransmission_id() { return transmission_id; }
    public void setTransmission_id(String transmission_id) { this.transmission_id = transmission_id; }
    public String getTransmission_name() { return transmission_name; }
    public void setTransmission_name(String transmission_name) { this.transmission_name = transmission_name; }
    public String getTransmission_type() { return transmission_type; }
    public void setTransmission_type(String transmission_type) { this.transmission_type = transmission_type; }
    public List<Page> getPage() { return page; }
    public void setPage(List<Page> page) { this.page = page; }
}

class Page {
    private String page_id;
    private String page_no;
    private String page_type;

    public Page() {}
    public Page(String page_id, String page_no, String page_type) {
        this.page_id = page_id;
        this.page_no = page_no;
        this.page_type = page_type;
    }

    // Add getters/setters as needed
}

Core Solution: Use a Map to Group and Merge

The key idea is to use a HashMap where the key is the unique transmission_id, and the value is the deduplicated Transmission object. We'll iterate through all incoming transmissions, merge pages for duplicates, then build a new Response with the cleaned-up list.

Imperative Style (Easy to Follow)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TransmissionDeduplicator {
    public Response deduplicateAndMerge(List<Response> databaseResponses) {
        // Map to hold unique Transmission objects, keyed by transmission_id
        Map<String, Transmission> uniqueTransmissions = new HashMap<>();

        // Iterate through all responses and their transmissions
        for (Response response : databaseResponses) {
            if (response.getTransmission() == null || response.getTransmission().isEmpty()) {
                continue; // Skip empty or null transmission lists
            }

            for (Transmission currentTrans : response.getTransmission()) {
                String transId = currentTrans.getTransmission_id();

                if (uniqueTransmissions.containsKey(transId)) {
                    // Merge pages into the existing Transmission
                    Transmission existingTrans = uniqueTransmissions.get(transId);
                    if (currentTrans.getPage() != null && !currentTrans.getPage().isEmpty()) {
                        if (existingTrans.getPage() == null) {
                            existingTrans.setPage(new ArrayList<>());
                        }
                        existingTrans.getPage().addAll(currentTrans.getPage());
                    }
                } else {
                    // Add new Transmission to the map (copy page list to avoid reference issues)
                    Transmission newTrans = new Transmission(
                            currentTrans.getTransmission_id(),
                            currentTrans.getTransmission_name(),
                            currentTrans.getTransmission_type(),
                            new ArrayList<>(currentTrans.getPage())
                    );
                    uniqueTransmissions.put(transId, newTrans);
                }
            }
        }

        // Build the final deduplicated Response
        List<Transmission> cleanedTransmissions = new ArrayList<>(uniqueTransmissions.values());
        return new Response(cleanedTransmissions);
    }
}

Java 8+ Stream Style (Concise)

If you prefer a more modern, concise approach, you can use Streams with Collectors.toMap:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StreamBasedDeduplicator {
    public Response deduplicateWithStream(List<Response> databaseResponses) {
        List<Transmission> cleanedTransmissions = databaseResponses.stream()
                // Filter out responses with empty/null transmission lists
                .filter(r -> r.getTransmission() != null && !r.getTransmission().isEmpty())
                // Flatten all transmissions into a single stream
                .flatMap(r -> r.getTransmission().stream())
                // Group by transmission_id, merging pages for duplicates
                .collect(Collectors.toMap(
                        Transmission::getTransmission_id,
                        // Create a copy of the transmission to avoid modifying original objects
                        trans -> new Transmission(
                                trans.getTransmission_id(),
                                trans.getTransmission_name(),
                                trans.getTransmission_type(),
                                new ArrayList<>(trans.getPage())
                        ),
                        // Merge logic: combine page lists of duplicate transmissions
                        (existingTrans, newTrans) -> {
                            if (newTrans.getPage() != null) {
                                existingTrans.getPage().addAll(newTrans.getPage());
                            }
                            return existingTrans;
                        }
                ))
                // Convert map values to a list
                .values()
                .stream()
                .collect(Collectors.toList());

        return new Response(cleanedTransmissions);
    }
}

Key Notes to Keep in Mind

  • Handling Duplicate Field Values: The above code assumes that for the same transmission_id, other fields (like transmission_name or transmission_type) are consistent across duplicates. If they might differ, you'll need to add logic to decide which value to keep (e.g., retain the first occurrence, or the latest one).
  • Null Safety: We added checks for null/empty lists to avoid NullPointerExceptions—always important when dealing with database data!
  • List Copying: Using new ArrayList<>(trans.getPage()) ensures that changes to the original Page lists don't affect the deduplicated objects. If you don't need this isolation, you can skip the copy.

内容的提问来源于stack exchange,提问作者SReddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:33:31