添加和删​​除ArrayList中的元素设置为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,我希望第二列具有它的值(int),在arraylist的第n个索引里面。

到目前为止,这是我的完整代码:

 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> userRatings = new HashMap<String, ArrayList>(); 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 ratings = new ArrayList(); // 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的内容时遇到问题。

总结一下:文件第一列中的每个字符串在hashmap中都有自己的键(userList)程序将检查它是否有密钥,如果没有密钥,它将创建一个新的arraylist作为键。 arrayList将填充50个索引,其中包含“0”。 之后,arraylist将从文件中添加新值,其中第二列中的整数将添加到第n列的相应值。

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

当地图中没有给定密钥时,您必须添加新的密钥值对

 if(!userRatings.containsKey(user)) //The user does not have ratings. { ArrayList ratings = new ArrayList(); 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 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 first = userRatings.get(e1); ArrayList 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 first = userRatings.get(e1); ArrayList second = userRatings.get(e2); Arrays.equals(first.toArray(), second.toArray()); 

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

例如

 List list = new ArrayList(); //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> userRatings = new HashMap>(); List ratings = new ArrayList(); 

要将值插入用户e6的第n个索引:

 List ratings = userRatings.get(user); if(ratings == null) { ratings = new ArrayList(); //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