JAVA: Preventing Duplicate Entries to an ArrayList

前端 未结 6 1651

I am trying to prevent duplicate entries from being added to an ArrayList as the list is being populated whilst reading through each line of a file. Each line of the file is

6条回答
  •  走了就别回头了
    2021-01-12 01:02

    It sounds like what you really want is a Set>

    Set> pairs = ...
    try(BufferedReader br = ... ) {
        for(String line; (line = br.readLine()) != null;) 
            pairs.add(new HashSet(Arrays.asList(line.split(" ")));
    }
    

    This creates a collection of pairs without duplicates regardless of the order of the words.

提交回复
热议问题