Remove duplicate numbers in Java 8 and get the sum

I am trying to remove duplicate numbers in java 8, including the number of links. For example, I have an array with values ​​similar to (2,3,2,5)

All numbers 2 should be deleted, and numbers should remain 3 and 5. Thus, the expected amount is 8. However, in my code below, it still gets 2. He deleted the duplicate number, but still kept the reference to the number.

Here is my code.

List<Integer> clearedNumbers = numbers.stream().distinct().collect(Collectors.toList());
    int sum = clearedNumbers.stream().mapToInt(Integer::intValue).sum();

The amount I get is 10 instead of 8.

+4
source share
5 answers

You can do a little smarter ... (whenever you have a duplicate, just map it with zerowith combiner):

int result = Stream.of(2, 3, 2, 5)
            .collect(Collectors.collectingAndThen(
                    Collectors.toMap(
                            Function.identity(),
                            Function.identity(),
                            (x, y) -> 0),
                    map -> map.values().stream().mapToInt(Integer::intValue).sum()))
+1
source

I would collect a count of each number and summarize only the singles:

 System.out.println(
         Stream.of(2,3,2,5)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .filter(e -> e.getValue() == 1)
                .mapToInt(Map.Entry::getKey)
                .sum());

8

+5

API Collections.Frequency,

 System.out.println(numbers.stream()
                .filter(i -> Collections.frequency(numbers, i)==1)
                .mapToInt(Integer::intValue)
                .sum());
0

The amount you receive is correct, because it removes duplicates and the operation of summation is performed, what you want is to delete all numbers with a frequency of more than one and perform the operation of summation. Try it.

List<Integer> numbers = new ArrayList<>();
    numbers.add(2);
    numbers.add(3);
    numbers.add(2);
    numbers.add(5);

    List<Integer> clearedNumbers = numbers.stream().filter(num -> {
        return Collections.frequency(numbers, num) == 1;
    }).collect(Collectors.toList());
    int sum = clearedNumbers.stream().mapToInt(Integer::intValue).sum();
    System.out.println(sum);
-1
source

Why not take a simple approach, because create another list of arrays without the current element and compare. If compare returns false, add them. See the code snippet below:

public static void main(String[] args) {
        List<Integer> a = new ArrayList<Integer>();
        List<Integer> b = new ArrayList<Integer>();
        a.add(2);
        a.add(2);
        a.add(3);
        a.add(5);
        int sum = 0;
        for(int i=0; i < a.size(); i++) {
            b.clear();
            for(int j = 0; j < a.size(); j++) {
                if(j != i) 
                    b.add(a.get(j));
            }
            if(! b.contains(a.get(i)))
                sum = sum + a.get(i);
        }
        System.out.println(sum);
    }

It can give the desired result.

-1
source

Source: https://habr.com/ru/post/1693860/


All Articles