将项目添加到hashmap中的阵列列表中

发布于 2025-02-10 15:23:45 字数 1136 浏览 3 评论 0原文

我正在尝试创建一个通过字符列表进行的hashmap 例如[X,X,X,Y,Z,X]将返回图:

{
  X: [0,1,4];
  Y: [2];
  Z: [3];
}

我有此代码,但它不起作用,因为添加方法返回布尔值,我需要返回新的列表:

/* turn the pattern into a List of characters
        turn the List into a HashMap with the keys as the characters and 
        the values as the indexes of the characters in the List
        */
        ArrayList<Character> listOfPatternChars = convertStringToCharList(userPattern);
        HashMap<Character,ArrayList<Integer>> mapOfPatternCharsIndex = new HashMap<Character,ArrayList<Integer>>();
        
        ArrayList<ArrayList<Integer>> arrayOfIndexes = new ArrayList<ArrayList<Integer>>();

        for (int i = 0; i < listOfPatternChars.size(); i++) {
            mapOfPatternCharsIndex.putIfAbsent(listOfPatternChars.get(i), new ArrayList<Integer>());
            mapOfPatternCharsIndex.computeIfPresent(listOfPatternChars.get(i), (k,v) -> v.add(i) );
        }

我猜在JavaScript中,我可以使用传播操作员并执行(k,v)=&gt之类的操作。 [... v,i]

Java中有类似的东西吗?

I'm trying to create a HashMap that goes through an ArrayList of characters and returns the characters as keys and the values as an ArrayList oof indexes where they showed up
for example [X,X,Y,Z,X] would return a map like :

{
  X: [0,1,4];
  Y: [2];
  Z: [3];
}

I have this code but it`s not working because the add method returns a boolean and I need to return a new List:

/* turn the pattern into a List of characters
        turn the List into a HashMap with the keys as the characters and 
        the values as the indexes of the characters in the List
        */
        ArrayList<Character> listOfPatternChars = convertStringToCharList(userPattern);
        HashMap<Character,ArrayList<Integer>> mapOfPatternCharsIndex = new HashMap<Character,ArrayList<Integer>>();
        
        ArrayList<ArrayList<Integer>> arrayOfIndexes = new ArrayList<ArrayList<Integer>>();

        for (int i = 0; i < listOfPatternChars.size(); i++) {
            mapOfPatternCharsIndex.putIfAbsent(listOfPatternChars.get(i), new ArrayList<Integer>());
            mapOfPatternCharsIndex.computeIfPresent(listOfPatternChars.get(i), (k,v) -> v.add(i) );
        }

I guess in JavaScript I could use the spread operator and do something like (k, v) => [...v,i]

is there anything similar in Java?

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

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

发布评论

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

评论(3

玩心态 2025-02-17 15:23:45

您可以简单地做类似的事情:

mapOfPatternCharsIndex.computeIfPresent(listOfPatternChars.get(i), (k,v) -> {v.add(i); return v;} );

You can simply do something like:

mapOfPatternCharsIndex.computeIfPresent(listOfPatternChars.get(i), (k,v) -> {v.add(i); return v;} );
雨巷深深 2025-02-17 15:23:45

以下是可以接受的吗?

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

public class Main {

    public static void main(String[] args) {
        Map<Character, List<Integer>> map = new HashMap<>();
        String userPattern = "XXYZX";
        for (int i = 0; i < userPattern.length(); i++) {
            Character key = userPattern.charAt(i);
            List<Integer> indexes = map.get(key);
            if (indexes == null) {
                indexes = new ArrayList<>();
            }
            indexes.add(i);
            map.put(key, indexes);
        }
        System.out.println(map);
    }
}

运行上述代码会产生以下输出:

{X=[0, 1, 4], Y=[2], Z=[3]}

Is the following acceptable?

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

public class Main {

    public static void main(String[] args) {
        Map<Character, List<Integer>> map = new HashMap<>();
        String userPattern = "XXYZX";
        for (int i = 0; i < userPattern.length(); i++) {
            Character key = userPattern.charAt(i);
            List<Integer> indexes = map.get(key);
            if (indexes == null) {
                indexes = new ArrayList<>();
            }
            indexes.add(i);
            map.put(key, indexes);
        }
        System.out.println(map);
    }
}

Running the above code produces the following output:

{X=[0, 1, 4], Y=[2], Z=[3]}
橘和柠 2025-02-17 15:23:45

您可以将intstreamcollectors.groupingby一起从列表中获取所有字符及其相应的索引。

List<Character> charList = Arrays.asList('X', 'X', 'Y', 'Z', 'X');

Map<Character, List<Integer>> map = IntStream.range(0, charList.size())
        .boxed()
        .collect(Collectors.groupingBy(charList::get));

System.out.println(map);

输出:

{X=[0, 1, 4], Y=[2], Z=[3]}

You can use IntStream with Collectors.groupingBy to fetch all the characters and their corresponding indices from the list.

List<Character> charList = Arrays.asList('X', 'X', 'Y', 'Z', 'X');

Map<Character, List<Integer>> map = IntStream.range(0, charList.size())
        .boxed()
        .collect(Collectors.groupingBy(charList::get));

System.out.println(map);

Output:

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