2012-06-04 29 views
0

所以我有一個看起來像這樣的文件:添加和刪除元素設置爲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個索引處添加新的整數,我可以?

回答

0

當給定的鍵不存在在你的地圖,你必須添加一個新的鍵值對

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

1

我想你可以閱讀第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建議很簡單,我嘗試了它,並讓它按我的想象工作!謝謝你的回答!你幫助我更好地理解了這個概念,併爲以後的不同方法提供了想法。 –