fjtirado commented on code in PR #3774:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3774#discussion_r1840238102


##########
kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java:
##########
@@ -251,4 +256,19 @@ private static ModelMetaData 
getModelMetadata(WorkflowProcess process, Class<?>
         return new ModelMetaData(process.getId(), 
modelClass.getPackage().getName(), modelClass.getSimpleName(), 
KogitoWorkflowProcess.PUBLIC_VISIBILITY,
                 VariableDeclarations.of(Collections.emptyMap()), false);
     }
+
+    public static <T, V> Map<V, Integer> findDuplicates(List<T> items, 
Function<T, V> converter) {
+        if (items == null) {
+            return Map.of();
+        }
+        Map<V, Integer> duplicates = new LinkedHashMap<>();
+        Set<V> helper = new HashSet<>();
+        items.forEach(item -> {
+            V toAdd = converter.apply(item);
+            if (!helper.add(toAdd)) {
+                duplicates.compute(toAdd, (k, v) -> v == null ? 2 : ++v);

Review Comment:
   Although the helper set is not stricly needed, since you can  remove not 
duplicates from the map after the main loop is completed
   `duplicates().values().removeIf ( v -> v == 1);`
   it will slighly affects the performace of the most common case. You have to 
remove original number - duplicate elements from the map. If duplicate elements 
is 0 (the most common case) you remove the original number. 
   It can be argued thad adding the helper Set consumes more memory, but this 
is not completely true, because at the end a HashSet is a "capped" HashMap. 
Since the number of Map key buckets depends on the number of duplicates, with a 
helper map you have original number + duplicate number buckets, without it you 
have original number buckets. So you only have the duplicate buckets as 
additional memory (precisely the addtional memory that will be preserved when 
the method returns) and as benefit you save the removal.  
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to