Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于多个条件对HashMap进行排序_Java_Sorting_Data Structures - Fatal编程技术网

Java 基于多个条件对HashMap进行排序

Java 基于多个条件对HashMap进行排序,java,sorting,data-structures,Java,Sorting,Data Structures,我有一个如下结构的HashMap {val1#val2=val3#val4-val5} 其中key=val1#val2和value=val3#val4-val5 HashMap<String, String> h = new HashMap<String, String>(); h.put("aaa#bbb", "111#444-555"); h.put("bbb#aaa", "222#ddd-222"); h.put("111#999

我有一个如下结构的HashMap

    {val1#val2=val3#val4-val5}
其中key=val1#val2和value=val3#val4-val5

HashMap<String, String> h = new HashMap<String, String>(); 
    h.put("aaa#bbb", "111#444-555");
    h.put("bbb#aaa", "222#ddd-222");
    h.put("111#999", "000#213-aaa");
HashMap h=newhashmap();
h、 看跌期权(“aaa#bbb”、“111#444-555”);
h、 看跌期权(“bbb#aaa”、“222#ddd-222”);
h、 卖出(“111#999”,“000#213 aaa”);
我有三个条件,我必须将地图排序为, 1.由瓦尔1。 2.由瓦尔2。 3.由val3.

这应该可以:

public static void main(String[] args) {
    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    map.put("aa#bb", "11111#44-5555555");
    map.put("bb#aa", "22222#ddd-222");
    map.put("11#99", "00000#213-aaa");

    Function<Map.Entry<String, String>, String> byVal1 = 
             entry -> entry.getKey().substring(0, entry.getKey().indexOf('#'));
    Function<Map.Entry<String, String>, String> byVal2 =
             entry -> entry.getKey().substring(entry.getKey().indexOf('#') + 1);
    Function<Map.Entry<String, String>, String> byVal3 =
             entry -> entry.getValue().substring(0, entry.getValue().indexOf('#'));

    // Just change this value to sort by a different value
    Function<Map.Entry<String, String>, String> value = byVal3;

    List<Map.Entry<String, String>> asList = map.entrySet().stream().sorted(Comparator.comparing(value)).collect(Collectors.toList());
    map.clear();
    asList.forEach(entry -> map.put(entry.getKey(), entry.getValue()));

    map.entrySet().stream().forEach(entry -> System.out.println(entry));
}
publicstaticvoidmain(字符串[]args){
LinkedHashMap=新建LinkedHashMap();
地图放置(“aa#bb”、“11111#44-555”);
地图放置(“bb#aa”、“22222#ddd-222”);
地图放置(“11#99”,“00000#213 aaa”);
函数byVal1=
entry->entry.getKey().substring(0,entry.getKey().indexOf('#');
函数byVal2=
entry->entry.getKey().substring(entry.getKey().indexOf('#')+1);
函数byVal3=
entry->entry.getValue().substring(0,entry.getValue().indexOf('#');
//只需将此值更改为按其他值排序
函数值=byVal3;
List asList=map.entrySet().stream().sorted(Comparator.comparing(value)).collect(Collectors.toList());
map.clear();
asList.forEach(entry->map.put(entry.getKey(),entry.getValue());
map.entrySet().stream().forEach(entry->System.out.println(entry));
}

当然,您不必在方法中创建
byValX
函数,也可以使用方法引用。

哈希映射不保证顺序,为了获得排序映射,您需要使用LinkedHashMap

    Map<String, String> h = new HashMap<>();
    h.put("aaa#bbb", "111#444-555");
    h.put("bbb#aaa", "222#ddd-222");
    h.put("111#999", "000#213-aaa");

    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
    h.entrySet().stream()
        .sorted(Comparator.comparing(e -> e.getKey().split("#")[0]))// sort by val1
        .sorted(Comparator.comparing(e -> e.getKey().split("#")[1]))// sort by val2
        .sorted(Comparator.comparing(e -> e.getValue().split("#")[0]))// sort by val3
        .forEach(e -> {
            linkedHashMap.put(e.getKey(), e.getValue());
        });
要对键进行排序,可以使用java流api对映射项进行排序,然后将它们插入LinkedHashMap

    Map<String, String> h = new HashMap<>();
    h.put("aaa#bbb", "111#444-555");
    h.put("bbb#aaa", "222#ddd-222");
    h.put("111#999", "000#213-aaa");

    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
    h.entrySet().stream()
        .sorted(Comparator.comparing(e -> e.getKey().split("#")[0]))// sort by val1
        .sorted(Comparator.comparing(e -> e.getKey().split("#")[1]))// sort by val2
        .sorted(Comparator.comparing(e -> e.getValue().split("#")[0]))// sort by val3
        .forEach(e -> {
            linkedHashMap.put(e.getKey(), e.getValue());
        });
maph=newhashmap();
h、 看跌期权(“aaa#bbb”、“111#444-555”);
h、 看跌期权(“bbb#aaa”、“222#ddd-222”);
h、 卖出(“111#999”,“000#213 aaa”);
LinkedHashMap LinkedHashMap=新LinkedHashMap();
h、 entrySet().stream()
.sorted(Comparator.comparing(e->e.getKey().split(“#”)[0])//按值排序1
.sorted(Comparator.comparing(e->e.getKey().split(“#”)[1])//按值排序2
.sorted(Comparator.comparing(e->e.getValue().split(“#”)0])//按值排序3
.forEach(e->{
put(e.getKey(),e.getValue());
});

不知道这是否是您想要完成的(澄清需要做什么,我可以调整解决方案),但您可以使用:

import java.util.Map;
导入java.util.Objects;
导入java.util.TreeMap;
公共类测试字典{
公共静态void main(字符串[]args){
Map Map=newtreemap(newcomparator(){
@凌驾
公共整数比较(键o1,键o2){
//在这里你想干什么就干什么
返回0;
}
});
map.put(新键(“a”、“b”、“c”)、新值();
map.put(新键(“b”、“c”、“a”),新值();
map.put(新键(“c”、“b”、“a”)、新值();
系统输出打印项次(map);
}
静态类密钥{
字符串val1;
字符串val2;
字符串val3;
公钥(字符串val1、字符串val2、字符串val3){
这是1.val1=val1;
this.val2=val2;
this.val3=val3;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
键=(键)o;
返回val1.equals(key.val1)&&
val2.equals(key.val2)&&
val3.equals(key.val3);
}
@凌驾
公共int hashCode(){
返回Objects.hash(val1,val2,val3);
}
}
静态类值{
整数;
}
}

首先,您可能希望使用
LinkedHashMap
,因为普通哈希映射不遵守顺序。此外,值是否总是由3个字符组成?那么您希望实现什么?根据键对映射进行排序?否值将由#@Schred
分隔HashMap
为无序。你根本无法对它进行排序,更不用说“根据不同的条件”了。使用
TreeMap