Check two unaligned arrays to match as neatly as possible (Java)

There are quite a few confirmations of matching between sequences whose order is unknown.

Arrays obtained from DB are often uncertain. I wondered if it would be possible to express such an array match check with as little code as possible. I want to do it neatly, so the situation should be as follows

Must be a type that can be sorted by Stream.sorted ()

Simple types such as String and Integer can be sorted by Stream.sorted (). However, this is not the case. Also, there is no problem if you define a Comparator.

Verified to contain no nulls

NullPointerException.

Get only matches, mismatches

Needless to say here

Source code

Sample.java


public boolean isEqualsList(List<?> a, List<?> b){
    return Arrays.equals(a.stream().sorted().toArray(), b.stream().sorted().toArray());
}

bonus

Even if you define a Comparator, it seems to be just barely neat.

Sample.java


public boolean isEqualsList(Comparator comp, List<?> a, List<?> b){
    return Arrays.equals(a.stream().sorted(comp).toArray(), b.stream().sorted(comp).toArray());
}

Afterword

I was able to write in one sentence. It's unpleasant that toArray () is Object [], but it's just a match check, so there's no problem.

Recommended Posts

Check two unaligned arrays to match as neatly as possible (Java)
Avoid Java Calendar as much as possible
Java, arrays to start with beginners
[Processing × Java] How to use arrays
How to check Java installed on Mac