简体   繁体   English

使用命令CASE_INSENSITIVE_ORDER按键对TreeMap进行排序

[英]Sort TreeMap by key using order CASE_INSENSITIVE_ORDER

How can I sort a TreeMap<String, String> in CASE_INSENSITIVE_ORDER based on key. 如何根据键在CASE_INSENSITIVE_ORDERTreeMap<String, String>进行排序。

Currently, by default, it is sorted like: 当前,默认情况下,其排序方式如下:

ABCD abcd ABCD abcd

I want it to be like: 我希望它像:

a A b B c C d D a A b B c C d D

The String.CASE_INSENSITIVE_ORDER - comparator does not sort as required in the question. String.CASE_INSENSITIVE_ORDER比较器未按问题的要求排序。 The result that was asked for was an order like 要求的结果是一个类似的命令

a, A, b, B ... a,a,b,B ...

Instead it handles "a" and "A" as "equal", which means an entry with key "a" would override an entry with the key "A" in the sorted-map. 相反,它将“ a”和“ A”视为“等于”,这意味着在排序映射中具有键“ a”的条目将覆盖具有键“ A”的条目。 The outcome would rather be something like "a, B", depending on what was added to the map last. 根据上一次在地图上添加的内容,结果可能更像“ a,B”。

One way to achieve the behaviour would be to use a custom-comparator like this: 实现此行为的一种方法是使用类似这样的自定义比较器:

Comparator<String> comparator = new Comparator<String>() {

    public int compare(String s1, String s2) {
        int n1 = s1.length();
        int n2 = s2.length();
        int min = Math.min(n1, n2);
        for (int i = 0; i < min; i++) {
            char c1 = s1.charAt(i);
            char c2 = s2.charAt(i);
            if (c1 != c2) {
                if (isAtoZ(c1) && isAtoZ(c2)) {
                    return getSortPosition(c1) - getSortPosition(c2);
                }
                return c1 - c2;
            }
        }
        return n1 - n2;
    }


    private boolean isAtoZ(char c) {
        return c > 64 && c < 123;
    }

    private int getSortPosition(char c) {
        if (c < 91) {
            // upper case
            return 2 * (c - 64); // A = 2, B = 4 ...
        } else if (c > 96) {
            // lower case
            return (2 * (c - 96)) - 1; // a = 1, b = 3 ...
        } else {
            // between Z and a: [, /, ], ^, _, `
            return c; // higher than the value for 'z'
        }
    }

};

调用String#CASE_INSENSITIVE_ORDERTreeMap构造函数,

Map<String, String> tree = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

您是否尝试过:

new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

您可以将不区分大小写的顺序比较器作为TreeMap 构造函数之一的参数传递:

TreeMap<String, String> treemap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

Use a Comparator in the TreeMap constructor. 在TreeMap构造函数中使用Comparator。 And String.CASE_INSENSITIVE_ORDER . 还有String.CASE_INSENSITIVE_ORDER

The accepted answer is wrong - 'a' and 'A' are the same key ignoring case. 接受的答案是错误的-'a'和'A'是忽略大小写的相同键。 Here is some toy code to prove this and provide the sketch of an alternative that answers the question. 这里有一些玩具代码可以证明这一点,并提供可以回答问题的替代方案的草图。

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapComparatorTest {
public static void main(String[] args) {
    TreeMap<String, Integer> caseInsensitive = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
    caseInsensitive.put("a", 97);
    caseInsensitive.put("A", 65);

    // prints [a]
    System.out.println(caseInsensitive.keySet());

    TreeMap<String, Integer> customComparator = new TreeMap<String, Integer>(new Comparator<String>(){

        @Override
        public int compare(String o1, String o2) {

            // assuming only single letters for a smaller example...
            Character c1 = o1.charAt(0);
            Character c2 = o2.charAt(0);
            Character upper1 = Character.toUpperCase(c1);
            Character upper2 = Character.toUpperCase(c2);
            if(c1.compareTo(c2) == 0){

                // exactly the same char
                return 0;
            }else if (upper1.compareTo(upper2) == 0){

                // return lowercase first for same upper chars
                return -c1.compareTo(c2);
            }else{

                // order alphabetically
                return upper1.compareTo(upper2);
            }
        }
    });
    customComparator.put("a", 97);
    customComparator.put("A", 65);
    customComparator.put("b", 98);
    customComparator.put("B", 66);

    // prints [a, A, b, B]
    System.out.println(customComparator.keySet());
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java TreeMap:无法按长度/值的降序对 TreeMap 键进行排序 - Java TreeMap : Unable to sort the TreeMap key in decreasing order of the length/value 如何以字母数字顺序对番石榴多图(键和值)进行排序(不区分大小写) - How to sort the guava multimap(both key and value) in alphanumeric order(case insensitive) 按字母顺序对 TreeMap 值进行排序 - Sort TreeMap Values In Alphabetical order 如何以不区分大小写的顺序对 Eclipse 中的导入语句进行排序? - How to sort import statements in Eclipse in case insensitive order? CursorLoader-SQL中的排序不区分大小写 - CursorLoader - order by in SQL is case insensitive 如何自定义TreeMap以降序对键进行排序? - How to Customize a TreeMap to sort keys in descending order? 如何在 Linux 系统上按字典顺序(不区分大小写)对文件\目录树进行排序 - How to sort file\directory tree in lexicographic order (case-insensitive) on Linux system 按字母顺序对 arraylist 进行排序(不区分大小写) - Sorting arraylist in alphabetical order (case insensitive) 如何对 TreeMap 值进行降序排序以及如何限制 output? - How to sort TreeMap values in descending order and how to limit the output? 最好使用TreeMap还是按顺序排序? - Better to use TreeMap or order by?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM