All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.kronicle.utils.MapComparator Maven / Gradle / Ivy

Go to download

Kronicle utils library that contains utility classes for use by Kronicle and its plugins

There is a newer version: 0.1.331
Show newest version
package tech.kronicle.utils;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class MapComparator, V extends Comparable> implements Comparator> {

    @Override
    public int compare(Map o1, Map o2) {
        Set allKeys = new HashSet<>();
        allKeys.addAll(o1.keySet());
        allKeys.addAll(o2.keySet());
        List sortedAllKeys = allKeys.stream().sorted().collect(Collectors.toList());

        for (K key : sortedAllKeys) {
            if (!o1.containsKey(key)) {
                return 1;
            }

            if (!o2.containsKey(key)) {
                return -1;
            }

            int result = o1.get(key).compareTo(o2.get(key));

            if (result != 0) {
                return result;
            }
        }

        return 0;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy