Created
December 2, 2011 20:01
-
-
Save puneetlakhina/1424640 to your computer and use it in GitHub Desktop.
A utility of collection functions. 1. Denormalize a java Map<K,List<T>> into a list of entries 2. firstOrNull in a list
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
private <K,T> List<Map.Entry<K, T>> denormalizeMap(Map<K,List<T>> nestedListMap) { | |
List<Map.Entry<K, T>> denormalizedEntryList = new ArrayList<Map.Entry<K,T>>(); | |
for(Map.Entry<K, List<T>> entry:nestedListMap.entrySet()) { | |
for(T t:entry.getValue()) { | |
denormalizedEntryList.add(new AbstractMap.SimpleEntry<K,T>(entry.getKey(), t)); | |
} | |
} | |
return denormalizedEntryList; | |
} | |
public static <T> T firstOrNull(List<T> list) { | |
if(list == null || list.isEmpty()) { | |
return null; | |
} else { | |
return list.get(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AbstractMap.SimpleEntry is public starting from Java 1.6, so this only works with that.