简体   繁体   English

如何在Java中的Same HashMap中查看多个键之间的公共值?

[英]How to view the common Values between multiple keys in Same HashMap in java?

HashMap<Integer,ArrayList<String>> = new HashMap<Integer,ArrayList<String>>(); 
ArrayList<constructor> frndList = new ArrayList<constructor> ();

In single hash map i will use the integer value as key and for that key i will be adding multiple values using list 在单个哈希图中,我将使用整数值作为键,对于该键,我将使用list添加多个值
now my question is key 1 having two values that same value is available in key 3 then i have to view that? 现在我的问题是键1具有两个值,键3中具有相同的值,那么我必须查看吗?

You have to manually loop inside your HashMap 您必须手动在HashMap内部循环

dupl = null;
for(ek:map.keys()){
    if(ek.get(ek).contains(my_value)){
        dupl = ek.get(ek)
        break;
    }
}

if dupl is not null, you have a value in key ek 如果dupl不为null,则键ek中有一个值

System.out.println("Enter first key");
    int f=scan.nextInt();
    System.out.println("Enter second key");
    int s=scan.nextInt();
    ArrayList<String> t1=new ArrayList<String>();
    ArrayList<String> t2=new ArrayList<String>();
    ArrayList<String> mf=new ArrayList<String>();
    t1.addAll(map.get(f));
    t2.addAll(map.get(s));
    for(String temp1:t1)
    {
        if(t2.contains(temp1))
        {
            mf.add(temp1);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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