Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the code
Java
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 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
Java
base.values().iterator()
but i get
java.util.HashMap$ValueIterator@e9e54c2
java.util.HashMap$ValueIterator@65ab7765
Posted
Updated 9-Jun-19 22:17pm
v3
Comments
Richard MacCutchan 2-Mar-17 3:22am    
You need to extract each factory object and compare them against each other. The forEach construct may not be the best choice of operation.
soulbog 2-Mar-17 4:54am    
yes but how? that i dont know
Richard MacCutchan 2-Mar-17 5:39am    
Probably better to use an iterator as described in HashMap in Java with Example[^].
soulbog 2-Mar-17 5:47am    
if use that i need to make a entry and iterator. i posted a shorter solution in answear..

found 2 solution now..

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

and the second one is
Java
for (factory t1:base.values()){
              System.out.println(t1.getA() + ", " + t1.getB());
          }

if somoane knows a better one pls let me know
 
Share this answer
 
Comments
Richard MacCutchan 2-Mar-17 5:56am    
Do not post extra information as Solutions, edit your question. It just means people think you have been answered and there is no need to look at your question.

Also, I am still not sure exactly what problem you are trying to solve.
soulbog 2-Mar-17 6:19am    
The problem was figuring out how to get the value of a b from inside factory from the hash map . Because when I add 2 entityId and base.put(entityId, new factory(entityId,arg1)); it creates 2 factory
and the solutions I posted gives me the values that i need, but just prints them but I can easily compare them if i have them
Hello, to completely understand how Map and its methods work you can check this example
Java Map Example
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900