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);
我如何从isbn获取ID?
示例我想获取阅读isbn 123的用户的ID? 谢谢。
答案 0 :(得分:4)
您需要按步骤进行思考:
isbn
好的商品,请保存1-lvl地图的ID 您可以按照以下方法构建方法,并像这样调用
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;
}
使用Stream
和lambda,它看起来像:
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>
}
答案 1 :(得分:1)
您可以直接迭代地图:
SELECT dt2.id,
dt2.NAME,
dt2.state,
dt2.type,
dt2.count
FROM (SELECT dt1.id,
dt1.NAME,
dt1.state,
dt1.type,
dt1.count,
Row_number()
OVER (
partition BY dt1.state
ORDER BY dt1.count DESC) AS row_num
FROM (SELECT c.id,
c.NAME,
c.state,
t.NAME AS type,
Count(*) AS count
FROM bookings_facilities AS f
JOIN bookings AS b
ON b.id = f.booking_id
JOIN clients AS c
ON c.id = b.client_id
JOIN client_types AS t
ON c.type = t.id
WHERE t.NAME = 'School'
GROUP BY c.id,
c.NAME,
c.state,
type) AS dt1) AS dt2
WHERE dt2.row_num = 1
答案 2 :(得分:0)
遍历第一级地图,然后遍历嵌套地图以执行检查。
以下代码部分将打印给定id
的所有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);
}
}