Java hashmap问题 [英] Java hashmap question

查看:86
本文介绍了Java hashmap问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码

I have the code

import java.util.*;

class factory{
    int a;
    int b;
    public factory(int a,int b){
        this.a = a;
        this.b = b;

    }

    public int getA() {
        return a;
    }

    public int getB() {
        return b;
    }
    
}
public class JavaApplication2 {

    public static void main(String[] args) {

        Map<Integer,factory> base = new HashMap<>();
        Scanner in;
        in = new Scanner(System.in);
        int entityCount = in.nextInt();
            for (int i = 0; i < entityCount; i++) {
                int entityId = in.nextInt();
                int arg1 = in.nextInt();

                base.put(entityId, new factory(entityId,arg1));
            }
            base.values().forEach((factory _item) -> {
                HERE if
        });
    }
    
}



i加上例子的值

2

1 2

3 4

如何使用entityid 1值b(即4)检查entityId 0值a(即1)的值。


这张照片的b $ b你会发现我想要比较的具体内容

http://oi66.tinypic.com/j7enar.jpg



我尝试过:



i试试


i add the value for exemple
2
1 2
3 4
how do i check the value from the entityId 0 value a(which is 1) with the entityid 1 value b(which is 4).

on this phot you will find more specific what i want to compare
http://oi66.tinypic.com/j7enar.jpg

What I have tried:

i have try

base.values().iterator()

但我得到

java.util.HashMap$ValueIterator@e9e54c2
java.util.HashMap$ValueIterator@65ab7765

推荐答案

ValueIterator @ e9e54c2
java.util.HashMap
ValueIterator@e9e54c2 java.util.HashMap


ValueIterator @ 65ab7765
ValueIterator@65ab7765


现在找到2个解决方案..



found 2 solution now..

base.values().forEach((factory _item) -> {
                System.out.println(_item.getA()+ " " _item.getB());
               
        });



,第二个是


and the second one is

for (factory t1:base.values()){
              System.out.println(t1.getA() + ", " + t1.getB());
          }



如果somoane知道更好的请告诉我


if somoane knows a better one pls let me know


这篇关于Java hashmap问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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