0

I am following this post on getting distinct elements from ArrayList.

My code:

List<HpbAlertKeeperDTO> distinctAlertKeeperDTOs = alertKeeperDeviceDTO.getBizHpbAlertKeeperDTO().stream()
.filter(distinctByKey(HpbAlertKeeperDTO::getKeeperId)).collect(Collectors.toList());

and my distinctByKey is:

    protected static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }

But the error from the IDE is: it is highlighting the parameter of distinctByKey in the first code snippet.

Non-static method cannot be referenced from a static context

My understanding of this error message is: getKeeperId() is not a static method. But in my HpbAlertKeeperDTO is just a normal POJO, with getter and setter methods of a variable.

How do I get around this error? Please feel free to ask me to provide more details.

Thanks and please help.

AppleT
  • 61
  • 8
  • It's better if you can add your code as well – ray Sep 05 '21 at 17:12
  • Use this its same solution. `Set set =ConcurrentHashMap.newKeySet(); alertKeeperDeviceDTO.getBizHpbAlertKeeperDTO() .stream() .filter(a -> set.add(a.getKeeperId())).collect(Collectors.toList()); ` – navnath Sep 06 '21 at 02:49

0 Answers0