Skip to content

Instantly share code, notes, and snippets.

@puneetlakhina
Created December 2, 2011 20:01
Show Gist options
  • Save puneetlakhina/1424640 to your computer and use it in GitHub Desktop.
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
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);
}
}
@puneetlakhina
Copy link
Author

AbstractMap.SimpleEntry is public starting from Java 1.6, so this only works with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment