Deleting duplicate lines in a file using Java

后端 未结 14 577
予麋鹿
予麋鹿 2020-12-14 01:39

As part of a project I\'m working on, I\'d like to clean up a file I generate of duplicate line entries. These duplicates often won\'t occur near each other, however. I came

14条回答
  •  隐瞒了意图╮
    2020-12-14 02:02

    You could use Set in the Collections library to store unique, seen values as you read the file.

    Set uniqueStrings = new HashSet();
    
    // read your file, looping on newline, putting each line into variable 'thisLine'
    
        uniqueStrings.add(thisLine);
    
    // finish read
    
    for (String uniqueString:uniqueStrings) {
      // do your processing for each unique String
      // i.e. System.out.println(uniqueString);
    }
    

提交回复
热议问题