1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| public class dateos
{
/**
* @param args
*/
public static void main(String[] args)
{
Map <String, Collection<String>> map = new HashMap<>();
Collection<String> c1 = new ArrayList<>();
c1.add("TOTO");
c1.add("TUTO");
c1.add("TJTO");
c1.add("TKTO");
Collection<String> c2 = new ArrayList<>();
c2.add("AOTO");
c2.add("AUTO");
c2.add("AJTO");
c2.add("AKTO");
map.put("CLE1", c1);
map.put("CLE2", c2);
System.out.println("Key=" +findKey(map, "AJTO"));
System.out.println("Key=" +findKeyM(map, "TKTO"));
}
/**
* @param map
* @return
*/
private static String findKey(Map<String, Collection<String>> map, String valeur)
{
Set<Entry<String, Collection<String>>> entries = map.entrySet();
for (Iterator<Entry<String, Collection<String>>> iterator = entries.iterator(); iterator.hasNext();)
{
Entry<String, Collection<String>> entry = iterator.next();
if (entry.getValue().stream().filter(e -> e.equals(valeur)).count()!=0)
return entry.getKey();
}
return null;
}
// OU
private static <T, U> T findKeyM(Map<T, Collection<U>> map, String valeur)
{
Set<Entry<T, Collection<U>>> entries = map.entrySet();
for (Iterator<Entry<T, Collection<U>>> iterator = entries.iterator(); iterator.hasNext();)
{
Entry<T, Collection<U>> entry = iterator.next();
if (entry.getValue().stream().filter(e -> e.equals(valeur)).count()!=0)
return entry.getKey();
}
return null;
}
} |
Partager