Java 创建字符串中的字符和ArrayList中的整数的哈希映射<;整数>;

Java 创建字符串中的字符和ArrayList中的整数的哈希映射<;整数>;,java,Java,我必须创建一个HashMap,将字母记录在字符串中,并将其索引值记录在ArrayList中,这样,如果使用某个字符串键调用HashMap,则返回每个相关的索引整数,这样映射就可以自己调用,从而每个键都显示其索引,例如,对于字符串“Hello World”,映射看起来像: d=[9], o=[4, 6], r=[7], W=[5], H=[0], l=[2, 3, 8], e=[1]. 输入的要求是String和ArrayList,而不是char和integer,这让我很困惑。你能给我解释一下地

我必须创建一个
HashMap
,将字母记录在字符串中,并将其索引值记录在
ArrayList
中,这样,如果使用某个字符串键调用
HashMap
,则返回每个相关的索引整数,这样映射就可以自己调用,从而每个键都显示其索引,例如,对于字符串
“Hello World”
,映射看起来像:

d=[9], o=[4, 6], r=[7], W=[5], H=[0], l=[2, 3, 8], e=[1].
输入的要求是
String
ArrayList
,而不是char和integer,这让我很困惑。你能给我解释一下地图与这些物体的关系,以及它们的组成部分,它们最终被记录为键和值吗?尝试调试时,它会在映射调用之前停止处理

错误消息是:

java.lang.AssertionError: Wrong number of entries in Concordance. Expected: 5. Got: 1 
Expected :1
Actual   :5
但是我真的认为我没有很好地掌握
HashMap
,所以如果有人能指导我学习基本知识,或者提供有关使用
HashMap
的任何教育,特别是那些使用
ArrayList
的人,我将不胜感激

public HashMap concordanceForString(字符串s){
HashMap sMap=newhashmap();//创建映射“sMap”
char[]sArray=new char[s.length()];//为字符串转换创建字符数组“sArray”
ArrayList sCharIndex=新的ArrayList();
对于(int i=0;i
本质上,这就是你想要做的

这假定一个
HashMap

但我建议你在钻研溪流之前(或在你的导师建议之前)先熟悉基础知识。 有关更多信息,请查看此代码:

 public static void main(String []args){
    //Create map of respective keys and values
    HashMap<Character, ArrayList<Integer>> map = new HashMap(); 
    String str = "Hello world"; //test string
    int length = str.length(); //length of string
    for(int i = 0; i < length; i++){ 
        ArrayList<Integer> indexes = new ArrayList(); //empty list of indexes
        //character of test string at particular position 
        Character ch = str.charAt(i);  
        //if key is already present in the map, then add the previous index associated with the character to the indexes list
        if(map.containsKey(ch)){ 
            //adding previous indexes to the list
            indexes.addAll(map.get(ch));
        }
        //add the current index of the character to the respective key in map
        indexes.add(i); 
        //put the indexes in the map and map it to the current character
        map.put(ch, indexes);
    }
    //print the indexes of 'l' character
    System.out.print(map.get('l')); 
 }
publicstaticvoidmain(字符串[]args){
//创建相应键和值的映射
HashMap=newHashMap();
String str=“Hello world”//测试字符串
int length=str.length();//字符串的长度
对于(int i=0;i

代码是不言自明的。

以下是一种方法:

String input = "hello world";
Map<String, List<Integer>> letters = new HashMap<String, List<Integer>>();

// remove all whitespace characters - since it appears you are doing that 
String string = input.replaceAll("\\s", "");

// loop over the length of the string
for (int i = 0; i < string.length(); i++) {
    // add the entry to the map
    // if it does not exist, then a new List with value of i is added
    // if the key does exist, then the new List of i is added to the 
    // existing List
    letters.merge(string.substring(i, i + 1), 
        Arrays.asList(i), 
        (o, n) -> Stream.concat(o.stream(), n.stream()).collect(Collectors.toList()));
}
            
System.out.println(letters);
编辑-使用字符作为地图的键:

String input = "hello world";
Map<Character, List<Integer>> letters = new HashMap<Character, List<Integer>>();

String string = input.replaceAll("\\s", "");
for (int i = 0; i < string.length(); i++) {
    letters.merge(string.charAt(i), Arrays.asList(i), (o, n) -> 
        Stream.concat(o.stream(), n.stream()).collect(Collectors.toList()));
}
            
System.out.println(letters);
String input=“hello world”;
映射字母=新HashMap();
String String=input.replaceAll(“\\s”和“”);
对于(int i=0;i
Stream.concat(o.Stream(),n.Stream()).collect(collector.toList());
}
系统输出打印号(字母);
公共类数组{
公共静态void main(字符串[]args){
printSortedMap(concordanceForString(“Hello world”);//r[7]d[9]e[1]w[5]H[0]l[2,3,8]o[4,6]
}
公共静态哈希映射concordanceForString(字符串s){
HashMap sMap=新的HashMap();
字符串str=s.replace(“,”);
对于(int i=0;i
“代码是不言自明的。”-我不太确定。@hev1谢谢你的评论,代码已经用相应的评论进行了更新。这对OP解决当前算法有什么帮助?@WJS我想它提供了另一种解决方案。这很有效!我在发布后意识到我误解了这个要求——我需要让它将字符串映射为键,也不是关于字符的部分。谢谢!但回顾我的说明,我需要做的是接受一个字符串并返回该字符串的字符及其索引。要更新我的问题,我修改了上面的内容,使其与字符串一起工作。它通过附加一个空Strin将字符
ch
转换为字符串上面的解决方案返回{Hello World=[0,1,2,3,4,5,6,7,8,9,10]}。第一个for循环可以简单地替换为
char[]sArray=s.tocharray();
String input = "hello world";
Map<String, List<Integer>> letters = new HashMap<String, List<Integer>>();

// remove all whitespace characters - since it appears you are doing that 
String string = input.replaceAll("\\s", "");

// loop over the length of the string
for (int i = 0; i < string.length(); i++) {
    // add the entry to the map
    // if it does not exist, then a new List with value of i is added
    // if the key does exist, then the new List of i is added to the 
    // existing List
    letters.merge(string.substring(i, i + 1), 
        Arrays.asList(i), 
        (o, n) -> Stream.concat(o.stream(), n.stream()).collect(Collectors.toList()));
}
            
System.out.println(letters);
{r=[7], d=[9], e=[1], w=[5], h=[0], l=[2, 3, 8], o=[4, 6]}

String input = "hello world";
Map<Character, List<Integer>> letters = new HashMap<Character, List<Integer>>();

String string = input.replaceAll("\\s", "");
for (int i = 0; i < string.length(); i++) {
    letters.merge(string.charAt(i), Arrays.asList(i), (o, n) -> 
        Stream.concat(o.stream(), n.stream()).collect(Collectors.toList()));
}
            
System.out.println(letters);
public class Array {

public static void main(String[] args) {

    printSortedMap(concordanceForString("Hello world"));   // r[7] d[9] e[1] w[5]  H[0] l[2, 3, 8] o[4, 6]
    
}


public static HashMap<String, ArrayList<Integer>> concordanceForString(String s) {
    HashMap<String, ArrayList<Integer>> sMap = new HashMap<>();
    String str = s.replace(" ", "");

    for (int i = 0; i < str.length(); i++) {
        ArrayList<Integer> sCharIndex =  new ArrayList<Integer>();

        for (int j = 0; j < str.length(); j++) {
            if ( str.charAt(i) == str.charAt(j) ) {
                sCharIndex.add(j);
            }
        }
        sMap.put(str.substring(i,i+1), sCharIndex);
    }
    return sMap;
}


public static void printSortedMap(HashMap<String, ArrayList<Integer>> sMap) {
    for (Map.Entry<String, ArrayList<Integer>> entry : sMap.entrySet()) {
        System.out.println(entry.getKey() + entry.getValue());
    }
}