如何在Java中按键对映射进行排序,但如果键是(String+;numeric)的组合

如何在Java中按键对映射进行排序,但如果键是(String+;numeric)的组合,java,treemap,Java,Treemap,我已经创建了一个名为result的映射 在sortByKeys方法中,由于我的键是带有数值的字符串,我将它们转换为整数键类型映射,然后对它们进行排序 Map<String, String> unsortMap = new TreeMap<String, String>(); unsortMap.put("room~1", "e"); unsortMap.put("room~2", "y"); unsortMap.put("room~10", "n"); unsortMap

我已经创建了一个名为result的映射

在sortByKeys方法中,由于我的键是带有数值的字符串,我将它们转换为整数键类型映射,然后对它们进行排序

Map<String, String> unsortMap = new TreeMap<String, String>();
unsortMap.put("room~1", "e");
unsortMap.put("room~2", "y");
unsortMap.put("room~10", "n");
unsortMap.put("room~4", "j");
unsortMap.put("room~5", "m");
unsortMap.put("room~3", "f");

Set set2 = unsortMap.entrySet();
Iterator iterator2 = set2.iterator();

while (iterator2.hasNext()) {
     /* Iterate */
    Map.Entry me2 = (Map.Entry) iterator2.next();
    String key = (String) me2.getKey();
    Object value = (Object) me2.getValue();
    System.out.println("Key ==>" + key + " Value ==>" + value);
}

# Current Output:#
/* current result */
Key ==>room~1 Value ==>e 
Key ==>room~10 Value ==>n
Key ==>room~2 Value ==>y
Key ==>room~3 Value ==>f
Key ==>room~4 Value ==>j
Key ==>room~5 Value ==>m

#Expected O/p:#
/* required result */
Key ==>room~1 Value ==>e
Key ==>room~2 Value ==>y
Key ==>room~3 Value ==>f
Key ==>room~4 Value ==>j
Key ==>room~5 Value ==>m
Key ==>room~10 Value ==>n 
Map unsortMap=newtreemap();
不排序地图。放置(“房间~1”,“e”);
不排序地图。放置(“房间~2”,“y”);
不排序地图。放置(“房间~10”,“n”);
放置(“房间~4”,“j”);
不排序地图。放置(“房间~5”,“m”);
放置(“房间~3”,“f”);
Set set2=unsortMap.entrySet();
迭代器迭代器2=set2.Iterator();
while(iterator2.hasNext()){
/*迭代*/
Map.Entry me2=(Map.Entry)迭代器2.next();
String key=(String)me2.getKey();
对象值=(对象)me2.getValue();
System.out.println(“键==>”+Key+“值==>”+Value);
}
#电流输出:#
/*当前结果*/
钥匙==>房间~1值==>e
钥匙==>房间~10值==>n
键==>房间~2值==>y
钥匙==>房间~3值==>f
钥匙==>房间~4值==>j
钥匙==>房间~5值==>m
#预期O/p:#
/*要求的结果*/
钥匙==>房间~1值==>e
键==>房间~2值==>y
钥匙==>房间~3值==>f
钥匙==>房间~4值==>j
钥匙==>房间~5值==>m
钥匙==>房间~10值==>n

创建自定义密钥对象

public class Key implements Comparable<Key>{
    String name;
    int id;

