0

I want to find the the recipes from the users provided ingredients and tell the user the missing ingredient from the main recipe's ingredients how can i find the missing ingredients ? i mean how should I query to to comparing ?

      {
          "ingredients":[
                          {"title":"apple"},
                          {"title":"cheese"},
                          {"title":"banana"},
                          {"title":"orange"},
                     ]
      } 

The second would be :

[
    {"title":"apple"},
    {"title":"cheese"},
]

It should give us the missing two others like :

{"title":"banana"},
{"title":"orange"},
Mehdi dev
  • 33
  • 6
  • These are not java objects, so: can you add some more text to your post to explain _what_ you're trying to compare? It looks like JSON syntax, but JSON is pure string data, comparing them _as strings_ is incredibly silly, so at the very least you're presumably trying to compare parsed JSON, in which case: please talk about how you've already started trying to do what it is you mention you're trying to do. – Mike 'Pomax' Kamermans May 27 '20 at 22:07
  • If these are the entries in some Java HashMap, then you can always do a lookup on the value set of these two maps and compare them and collect the ones missing from the other in another collection – Kashif May 27 '20 at 22:14
  • Is your question related to MongoDB document in a collection? Are you trying to find the missing ingredients documents for given set of supplied ingredients? Are you using MongoDB Java Driver? – prasad_ May 28 '20 at 01:02

1 Answers1

1

I'm not sure what objects you intend to use, but if each object possessed a Set of these values, you could find the difference as per Prabhaker A's answer here like so:

Set<String> objectASet = new HashSet<>(Arrays.asList("apple", "cheese", "banana", "orange"));
Set<String> objectBSet = new HashSet<>(Arrays.asList("apple", "cheese"));

Set<String> differenceSet = objectASet.removall(objectBSet);

This will change the values of object A's set, however, so a copy will need to be made to preserve those values.

paullc
  • 27
  • 6