0

i have a problem with object deserialized from a yaml. I did a group of classes to model the yaml file so when i read the file i can map the content into an object. Now, the question is that i have the same object, one in the List and one out but for the runtime environment they are different each other with the nude equals and not with the attributes comparison. Here there is the code's fragment. Thanks a lot.

public String changeKey(String richiedente, String apikey) throws IOException {

    ObjectMapper mapper= new ObjectMapper(new YAMLFactory());

    Yaml yaml = mapper.readValue(new File(domainYaml), new TypeReference<Yaml>() {});

    String newApiKey = null;

    YamlSistRichiedenti sistemaRichiedenteOld = new YamlSistRichiedenti(richiedente, apikey);

    System.out.println(yaml.server.sistemiRichiedenti.contains(sistemaRichiedenteOld));
    System.out.println(yaml.server.sistemiRichiedenti.get(0).equals(sistemaRichiedenteOld));
    System.out.println(yaml.server.sistemiRichiedenti.get(0).apikey.equals(sistemaRichiedenteOld.apikey));
    System.out.println(yaml.server.sistemiRichiedenti.get(0).richiedente.equals(sistemaRichiedenteOld.richiedente));

That should print true at all 4 printing examples but equals and contains print false.

Here there is the console printing

Here there is the console printing

1
  • Welcome to SO! As it is, this question does not enable us to help you solving your problem. It is missing a clear problem statement – you show some code but do not tell us what it does (versus what it should do). I also don't really understand the problem statement in your text. Then, Yaml is the main class from SnakeYaml, but you seem to have created an own class with that name, that's confusing and you should change it so that it's more clear what you're doing. Finally, you should create an MCVE so that we can reproduce what you're doing.
    – flyx
    Mar 11, 2019 at 10:15

1 Answer 1

1

I fixed overriding the equals method then also the contains method came to work again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.