1

I am attempting to convert this line of code from C# TO Java and I am having quite a hard time wrapping my head around it.

isEqual = !dbCertDict.Keys.Any(x => !String.Equals(dbCertDict[x], requestCertDict.ContainsKey(x) ? requestCertDict[x] : "", StringComparison.OrdinalIgnoreCase));

Originally, the dbCertDict was of type Dictionary<string, string>, it is now of type HashMap<String, String>. Essentially I want to write in Java a function that will set isEqual to true if there is a match for all of the items in both dbCertDict and requestCertDict. And I want it to return false if there are any mismatches.

At least, that's what I think this line of C# code is doing...Any help is appreciated!

I have tried to search for an equivalent function to Any() but the closest that I could find was to replace dbCertDict.Keys.Any with dbCertDict.keySet().contains() but they are not the same unfortunately...

Flats
  • 81
  • 1
  • 13
  • @MaythamFahmi I suppose nothing. Just thought I would use something available in an existing library if I could. If I need to, I could definitely write my own, but I am asking this in case I do not need to – Flats May 26 '22 at 21:18
  • 1
    I got you, that is fair enough – Maytham Fahmi May 26 '22 at 21:19

1 Answers1

4
isEqual = !dbCertDict.Keys.Any(x => 
!String.Equals(dbCertDict[x], requestCertDict.ContainsKey(x) ?
requestCertDict[x] : "", StringComparison.OrdinalIgnoreCase));

boolean isEqual = !dbCertDict.keySet().stream().anyMatch(x ->
dbCertDict.get(x).equalsIgnoreCase(requestCertDict.containsKey(x) ?
requestCertDict.get(x) : ""));

or something along those lines.

The latter can be tightened to

boolean isEqual = dbCertDict.entrySet().stream().noneMatch(x -> 
x.getValue().equalsIgnoreCase(requestCertDict.getOrDefault(x.getKey(), "");
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Perfect! I didn't realize that Stream had a function like anyMatch(), this will probably work. I'll accept once I test it :) – Flats May 26 '22 at 21:28