    public Key(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public int compareTo(Key o) {
        if(Objects.equals(name, o.name)){
            return Integer.compare(id, o.id);
        }else{
            return name.compareTo(o.name);
        }
    }

    @Override
    public String toString() {
        return name +"~"+ id;
    }

    @Override
    public boolean equals(Object o){
    ...
    @Override
    public int hashCode(){
    ...
}
公共类密钥实现可比较{
字符串名;
int-id;
公钥(字符串名称,整数id){
this.name=名称;
this.id=id;
}
@凌驾
公共整数比较(键o){
if(Objects.equals(name,o.name)){
返回整数。比较(id,o.id);
}否则{
返回name.compareTo(o.name);
}
}
@凌驾
公共字符串toString(){
返回名称+“~”+id;
}
@凌驾
公共布尔等于(对象o){
...
@凌驾
公共int hashCode(){
...
}
然后像这样使用它:

    Map<Key, String> unsortMap = new TreeMap<>();
    unsortMap.put(new Key("room", 5), "e");
Map unsortMap=newtreemap();
放(新钥匙(“房间”,5),“e”);

但如果字符串始终为room,则应在键中使用它创建自定义键对象

public class Key implements Comparable<Key>{
    String name;
    int id;

    public Key(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public int compareTo(Key o) {
        if(Objects.equals(name, o.name)){
            return Integer.compare(id, o.id);
        }else{
            return name.compareTo(o.name);
        }
    }

    @Override
    public String toString() {
        return name +"~"+ id;
    }

    @Override
    public boolean equals(Object o){
    ...
    @Override
    public int hashCode(){
    ...
}
公共类密钥实现可比较{
字符串名;
int-id;
公钥(字符串名称,整数id){
this.name=名称;
this.id=id;
}
@凌驾
公共整数比较(键o){
if(Objects.equals(name,o.name)){
返回整数。比较(id,o.id);
}否则{
返回name.compareTo(o.name);
}
}
@凌驾
公共字符串toString(){
返回名称+“~”+id;
}
@凌驾
公共布尔等于(对象o){
...
@凌驾
公共int hashCode(){
...
}
然后像这样使用它:

    Map<Key, String> unsortMap = new TreeMap<>();
    unsortMap.put(new Key("room", 5), "e");
Map unsortMap=newtreemap();
放(新钥匙(“房间”,5),“e”);

但是,如果字符串总是空的,您应该在键中使用它,而不更改代码,您可以这样做。需要编写自己的自定义比较器。请始终记住,当您需要按照自己的方式排序时,您可以始终创建比较器登录

Map<String, String> unsortMap = new TreeMap<String, String>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                // TODO Auto-generated method stub

                int j=Integer.parseInt(o1.substring(o1.indexOf("~")+1));
                int k=Integer.parseInt(o2.substring(o1.indexOf("~")+1));
                return j-k;
            }

        });

unsortMap.put("room~1", "e");
unsortMap.put("room~2", "y");
unsortMap.put("room~10", "n");
unsortMap.put("room~4", "j");
unsortMap.put("room~5", "m");
unsortMap.put("room~3", "f");

Set set2 = unsortMap.entrySet();
Iterator iterator2 = set2.iterator();

while (iterator2.hasNext()) {
     /* Iterate */
    Map.Entry me2 = (Map.Entry) iterator2.next();
    String key = (String) me2.getKey();
    Object value = (Object) me2.getValue();
    System.out.println("Key ==>" + key + " Value ==>" + value);
}
Map unsortMap=newtreemap(newcomparator(){
@凌驾
公共整数比较(字符串o1、字符串o2){
//TODO自动生成的方法存根
int j=Integer.parseInt(o1.substring(o1.indexOf(“~”+1));
int k=Integer.parseInt(o2.substring(o1.indexOf(“~”)+1));
返回j-k;
}
});
不排序地图。放置(“房间~1”,“e”);
不排序地图。放置(“房间~2”,“y”);
不排序地图。放置(“房间~10”,“n”);
放置(“房间~4”,“j”);
不排序地图。放置(“房间~5”,“m”);
放置(“房间~3”,“f”);
Set set2=unsortMap.entrySet();
迭代器迭代器2=set2.Iterator();
while(iterator2.hasNext()){
/*迭代*/
Map.Entry me2=(Map.Entry)迭代器2.next();
String key=(String)me2.getKey();
对象值=(对象)me2.getValue();
System.out.println(“键==>”+Key+“值==>”+Value);
}

在不更改代码的情况下,您可以这样做。需要编写自己的自定义比较器。请始终记住,当您需要按照自己的方式进行排序时,您始终可以创建比较器登录

Map<String, String> unsortMap = new TreeMap<String, String>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                // TODO Auto-generated method stub

                int j=Integer.parseInt(o1.substring(o1.indexOf("~")+1));
                int k=Integer.parseInt(o2.substring(o1.indexOf("~")+1));
                return j-k;
            }

        });

unsortMap.put("room~1", "e");
unsortMap.put("room~2", "y");
unsortMap.put("room~10", "n");
unsortMap.put("room~4", "j");
unsortMap.put("room~5", "m");
unsortMap.put("room~3", "f");

Set set2 = unsortMap.entrySet();
Iterator iterator2 = set2.iterator();

while (iterator2.hasNext()) {
     /* Iterate */
    Map.Entry me2 = (Map.Entry) iterator2.next();
    String key = (String) me2.getKey();
    Object value = (Object) me2.getValue();
    System.out.println("Key ==>" + key + " Value ==>" + value);
}
Map unsortMap=newtreemap(newcomparator(){
@凌驾
公共整数比较(字符串o1、字符串o2){
//TODO自动生成的方法存根
int j=Integer.parseInt(o1.substring(o1.indexOf(“~”+1));
int k=Integer.parseInt(o2.substring(o1.indexOf(“~”)+1));
返回j-k;
}
});
不排序地图。放置(“房间~1”,“e”);
不排序地图。放置(“房间~2”,“y”);
不排序地图。放置(“房间~10”,“n”);
放置(“房间~4”,“j”);
不排序地图。放置(“房间~5”,“m”);
放置(“房间~3”,“f”);
Set set2=unsortMap.entrySet();
迭代器迭代器2=set2.Iterator();
while(iterator2.hasNext()){
/*迭代*/
Map.Entry me2=(Map.Entry)迭代器2.next();
String key=(String)me2.getKey();
对象值=(对象)me2.getValue();
System.out.println(“键==>”+Key+“值==>”+Value);
}

使用流和
LinkedHashMap
来维持秩序

    Map<String, String> unsortMap = new TreeMap<String, String>();
    unsortMap.put("room~1", "e");
    unsortMap.put("room~2", "y");
    unsortMap.put("room~10", "n");
    unsortMap.put("room~4", "j");
    unsortMap.put("room~5", "m");
    unsortMap.put("room~3", "f");
    Comparator<String> c = (s1, s2) -> Integer.parseInt(s1.split("~")[1])   - Integer.parseInt(s2.split("~")[1]);
    Map<String, String> sortedMap = unsortMap.keySet()
            .stream()
            .sorted(c)
            .collect(Collectors.toMap(k -> k, k -> unsortMap.get(k), (k, v) -> v, LinkedHashMap::new));
    System.out.println(sortedMap);

使用流和
LinkedHashMap
来维持秩序

    Map<String, String> unsortMap = new TreeMap<String, String>();
    unsortMap.put("room~1", "e");
    unsortMap.put("room~2", "y");
    unsortMap.put("room~10", "n");
    unsortMap.put("room~4", "j");
    unsortMap.put("room~5", "m");
    unsortMap.put("room~3", "f");
    Comparator<String> c = (s1, s2) -> Integer.parseInt(s1.split("~")[1])   - Integer.parseInt(s2.split("~")[1]);
    Map<String, String> sortedMap = unsortMap.keySet()
            .stream()
            .sorted(c)
            .collect(Collectors.toMap(k -> k, k -> unsortMap.get(k), (k, v) -> v, LinkedHashMap::new));
    System.out.println(sortedMap);

一个更通用的比较器,用于包含数字和字母的字符串类型的键:

Map<String, Double> result = new TreeMap<String, Double>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            String numericPart1 = o1.replaceAll("\\D+","");
            String numericPart2 = o2.replaceAll("\\D+","");
            String alphaPart1 = o1.replace(numericPart1, "");
            String alphaPart2 = o2.replace(numericPart2, "");

            if(alphaPart1.equals(alphaPart2)) {
                return Integer.compare(Integer.parseInt(numericPart1), Integer.parseInt(numericPart2));
            } else {
                return alphaPart1.compareTo(alphaPart2);
            }
        }
    });
Map结果=新树映射(新比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
字符串numericPart1=o1.replaceAll(“\\D+”,“”);
字符串numericPart2=o2.replaceAll(“\\D+”,“”);
字符串alphaPart1=o1。替换(数字Part1,“”);
字符串alphaPart2=o2。替换(数字Part2,“”);
如果(字母部分1等于(字母部分2)){
返回Integer.compare(Integer.parseInt(numericPart1)、Integer.parseInt(numericPart2));
}否则{
返回alphaPart1.compareTo(alphaPart2);
}
}
});

一个更通用的比较器,用于包含数字和字母的字符串类型的键:

Map<String, Double> result = new TreeMap<String, Double>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            String numericPart1 = o1.replaceAll("\\D+","");
            String numericPart2 = o2.replaceAll("\\D+","");
            String alphaPart1 = o1.replace(numericPart1, "");
            String alphaPart2 = o2.replace(numericPart2, "");

            if(alphaPart1.equals(alphaPart2)) {
                return Integer.compare(Integer.parseInt(numericPart1), Integer.parseInt(numericPart2));
            } else {
                return alphaPart1.compareTo(alphaPart2);
            }
        }
    });
Map结果=新树映射(新比较器()