I have a HashMap relating Keys to Strings, and I need to compare some of the Strings against each other. However, some of the Strings may or may not be in the HashMap.
Example: Let’s say I have 4 Strings that I plan to compare to each other if possible, but only 3 of them end up in the HashMap. How can I compare the Strings that are present without trying to compare them to the String that isn’t, and without doing a bunch of nested ifs and elses?
edit: Alohci’s solution was easy and fast, and it worked.
Loop through the .values collection of the HashMap Store the first entry. Compare each remaining entry with the stored one. As soon as you find one that doesn’t match, throw your error. If you reach the end of the loop then all the strings match.