Skip to content

Instantly share code, notes, and snippets.

@jotak
Created April 30, 2014 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jotak/277aef843126ef24bd01 to your computer and use it in GitHub Desktop.
Save jotak/277aef843126ef24bd01 to your computer and use it in GitHub Desktop.
That's collector
@SuppressWarnings("unchecked")
private static <T,U> Map<U, List<T>> formatMapClefEnsembleValeursAvecOuSansLimitateur(List<Map<String, Object>> liste,
String identifiant, String valeur, Integer limitation) {
final Map<U, List<T>> mapKeyValue = new HashMap<U, List<T>>();
final Map<U, Integer> mapLimitation = new HashMap<U, Integer>();
if (!isEmpty(liste)) {
final boolean ligneComplete = StrUtil.isEmpty(valeur);
for (final Map<String, ?> map : liste) {
final U key = (U) map.get(identifiant);
if (mapKeyValue.containsKey(key)) {
final Integer limitationEnCours = mapLimitation.get(key);
if (limitationEnCours < limitation || limitation == 0) {
final List<T> listeResult = mapKeyValue.get(key);
if (ligneComplete) {
listeResult.add((T)map);
} else {
listeResult.add((T)map.get(valeur));
}
mapKeyValue.put(key, listeResult);
mapLimitation.put(key, limitationEnCours + 1);
}
} else {
final List<T> listeResult = new ArrayList<T>();
if (ligneComplete) {
listeResult.add((T)map);
} else {
listeResult.add((T)map.get(valeur));
}
mapKeyValue.put(key, listeResult);
mapLimitation.put(key, 1);
}
}
}
return mapKeyValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment