简体   繁体   中英

Hashmap in Hashmap Search

Map<Integer, HashMap<String, Integer>> map = new HashMap<Integer, 

HashMap<String, Integer>>();
map.put(1, new HashMap<>());
map.get(1).put("123",5);
map.get(1).put("124",3);
// i store (id, isbn, rate) in Hashmap in Hashmap

map.put(2, new HashMap<>());
map.get(2).put("123",5);
map.get(2).put("122",2);

How i get id from isbn?

Example i want to get id of the users who read isbn 123? Thanks.

You need to think by steps :

  • Iterate over the pairs of the first level Map
  • for each one, iterate over its pair (second level Map)
  • if you find a pair with the good isbn then save the ID of the 1-lvl Map

You can build a method as follows, and call like this

List<Integer> listId = getIdFromIsbn("123", map);
static List<Integer> getIdFromIsbn(String isbn, Map<Integer, Map<String, Integer>> map) {
    List<Integer> list = new ArrayList<>();

    for (Map.Entry<Integer, Map<String, Integer>> entry : map.entrySet()) {
        Map<String, Integer> value = entry.getValue();
        for (Map.Entry<String, Integer> subEntry : value.entrySet()) {
            if (subEntry.getKey().equals(isbn)) {
                list.add(entry.getKey());
            }
        }
    }

    return list;
}

Using Stream and lambdas, it would look like :

static List<Integer> getIdFromIsbn(String isbn, Map<Integer, Map<String, Integer>> map) {
    return map.entrySet()                           // Set<Entry<Integer,Map<String,Integer>>>
            .stream()                               // Stream<Entry<Integer,Map<String,Integer>>>
            .flatMap(entry -> entry.getValue().entrySet() // Set<Entry<String,Integer>>
                    .stream()                             // Stream<Entry<String,Integer>>
                    .map(Map.Entry::getKey)               // Stream<String> 
                    .filter(isbn::equals)                 // Stream<String> 
                    .map(subEntry -> entry.getKey()))     // Stream<Integer>
            .collect(Collectors.toList());                // List<Integer>
}

You can iterate the maps directly:

List<Integer> idsFound = new ArrayList<>(); 
map.forEach((id, innerMap) -> innerMap.forEach((isbn, rate) -> {
    if (isbn.equals(isbnToMatch)) idsFound.add(id);
}));

Iterate over the first level map, then iterate over the nested map to perform your check.

Following code section prints all the id for a given targetIsbn

    for (Entry<Integer, HashMap<String, Integer>> entry : map.entrySet()) {
        int id = entry.getKey();
        Map<String, Integer> value = entry.getValue();

        for (Entry<String, Integer> subEntry : value.entrySet()) {
            String isbn = subEntry.getKey();
            int rate = subEntry.getValue();
            if (isbn.equals(targetIsbn))
                System.out.println("given isbn found for id " + id);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM