javac#code-conversion

How to convert a condition check from C# to Java?


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...


Solution

  • 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(), "");