在Java中保持瞬态映射

发布于 2025-01-26 10:39:04 字数 631 浏览 2 评论 0原文

方案

class A{
  String aId;
;}
class B{
  String bId;
;}
class C{
  String bId;
;}
class D{
    B b;
    C c;
;}

i hava a 列表< a>。我使用for循环浏览此列表,执行一些数据库操作并创建list< b>。我需要从a到b的映射,因此我还创建了map< a,b>

从此列表< b>,我进行批量API调用并获取list< d>。我还需要从B到C的映射,因此我遍历此list< d>并创建map&lt< b,c>

我的主要要求是从A到C的映射。我无法删除批量API调用,而要删除维护两个地图的额外开销。是否有某种方法可以维护瞬态映射,直接在Java中的a映射到c?

Scenario

class A{
  String aId;
;}
class B{
  String bId;
;}
class C{
  String bId;
;}
class D{
    B b;
    C c;
;}

I hava a List<A>. I traverse this list using a for loop, perform some DB operations and create a List<B>. I need a mapping from A to B, so I create a Map<A,B> also.

From this List<B>, I make a bulk API call and fetch a List<D>. I require a mapping from B to C also, so I traverse over this List<D> and create a Map<B,C> .

My main requirement is a mapping from A to C. I can't remove the bulk API call but want to remove the extra overhead of maintaining two maps. Is there some way of maintaining a transitive mapping, a mapping of A to C directly in JAVA?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶花眉 2025-02-02 10:39:04

您可以按以下方式生成它:

对于循环

import java.util.HashMap;
import java.util.Map;

public class Main
{

    public static void main(String[] args)
    {
        Map<String,String> m1= new HashMap<>();
        Map<String,String> m2 = new HashMap<>();
        m1.put("aaa", "bbb");
        m2.put("bbb", "ccc");
        Map<String,String> combined = new HashMap<>();
        for(String key : m1.keySet())
        {
            // TODO: add error handling or make sure get never returns null 
            combined.put(key, m2.get(m1.get(key)));
        }
        System.out.println(combined.get("aaa"));
    }
}

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;;

public class Main
{

    public static void main(String[] args)
    {
        Map<String,String> m1= new HashMap<>();
        Map<String,String> m2 = new HashMap<>();
        m1.put("aaa", "bbb");
        m2.put("bbb", "ccc");
        Map<String,String> combined = m1.keySet().stream().collect(Collectors.toMap( Function.identity(), x->m2.get(m1.get(x))));
        System.out.println(combined);
    }

}

编辑:根据要求,即使我更喜欢第一个版本,也可以使用流添加一个版本。

You can generate it as follows:

for loop

import java.util.HashMap;
import java.util.Map;

public class Main
{

    public static void main(String[] args)
    {
        Map<String,String> m1= new HashMap<>();
        Map<String,String> m2 = new HashMap<>();
        m1.put("aaa", "bbb");
        m2.put("bbb", "ccc");
        Map<String,String> combined = new HashMap<>();
        for(String key : m1.keySet())
        {
            // TODO: add error handling or make sure get never returns null 
            combined.put(key, m2.get(m1.get(key)));
        }
        System.out.println(combined.get("aaa"));
    }
}

streams

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;;

public class Main
{

    public static void main(String[] args)
    {
        Map<String,String> m1= new HashMap<>();
        Map<String,String> m2 = new HashMap<>();
        m1.put("aaa", "bbb");
        m2.put("bbb", "ccc");
        Map<String,String> combined = m1.keySet().stream().collect(Collectors.toMap( Function.identity(), x->m2.get(m1.get(x))));
        System.out.println(combined);
    }

}

Edit: As requested, added a version using streams, even though I prefer the first version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文