简体   繁体   English

具有动态列的数据结构

[英]Data Structure with Dynamic Columns

I have the following scenario in which 3 arrays are hierarchical - List#3 holds the children of List#2 and List#2 holds the children of List#1 - I want one final list containing List#1, List#2, List#3 - every list consist of (id, child_id) so the link between List#1 and List#2 would be: List1.child_id = List2.id and so on: List2.child_id = List3.id我有以下场景,其中 3 个 arrays 是分层的 - List#3 包含 List#2 的子项,List#2 包含 List#1 的子项 - 我想要一个包含 List#1、List#2、List# 的最终列表3 - 每个列表都由 (id, child_id) 组成,因此 List#1 和 List#2 之间的链接将是: List1.child_id = List2.id 等等: List2.child_id = List3.id

  1. KeyValue List #1:键值列表 #1:

     [(id=1,child_id=5), (id=2,child_id=6), (id=3,child_id=7), ...]
  2. KeyValue List #2:键值列表#2:

     [(id=5,child_id=10), (id=6,child_id=11), (id=7,child_id=12), ...]
  3. KeyValue List #3:键值列表#3:

     [(id=10,child_id=34), (id=11,child_id=35), (id=12,child_id=36), ...]

I need to have one dataset which combines the 3 based on id=child_id and looks like this one:我需要有一个数据集,它结合了基于 id=child_id 的 3 个数据集,看起来像这样:

 [(id=1, child_id_1=5, child_id_2=10,child_id_3=34), (id=2, child_id_1=6, child_id_2=11,child_id_3=35), (id=3, child_id_1=7, child_id_2=12,child_id_3=36), ...]

How can I achieve this in Java?如何在 Java 中实现这一点? Moreover, the number of lists is dynamic, that means > 3 lists.此外,列表的数量是动态的,这意味着 > 3 个列表。

This will work for you.这对你有用。 For each input list below logic will run.对于下面的每个输入列表,逻辑将运行。

List<Map<String,Integer>> mapList = new ArrayList<Map<String,Integer>>();
    for(int i = 0; i < list.size(); i++) {
        Map<String,Integer> tempMap = new HashMap<String,Integer>();
        tempMap.put("id",list.get(i).getId());
        for(int j = 0; j < list.size(); j++) {
            tempMap.put("child_id_"+list.get(j).getId(),list.get(j).getChild_id());
        }
        mapList.add(tempMap);
    }
    System.out.println(mapList);
}

This is the output.这是 output。 [{child_id_1=10, child_id_2=11, id=1}, {child_id_1=10, child_id_2=11, id=2}] [{child_id_1=10, child_id_2=11, id=1}, {child_id_1=10, child_id_2=11, id=2}]

I have created a Class Data to hold the value as u want, refer below code,我创建了一个 Class 数据来保存你想要的值,请参阅下面的代码,

    public static void main(String[] args) throws Exception {
        Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
        map1.put(1, 5);
        map1.put(2, 6);
        map1.put(3, 7);

        Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();
        map2.put(5, 10);
        map2.put(6, 11);
        map2.put(7, 12);

        Map<Integer, Integer> map3 = new HashMap<Integer, Integer>();
        map3.put(10, 34);
        map3.put(11, 35);
        map3.put(12, 36);

        List<Data> datas = new ArrayList<Data>();
        for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
            Data data = new Data();
            data.key = entry.getKey();
            int value = entry.getValue();
            data.col_1 = value;
            data.col_2 = map2.get(value);
            data.col_3 = map3.get(data.col_2);
            datas.add(data);
        }
        System.out.println(datas);
    }
}

class Data{
    int key;
    int col_1;
    int col_2;
    int col_3;
    @Override
    public String toString() {
        return "Data [key=" + key + ", child_1=" + col_1 + ", child_2=" + col_2 + ", child_3=" + col_3 + "]\n";
    }   
}

output output

[Data [key=1, child_1=5, child_2=10, child_3=34]
, Data [key=2, child_1=6, child_2=11, child_3=35]
, Data [key=3, child_1=7, child_2=12, child_3=36]
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM