Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取值的索引_Java_Hashmap - Fatal编程技术网

Java 获取值的索引

Java 获取值的索引,java,hashmap,Java,Hashmap,我有这样一个输入 0 Overcast Yes 4 0 Rainy Yes 3 0 Sunny No 3 1 Cool No 1 1 Cool Yes 3 else{ for (Map.Entry<String, List<String>> entry : mapPart.entrySet()) { String colId = entry.getKey(); if(colId.equals(firstPart)){ List<Str

我有这样一个输入

0 Overcast Yes  4
0 Rainy Yes 3
0 Sunny No  3
1 Cool No   1
1 Cool Yes  3
else{
 for (Map.Entry<String, List<String>> entry : mapPart.entrySet()) {
  String colId = entry.getKey();
  if(colId.equals(firstPart)){
     List<String> lst = get(colId);
     lst.add(restOfString);
     mapPart.put(colId,lst);
  }
 }
我正在尝试将这些数据存储在Hashmap中

{0=[Overcast Yes 4,Rainy Yes    3,Sunny No  3]}
{1=[Cool No 1,Cool Yes  3]}
到目前为止,我所做的是

        Map<String, List<String>> mapPart = new HashMap<String, List<String>>();
        List<String> tmpList = new ArrayList<String>();
        while((partLine = bfpart.readLine())!=null){
            String restOfString="";
            String[] first = partLine.split(" ");
            String firstPart = first[0];
            for (int i=1; i<first.length; i++)
            {
                restOfString += first[i];
                restOfString += " ";
            }
            if(mapPart.isEmpty()){
                tmpList.add(restOfString);
                mapPart.put(firstPart, tmpList);
            }
            else{
                for (Map.Entry<String, List<String>> entry : mapPart.entrySet()) {
              String colId = entry.getKey();
              if(colId.equals(firstPart)){
                 List<String> lst = mapPart.get(colId);
                 lst.add(restOfString);
                 mapPart.put(colId,lst);
              }
              else{ //should we craete a new list
              }
            }

        }
我试图计算这个方程式

Info(n)=([阴天的是编号,阴天的否编号],[阴天的是编号,阴天的否编号],[晴天的是编号,晴天的否编号])

  • 以上是处理这个方程的好方法吗
  • 或者,在读取文件本身时,是否可以完成等式

  • 有一种更简单易读的方法

    List<String> value = mapPart.get(firstPart); //1. Get the List<String> for the key
    if(value == null) { //2. If it doesn't exist, then create and put it against the key
        value = new ArrayList<String>();
        mapPart.put(first, value);
    }
    value.add(restOfString); //3. Finally add the values for the key
    
    List value=mapPart.get(第一部分)//1.获取密钥的列表
    如果(value==null){//2。如果它不存在,则创建它并将其放在键上
    值=新的ArrayList();
    mapPart.put(第一,值);
    }
    增值(再增值)//3.最后添加键的值
    
    HashMap不存储基于索引的值。这里不需要索引。你可以这样做

    0 Overcast Yes  4
    0 Rainy Yes 3
    0 Sunny No  3
    1 Cool No   1
    1 Cool Yes  3
    
    else{
     for (Map.Entry<String, List<String>> entry : mapPart.entrySet()) {
      String colId = entry.getKey();
      if(colId.equals(firstPart)){
         List<String> lst = get(colId);
         lst.add(restOfString);
         mapPart.put(colId,lst);
      }
     }
    
    else{
    对于(Map.Entry:mapPart.entrySet()){
    字符串colId=entry.getKey();
    如果(共线性等于(第一部分)){
    列表lst=get(colId);
    第一次添加(重新启动);
    mapPart.put(colId,lst);
    }
    }
    
    您不需要迭代整个哈希映射。这是映射的概念,您可以非常高效地按键搜索:

    List<String> list = mapPart.get(firstPart);
    if (list == null) {
        // first time using this index
        list = new ArrayList<String>();
        mapPart.put(firstPart, list);
    }
    list.add(restOfString);
    
    List List=mapPart.get(第一部分);
    if(list==null){
    //第一次使用这个索引
    列表=新的ArrayList();
    mapPart.put(第一部分,列表);
    }
    list.add(restOfString);
    

    (替换你的
    for(Map.entry)你想完成什么?替换现有的值还是什么?你一定要检查
    Map
    代表什么以及它有什么好处-我想你把地图错当成了列表。@mvreijn:我想得到的输出是“{0=[阴天是4,阴雨是3,晴天是3],1=[Cool No 1,Cool Yes 3]}“你能更详细地解释你的目标吗?几个输入/输出示例也不错。对于每个列id,我想找到上面的公式“Info(n)”映射不适用于参数(字符串,布尔值)。mapPart.get(colId)。add(restOfString)是Boolean编辑的答案以添加列表。谢谢Sanbhat:在其他部分中,我们需要创建一个新条目。在当前给定的答案中,我们将只获得o值,在mapPart.get(firstPart)中显示“类型不匹配:无法从列表转换为字符串”对不起,请再试一次。
    list
    的类型应该是
    list
    当然可以。我可以单独获取值。请查看我的编辑