The work requires such an object:

Map<RealProfile, ArrayList<ModelledProfile>> example = new HashMap<RealProfile, ArrayList<ModelledProfile>>(); 

Next, you need to determine whether the object of the RealProfile class is in this map or not , but not all fields should be compared.

How to implement it?

Here is the RealProfile class itself.

 public class RealProfile { private String idRealProfile; private Gender gender; private Age age; private Income income; // getters & setters } 

Gender, Age and Income are enums. I also need to determine if there is one among the keys that has the same fields as gender, age and income

  • If you need to search not by key, maybe you use the wrong data structure. And in general, go over the set of keys and compare. - VladD
  • I need a search by key: I need to search to know if the given realProfile is among the keys or not. You need to compare not all fields. I thought to apply the .contain method. But he, for some reason, gives false all the time - Stas0n
  • 2
    @ Stas0n you would bother to read the Java Collections Framework documentation, as well as the equals () and hashCode () methods. And then you ask ridiculous questions. There is an excellent resource for learning Java and standard libraries ( ) - a_gura

2 answers 2

RealProfile - key

ArrayList <ModelledProfile> - value

  1. find the object by key
  2. if you have, you return
  3. check the required fields
  • Can I just somehow rewrite the equals method? - Stas0n
  • There is a problem in one more: filled the map with 2 keys (rp and rp2) and it turned out that out.println (example.containsValue (rp2)); returns false - Stas0n

Definitely, you need to override the equals method of RealProfile and check the required fields in it, if they match, then return true. And can you see the code of your problem when filling the map with two keys?