Java循环遍历每个唯一值

时间:2016-03-02 09:48:15

标签: java loops csv unique divide

我有一个像这样的csv文件:

"user1","track1","player1"
-------------------------
"user1","track2","player2"
-------------------------
"user1","track3","player3"
-------------------------
"user1","track4","player4"
-------------------------
"user2","track2","player3"
-------------------------
.
.
"userN","trackM","playerX"

我需要做的是将与每个用户相关的轨道和播放器分成两半并将它们放在单独的文件中。 例如,对于user1,如果它有4行,我需要将它分为两​​部分(文件A中的前两行,文件B中的其余部分),并为所有用户重复相同的操作。 这是我到目前为止写的:

public static void main(String[] args) throws java.lang.Exception { 

    BufferedReader userlines = new BufferedReader(new FileReader("/Users/mona/Documents/Bolzano/Datasets/Lastfm_Matthias/lastfm_usertrackplayer.csv"));       
    String uLine = null;

    while ((uLine = userlines.readLine()) != null) {

        String[] userId = uLine.split(","); 
        ArrayList<String> list = new ArrayList<String>();       
        list.add(uLine);

    for(int i=0; i<=list.size();i++){       
    //  --> THIS FOR CONDITION IS MY PROBLEM,I need s.th like for(i=0; i<=(last unique userId (i.e., length of userId[i]) until it reaches the next unique userId)      
          //Divide the lines and put into two separate files           

            }
    }
    userlines.close();
}

抱歉,我知道它应该是简单的东西,但我真的找不到任何相关/类似问题谷歌搜索我的问题:( 有人可以帮帮我吗?

由于

2 个答案:

答案 0 :(得分:1)

您无法知道“先验”每个用户的行数。 所以你必须记住(例如在列表中)当前用户的所有行,直到你读到下一个用户。然后在两个文件中保存列表的内容。

清理列表,为下一个用户执行相同的操作。

修改

public static void main(String[] args) throws java.lang.Exception { 

    try(BufferedReader userlines = new BufferedReader(new FileReader("/Users/mona/Documents/Bolzano/Datasets/Lastfm_Matthias/lastfm_usertrackplayer.csv"));) {       
    String uLine = null;

    ArrayList<String> list = new ArrayList<String>();  
    String currentUserId = null;     
    while ((uLine = userlines.readLine()) != null) {

        String[] userData = uLine.split(","); 
        String userId = userData[0]; // <-- get User ID here
        if (userId.equals(currentUserId)) {
            // Do what ever you need while buffering same userId
        } else {
            // Save currentUserId in file
            yourSaveMethod(list);
            currentUserId = userId;
            list.clear();
        }
        list.add(uLine);

        }
    }
}

答案 1 :(得分:1)

您可以使用StringTokenizer class来解析数据。

示例:

    String str = "user1, track1, player1";
    StringTokenizer st = new StringTokenizer(str);

    System.out.println("---- Split by space ------");
    while (st.hasMoreElements()) {
        System.out.println(st.nextElement());
    }

    System.out.println("---- Split by comma ',' ------");
    StringTokenizer st2 = new StringTokenizer(str, ",");

    while (st2.hasMoreElements()) {
        System.out.println(st2.nextElement());
    }