添加和删除元素设置为HashMap中的值

问题描述:

所以我有一个看起来像这样的文件:添加和删除元素设置为HashMap中的值

1st 2nd­ nth 
    e1­, ­­v1, 1 
    e1, v3, 2 
    e1, v4, 4 
    e1, v5, 7 
    e2, v1, 1 
    ., ­., . 
    ., ­., . 
    ., ­., . 

,我想的第一列是一个HashMap的关键(E1或e2或者e3),并且这个值是一个名为“Ratings”的ArrayList,我希望第二列在ArrayList的第n个索引内具有它的值(一个int)。

这里是我的代码在它的全部迄今:

import java.util.*; 
import java.io.*; 

public class Setup 
{ 
    public static void Setup(String[] args) 
    { 
     String user; 
     int value, location; 
     //Create a Hashmap that holds a string key and an ArrayList value 
     HashMap<String, ArrayList<Integer>> userRatings = new HashMap<String, ArrayList<Integer>>(); 
     try 
     { 
      BufferedReader bufferReader = new BufferedReader(new FileReader("Student list.txt")); //read from file 
      String line, sentence; //declare two string variables 
      String[] sData; //declare a string array (store the contents here) 
      line = bufferReader.readLine(); //Read the line 
      while (line != null) //While there is a line, do this: 
      { 
       line = bufferReader.readLine(); 
       sData = line.split(", "); //Into the string array, enter individual values in the line split by the ", " characters 
       int iData[] = new int[sData.length]; //Create an int array the size of the string array 
       user = sData[0]; 
       for (int i = 0; i <sData.length; i++) //fill the int array with the int-version of the string array 
       { 
        iData[i] = Integer.parseInt(sData[i]); //pass the strings as integers into the integer array 
       } 
       value = iData[1]; 
       location = iData[2]; 
       if(!userRatings.containsKey(user)) //The user does not have ratings. 
       { 
        ArrayList<Integer> ratings = new ArrayList<Integer>(); 
        //      ratings = userRatings.get(user); 
        for (int j = 0; j < 50; j++) 
        { 
         ratings.add(j); 
        } 
        System.out.println(user + " " + userRatings.get(user)); 

       } 
       else //The user has ratings 
       { 
        userRatings.get(user).add(location,value); 
        System.out.println(user + " " + userRatings.get(user)); 
       } 
      } 
      bufferReader.close(); 
     } catch (FileNotFoundException e) 
     { 
      System.out.println("File does not exist or could not be found."); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Can't read from file"); 
     } 
     catch (NullPointerException e) 
     { 
     } 
    } 
} 

我有修改的ArrayList内容的问题。

综上所述: 在文件的第1列的每个字符串必须是在HashMap中(用户列表) 程序会检查是否有钥匙,如果没有键存在,它会创建一个新的自己的钥匙arraylist作为密钥的值。 arrayList将填充50个索引,其中将包含“0”。 之后,ArrayList将从文件中添加新值,其中第二列中的整数将被添加到第n列的对应值。

我该如何填充arraylist,以及如何编辑它,以便如果我想在用户e6的第n个索引处添加新的整数,我可以?

当给定的键不存在在你的地图,你必须添加一个新的键值对

if(!userRatings.containsKey(user)) //The user does not have ratings. 
{ 
    ArrayList<Integer> ratings = new ArrayList<Integer>();     
    for (int j = 0; j < 50; j++) { 
     ratings.add(j); 
    } 
    userRatings.put(user, ratings); // place new mapping 
    System.out.println(user + " " + userRatings.get(user)); 
} else //The user has ratings 
{ 
    ArrayList<Integer> ratings = userRatings.get(user); 
    ratings.add(location,value); // Update your list with location and value 
    System.out.println(user + " " + userRatings.get(user)); 
} 

iData[i] = Integer.parseInt(sData[i]);这是不行的给你的文件的内容会抛出NumberFormatException为V1不能被解析为int。

相反,你可以做这样的事情:

value = Integer.parseInt(sData[1].trim().substring(1)); 
location = Integer.parseInt(sData[2].trim()); 

比较两个键的值:

方法1

ArrayList<Integer> first = userRatings.get(e1); 
ArrayList<Integer> second = userRatings.get(e2); 

//Taking the smallest size will ensure that we don't get IndexOutOfBoundsException. 

int length = first.size() < second.size() ? first.size() : second.size(); 

for(int iDx = 0; iDx < legth; iDx++){ 
    //compare content 
} 

方法2

ArrayList<Integer> first = userRatings.get(e1); 
ArrayList<Integer> second = userRatings.get(e2); 

Arrays.equals(first.toArray(), second.toArray()); 
+0

谢谢@mprabhat!这很棒!很简单,我没有这样想过,但这很有道理!拿出来,编辑它,然后放回去。 如果我要比较两个不同的键的值,我将如何处理? 即我想比较e1的阵列列表和e2的阵列列表。 –

+0

如果你想比较两个ArrayList,那么你将不得不循环检查并逐一比较内容。 – mprabhat

+0

另一种方法是将两个ArrayList转换为Array,然后调用[Arrays.equals](http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html) – mprabhat

我想你可以阅读第n个值并使用它将值插入列表中。

例如

List<Integer> list = new ArrayList<Integer>(); 
//values values (0) will be inserted into the list 
//after that for each value in the nth you do something as follows  
//i is the value in the nth column, if the least value for i is 1 otherwise just use i 
list.add(i-1, value); 

也为参考使用界面,而不是实施类如下 -

Map<String, List<Integer>> userRatings = new HashMap<String, List<Integer>>(); 
List<Integer> ratings = new ArrayList<Integer>(); 

要插入值到第n个指标为用户E6:

List<Integer> ratings = userRatings.get(user); 
if(ratings == null) { 
    ratings = new ArrayList<Integer>(); //it's not in the map yet 
    //insert 50 0s here 
    userRatings.put(user, ratings); 
} 
ratings.add(i, value); //i is the nth index and value is the rating 
+0

然而,这太好了,做了什么mprabhat建议很简单,我尝试了它,并让它按我的想象工作!谢谢你的回答!你帮助我更好地理解了这个概念,并为以后的不同方法提供了想法。 –