Print specific values ​​with Collections.frequency ()

I have an array as follows:

int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};

What I wanted to do was find duplicate values ​​and print them.

So my way of doing this was to convert to ArrayList, then to Setand use streamto Set.

ArrayList<Integer> list = new ArrayList<>(array.length);
for (int i = 0; i < array.length; i++) {
    list.add(array[i]);
}

Set<Integer> dup = new HashSet<>(list);

Then I used streamto scroll it and print the values ​​with Collections.frequency.

dup.stream().forEach((key) -> {
            System.out.println(key + ": " + Collections.frequency(list, key));
        });

Which, of course, prints them all, even if there is only one counter.

I thought to add in if(key > 1), but it is a value that I do not want to use.

How can I get the value in this instance to print only where value > 2.

I could add:

int check = Collections.frequency(list, key);
            if (check > 1) {

but it duplicates Collections.frequency(list, key)in streamand is pretty ugly.

+6
10

, filter, , 2:

dup.stream()
       .filter(t -> Collections.frequency(list, t) > 2)
       .forEach(key -> System.out.println(key + ": " + Collections.frequency(list, key)));

:

11: 4

:

Set Collections.frequency, :

Integer[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};
Arrays.stream(array).collect(Collectors.groupingBy(p -> p, Collectors.counting()))
        .entrySet().stream().filter(t -> t.getValue() > 1)
        .forEach(key -> System.out.println(key.getKey() + ": " + key.getValue()));

48: 2
17: 2
11: 4
+6

dup Collections.frequency :

Integer[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};
List<Integer> list = Arrays.asList(array);
Arrays.stream(array).collect(Collectors.toSet())
  .stream()
  .map(v -> new SimpleEntry<>(v, Collections.frequency(list, v)))
  .filter(v -> v.getValue() > 1)
  .forEach(v -> System.out.println(v.getKey() + ":" + v.getValue()));
+4

:

int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};

List<Integer> list = Arrays.stream(array).boxed().collect(Collectors.toList());

// print values appearing more than once
list.stream().filter(i -> Collections.frequency(list, i) >1)
        .collect(Collectors.toSet()).forEach(System.out::println);

:

48  
17  
11  

, - :

int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};

List<Integer> list = Arrays.stream(array).boxed().collect(Collectors.toList());

list.stream()
    .filter(i -> Collections.frequency(list, i) >1)
    .collect(Collectors.toMap(identity(), v -> 1, Integer::sum))
    .forEach((x, y) -> System.out.println("Key: " + x +", occurence: "+ y));

Key: 48, occurence: 2
Key: 17, occurence: 2
Key: 11, occurence: 4
+2
package various;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Frequency {
    public static void main(String[] args) {
        int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};
        ArrayList<Integer> list = new ArrayList<>(array.length);
        for (int i = 0; i < array.length; i++) {
            list.add(array[i]);
        }

        Set<Integer> dup = new HashSet<>(list);

        dup.stream().forEach((key) -> {
            System.out.print((Collections.frequency(list, key) > 2) ? key + ": " + Collections.frequency(list, key) +"\n" : "");
        });
    }
}

:

11: 4
+2
HashMap<Integer, Integer> hashmap = new HashMap<>();
for (int i : array) {
    if (hashmap.containsKey(i)) {
        hashmap.put(i, hashmap.get(i) + 1);
    } else {
        hashmap.put(i, 1);
    }
}

.

{48=2, 17=2, 33=1, 18=1, 22=1, 11=4, 29=1, 14=1}

EDIT: ,

hashmap.entrySet().stream().filter((entry) -> ((int) entry.getValue() >= 2)).forEach((entry) -> {
    System.out.println(entry.getKey() + ":" + entry.getValue());
});

:

48:2
17:2
11:4
+2

, Java, Stream.collect:

<R> R collect(Supplier<R> supplier,
    BiConsumer<R,? super T> accumulator,
    BiConsumer<R,R> combiner)

:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Program {
    public static void main(final String[] args) {
        // Input.
        final int[] inputArray = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};

        // Processing.
        final DuplicateCounter<Integer> duplicateCounter = Arrays.stream(inputArray)
            .collect(
                DuplicateCounter<Integer>::new,
                DuplicateCounter<Integer>::accept,
                DuplicateCounter<Integer>::combine
            );

        // Output.
        System.out.println(duplicateCounter.getDuplicateCountMap());
    }

    private static final class DuplicateCounter<T> implements Consumer<T> {
        private final Map<T, Integer> countMap = new HashMap<>();

        public void accept(final T obj) {
            this.incrementCounter(obj, 1);
        }

        public void combine(final DuplicateCounter<T> other) {
            other.countMap.entrySet()
                .forEach(entry -> this.incrementCounter(entry.getKey(), entry.getValue()));
        }

        public Map<T, Integer> getDuplicateCountMap() {
            return this.countMap.entrySet()
                .stream()
                .filter(entry -> entry.getValue() > 1)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }

        private void incrementCounter(final T obj, int increment) {
            final Integer counter = this.countMap.getOrDefault(obj, 0);
            this.countMap.put(obj, counter + increment);
        }
    }
}

:

{48 = 2, 17 = 2, 11 = 4}

:

+2

:

HashMap<Integer, Long> result = Arrays.stream(array)
            .boxed()
            .collect(Collectors.collectingAndThen(Collectors.groupingBy(
                    Function.identity(),
                    HashMap::new,
                    Collectors.counting()),
                    map -> {
                        map.entrySet().removeIf(x -> x.getValue() == 1);
                        return map;
                    }));

Btw - , , Collections.frequency ( )

+2

Collections.frequency , , . , O(n^2), .. , .

, , :

int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};

Map<Integer, Long> occurrences = Arrays.stream(array)
    .boxed()
    .collect(Collectors.groupingBy(
        Function.identity(), 
        Collectors.counting()));

, 1 , , 1:

occurrences.values().removeIf(v -> v == 1);

, :

System.out.println(occurrences);

:

{48=2, 17=2, 11=4}

, :

occurrences.forEach((k, v) -> System.out.println(k + ": "+ v));

, :

Map<Integer, Long> occurrences = new HashMap<>();
for (int n : array) occurrences.merge(n, 1L, Long::sum);

, :

occurrences.values().removeIf(v -> v == 1);
+2
source

You can use Filter:

dup.stream().filter(key -> Collections.frequency(list,key)>2).forEach(System.out::println);
+1
source

You do not need to convert it to an ArrayList. Take a look at this:

HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < array.length; i++) {
    if(set.contains(array[i]))
        System.out.println(array[i]);
    else
        set.add(array[i]);
}
+1
source

All Articles