private Map<Object, List<T>> collapseBucketsByCase(Map<Object, List<T>> elementsByProperty) { Map<String, List<Map.Entry<Object, List<T>>>> stringEntries = new HashMap<>(); Map<Object, List<T>> results = new HashMap<>(); // for strings first group them by there lowercase version for (Map.Entry<Object, List<T>> entry : elementsByProperty.entrySet()) { if (entry.getKey() instanceof String) { String lowerCaseKey = ((String) entry.getKey()).toLowerCase(); List<Map.Entry<Object, List<T>>> l = stringEntries.computeIfAbsent(lowerCaseKey, s -> new ArrayList<>()); l.add(entry); } else { results.put(entry.getKey(), entry.getValue()); } } // for strings find the best key (the one with the most entries) and use that as the bucket name for (Map.Entry<String, List<Map.Entry<Object, List<T>>>> entry : stringEntries.entrySet()) { results.put( findBestKey(entry.getValue()), entry.getValue().stream() .flatMap(l -> l.getValue().stream()) .collect(Collectors.toList()) ); } return results; }
private Map<Object, List<T>> collapseBucketsByCase(Map<Object, List<T>> elementsByProperty) { Map<String, List<Map.Entry<Object, List<T>>>> stringEntries = new HashMap<>(); Map<Object, List<T>> results = new HashMap<>(); // for strings first group them by there lowercase version for (Map.Entry<Object, List<T>> entry : elementsByProperty.entrySet()) { if (entry.getKey() instanceof String) { String lowerCaseKey = ((String) entry.getKey()).toLowerCase(); List<Map.Entry<Object, List<T>>> l = stringEntries.computeIfAbsent(lowerCaseKey, s -> new ArrayList<>()); l.add(entry); } else { results.put(entry.getKey(), entry.getValue()); } } // for strings find the best key (the one with the most entries) and use that as the bucket name for (Map.Entry<String, List<Map.Entry<Object, List<T>>>> entry : stringEntries.entrySet()) { results.put( findBestKey(entry.getValue()), entry.getValue().stream() .flatMap(l -> l.getValue().stream()) .collect(Collectors.toList()) ); } return results; }