简体   繁体   English

Go 通过多个不同大小的 hashmap 来提取具有公共键的值

[英]Go through multiple hashmaps of different sizes to extract values with common keys

So I essentially want to go through all the elements in the arraylist and match it with the keys of each hashmap and for the values of the common keys I want to make a new arraylist. So I essentially want to go through all the elements in the arraylist and match it with the keys of each hashmap and for the values of the common keys I want to make a new arraylist.

Essentially if keygrades is on value 1, I want to check every hashmap with the key 1 and then extract all the values associated with that key and make a brand new arraylist with those values.本质上,如果 keygrades 的值为 1,我想使用密钥 1 检查每个 hashmap,然后提取与该密钥关联的所有值,并使用这些值创建一个全新的 arraylist。

ArrayList <String> keygrades = new ArrayList<>();
HashMap <String,String> gradeA = new HashMap<>();
HashMap <String,String> gradeB = new HashMap<>();
HashMap <String,String> gradeC = new HashMap<>();
HashMap <String,String> gradeD = new HashMap<>();

This is what is in the hashmap:这是 hashmap 中的内容:

keygrades = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
gradeA = {11=134, 1=100, 3=110, 4=120, 15=142, 5=130}
gradeB = {2=102, 3=103, 6=108, 8=109}
gradeC = {3=104, 5=105, 6=111}
gradeD = {3=122, 4=123}

For example for key 1 I want a new arraylist which would be (100,"","","") ""= empty string.例如,对于键 1,我想要一个新的 arraylist,即 (100,"","","") ""= 空字符串。 For key 2 I want a new arraylist which would be ("",102,"","").It would continue going through hashmaps in order and inputting into new arraylist each time.对于键 2,我想要一个新的 arraylist,它将是(“”,102,“”,“”)。它将继续按顺序遍历哈希图并每次输入新的 arraylist。

I recommend writing a method with this struture:我建议用这种结构编写一个方法:

  1. Create an ArrayList<ArrayList>, which will hold the outcoming ArrayLists containing the different values for common keys.创建一个 ArrayList<ArrayList>,它将保存包含公共键的不同值的输出 ArrayList。

  2. Iterate over Arraylist containing the keys.遍历包含密钥的 Arraylist。

  3. Create an ArrayList that will get the 4 values.创建将获得 4 个值的 ArrayList。

  4. Check all 4 Hashmaps with the current key using HashMap.get(key).使用 HashMap.get(key) 使用当前键检查所有 4 个 Hashmap。 If the outcome is null, you should add "" to your ArrayList, otherwise you enter the value to the ArrayList inside the loop.如果结果是 null,则应将“”添加到 ArrayList,否则将值输入到循环内的 ArrayList。

After it iterated through the ArrayList containing the keys.在它遍历包含密钥的 ArrayList 之后。 You should have an ArrayList<ArrayList> holding exactly as much ArrayList containing the values for similar keys, as you ArrayList of keys size.您应该有一个 ArrayList<ArrayList> 与包含类似键值的 ArrayList 完全相同,就像您的键大小为 ArrayList 一样。

You might need to create a method like this:您可能需要创建这样的方法:

public ArrayList<String> createArryList(ArrayList <String> keyGrades, HashMap <String,String> grade)
    {
        ArrayList <String> ars = new ArrayList<>();
        for(String keyGrade: keyGrades)
        {
            ars.add(keyGrade);
            var str = grade.get(keyGrade);
            ars.set(Integer.parseInt(keyGrade)-1, str != null? str:"emptyString");
        }

        return ars;
    }

You first need to initialize the arrayList by adding the keyGuard and then if the grade has a value for the specified keyGrade it would be stored in the str variable and in your main method you can do it like this for example:您首先需要通过添加keyGuard来初始化arrayList ,然后如果等级具有指定keyGrade的值,它将存储在str变量中,在您的 main 方法中,您可以这样做,例如:

public static void main(String[] args)
    {
        ArrayList<String> keygrades = new ArrayList<>();
        HashMap<String,String> gradeA = new HashMap<>();
        keygrades.add("1");
        keygrades.add("2");
        keygrades.add("3");
        keygrades.add("4");
        gradeA.put("1", "100");
        gradeA.put("11", "102");
        gradeA.put("3", "104");
        gradeA.put("4", "122");
        gradeA.put("5","123");
        var arr = createArryList(keygrades,gradeA);
        for(String s: arr)
        {
            System.out.print(s+ " ");
        }
    }

The output would be like this: output 将是这样的:

100 emptyString 104 122 

You can achieve this by transforming your maps into a stream of maps and extracting the values for a particular key.您可以通过将地图转换为 stream 地图并提取特定键的值来实现此目的。

List<String> values = Stream.of(gradeA,
                                gradeB,
                                gradeC,
                                gradeD)
                            .map(map -> map.getOrDefault("1", ""))
                            .collect(Collectors.toList());

To reuse this, you'll have to create a function that handles the combining of the maps and a function that extracts values from that stream of maps.要重用它,您必须创建一个处理地图组合的 function 和一个 function 从地图的 stream 中提取值。

The example should get you on your way.这个例子应该让你上路。

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

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