0

I am trying to delete duplicates from arraylist :

public static List<List<String>> bigList = new ArrayList<>();
    for (int i = 0; i < bigList.size(); i++) {
        bigList.get(i).stream()
            .map(str -> new LinkedHashSet<>(Arrays.asList(str.split(","))))
            .distinct()
            .map(set -> set.stream().collect(Collectors.joining(",")))
            .collect(Collectors.toList());
    }
}

When executing the code, I still get duplicates in my list. I want to delete the duplicates from bigList and bigList.get(i).

12
  • 4
    You are not assigning the result of .collect(Collectors.toList()) to anything. So you are constructing a new list, which is just immediately thrown away. Commented May 14, 2020 at 23:38
  • 3
    @LSoft Wrap your line with bigList.set( i , … ) to replace the old list with the new list. Commented May 14, 2020 at 23:50
  • 1
    Nothing in the code you posted has anything to do with doubles. Commented May 14, 2020 at 23:57
  • 1
    Your for loop will not run at all; you do bigList = new ArrayList<>() and you loop on i < bigList.size() — well, bigList.size() is zero so there will be no iterations. Commented May 15, 2020 at 0:00
  • 1
    @BasilBourque instead of looping and calling get, followed by calling set, it’s much simpler to use bigList.replaceAll(list -> list.stream()….collect(Collectors.toList())); Alternatively, the OP could truly remove the duplicates from the lists, instead of creating new lists. Commented May 15, 2020 at 10:29

1 Answer 1

2

Capture the new list

Your code creates a new list each time through the for loop. But the new list is immediately lost, headed for garbage collection. You neglected to capture a reference to that newly-created list.

So, the solution is: Replace the old list stored as an element in the outer list with the new list. Wrap your stream line with bigList.set( i , … ).

public static List< List< String > > bigList = new ArrayList<>(); // A list of lists of strings.
…
    for (int i = 0; i < bigList.size(); i++) {
        bigList.set( 
            i ,       // Index of outer list, where the new list should go.
            bigList   
            .get(i)
            .stream()
            .map( str -> new LinkedHashSet<>( Arrays.asList( str.split( "," ) ) ) )
            .distinct()
            .map( set -> set.stream().collect( Collectors.joining( "," ) ) )
            .collect( Collectors.toList() )  // Produce a new list. Stored in place of the old list.
        ) ;
    }

For clarity, break the code into separate lines.

public static List< List< String > > bigList = new ArrayList<>(); // A list of lists of strings.
…
    for (int i = 0; i < bigList.size(); i++) {
        List< String > oldList = bigList.get( i ) ;
        List< String > newList = 
            oldList
            .stream()
            .map( str -> new LinkedHashSet<>( Arrays.asList( str.split( "," ) ) ) )
            .distinct()
            .map( set -> set.stream().collect( Collectors.joining( "," ) ) )
            .collect( Collectors.toList() )  // Produce a new list. Stored in place of the old list.
        ) ;
        bigList.set( i , newList ) ;  // Replace the old list with the newly-created list.
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.