在哈希图中的数组列表中搜索值 [英] Search for a value in an arraylist within a hashmap

查看:90
本文介绍了在哈希图中的数组列表中搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ID作为键的哈希表,因为值具有一个具有更多ID的数组列表.

I have a hashmap that uses a ID as a key, as value has an arraylist with more ID.

我需要搜索ArrayList ID,但无需知道键的ID.

I need to do a search for an ArrayList ID, but without needing to know the ID of the key.

您如何进行搜索?

我需要在不知道其哈希映射键的数组列表中查找数字.

EDITT: I need to look for a number, inside the arraylist without knowing its hashmap key.

示例: 验证是否存在20,如果为true,则返回3333

Example: Validate if 20 exists, if true, return 3333

推荐答案

哈希表上的简单循环:

public static void main(String[] args) {
    Integer needle = 20;

    HashMap<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
    hm.put(1111, new ArrayList<Integer>());
    hm.get(1111).add(1);
    hm.get(1111).add(2);
    hm.get(1111).add(3);
    hm.get(1111).add(4);
    hm.get(1111).add(5);
    hm.get(1111).add(6);

    hm.put(2222, new ArrayList<Integer>());
    hm.get(2222).add(8);
    hm.get(2222).add(10);
    hm.get(2222).add(11);

    hm.put(3333, new ArrayList<Integer>());
    hm.get(3333).add(15);
    hm.get(3333).add(19);
    hm.get(3333).add(20);
    hm.get(3333).add(31);

    for (Entry<Integer, ArrayList<Integer>> entry : hm.entrySet()) {
        ArrayList<Integer> v = entry.getValue();
        if (v.contains(needle)){
            System.out.println(entry.getKey());
            break;
        }
    }
}

这篇关于在哈希图中的数组列表中搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