Created
December 31, 2019 22:32
-
-
Save dfparker2002/d20ee337f1aadb7910b9565da71d898c to your computer and use it in GitHub Desktop.
Demo different approaches to get count of duplicated elements in an arrayList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src: https://github.com/eugenp/tutorials/blob/85922e3a35f492cd8a6faba254f150ef98f7a588/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/duplicatescounter/DuplicatesCounter.java | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
/** | |
* Demo different approaches to get count of duplicated elements in an | |
* arrayList | |
*/ | |
public class DuplicatesCounter { | |
public static <T> Map<T, Long> countByClassicalLoop(List<T> inputList) { | |
Map<T, Long> resultMap = new HashMap<>(); | |
for (T element : inputList) { | |
if (resultMap.containsKey(element)) { | |
resultMap.put(element, resultMap.get(element) + 1L); | |
} else { | |
resultMap.put(element, 1L); | |
} | |
} | |
return resultMap; | |
} | |
public static <T> Map<T, Long> countByForEachLoopWithGetOrDefault(List<T> inputList) { | |
Map<T, Long> resultMap = new HashMap<>(); | |
inputList.forEach(e -> resultMap.put(e, resultMap.getOrDefault(e, 0L) + 1L)); | |
return resultMap; | |
} | |
public static <T> Map<T, Long> countByForEachLoopWithMapCompute(List<T> inputList) { | |
Map<T, Long> resultMap = new HashMap<>(); | |
inputList.forEach(e -> resultMap.compute(e, (k, v) -> v == null ? 1L : v + 1L)); | |
return resultMap; | |
} | |
public static <T> Map<T, Long> countByForEachLoopWithMapMerge(List<T> inputList) { | |
Map<T, Long> resultMap = new HashMap<>(); | |
inputList.forEach(e -> resultMap.merge(e, 1L, Long::sum)); | |
return resultMap; | |
} | |
public static <T> Map<T, Long> countByStreamToMap(List<T> inputList) { | |
return inputList.stream().collect(Collectors.toMap(Function.identity(), v -> 1L, Long::sum)); | |
} | |
public static <T> Map<T, Long> countByStreamGroupBy(List<T> inputList) { | |
return inputList.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment