Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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按降序排列_Java_Sorting_Printing_Hashmap_Output - Fatal编程技术网

输出映射。输入<;句子,整数>;用java按降序排列

输出映射。输入<;句子,整数>;用java按降序排列,java,sorting,printing,hashmap,output,Java,Sorting,Printing,Hashmap,Output,显然,这是一个非常幼稚的尝试,但我在编程方面相当缺乏经验。我想做的是按降序打印这些值,即最大值先打印,然后最小值最后打印,如何实现这一点 for (Map.Entry<Sentence, Integer> entry : ontology.ruleCount.entrySet()) { if(entry.getValue() >= 10) System.out.println(entry.getKey()+" : "+entry.get

显然,这是一个非常幼稚的尝试,但我在编程方面相当缺乏经验。我想做的是按降序打印这些值,即最大值先打印,然后最小值最后打印,如何实现这一点

for (Map.Entry<Sentence, Integer> entry : ontology.ruleCount.entrySet()) 
    {
        if(entry.getValue() >= 10)
        System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 9)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 8)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 7)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 6)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 5)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 4)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 3)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 2)
            System.out.println(entry.getKey()+" : "+entry.getValue());
    } 
for(Map.Entry:ontology.ruleCount.entrySet())
{
if(entry.getValue()>=10)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=9)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=8)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=7)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=6)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=5)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=4)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=3)
System.out.println(entry.getKey()+“:”+entry.getValue());
if(entry.getValue()>=2)
System.out.println(entry.getKey()+“:”+entry.getValue());
} 
基本过程(Java 8之前)是:

  • ontology.ruleCount.entrySet()的元素写入
    列表中,例如
    ArrayList
  • 实施一个适当的方法,查看条目的值
  • 用于使用
    比较器对
    列表进行排序
  • 使用排序的列表,但你喜欢
    您需要对地图条目集合进行排序。在Java 8中,这需要非常简单的代码:

    ontology.ruleCount.entrySet().stream()
            .sorted(reverseOrder(Map.Entry.comparingByValue()))
            .forEach(e -> System.out.println(e.getKey() + " : " + e.getValue()));
    

    (这意味着导入静态java.util.Collections.reverseOrder;

    条目。comparingByValue()。reversed
    在根据值降序对映射进行排序时不起作用。这里有一个简单的解决方案

    UnSortedMap.entrySet().stream()
    .filter(e->e.getValue()>1)
    .sorted(Entry.comparingByValue(Comparator.reverseOrder()))
    .collect(Collectors.toMap)(条目::getKey,条目::getValue,
    
    (e1,e2)->e1,LinkedHashMap::new)如何进行集合。按降序排序?实现比较器以给出所需的顺序,例如
    返回-整数。比较(第一个值,第二个值)
    返回整数。比较(第二个值,第一个值)如何确保它每行只打印一个条目?对每个条目调用
    System.println
    。对地图进行排序如何?使用treemap根据贴图值的排序顺序对这些值进行排序存储。比较器实现sld帮助。哈哈,很酷。黑客。太棒了@zerocool根据一个标准对映射条目进行排序是一个非常糟糕的主意,该标准依赖于除键以外的任何其他内容。这将违反
    TreeMap
    所基于的基本假设。@BoristSpider在那里,此版本有效。奇怪的是,使用
    comparingByValue().reversed()
    失败,但只是内联
    reversed
    的代码,即
    集合。reverseOrder(此)
    ,有效。我必须将其添加到我的技巧列表中。这是正确的答案,+1。太棒了!如果值大于1,是否有一种很好的方法可以只打印出来?这很简单,只需在排序的
    之前插入一个筛选阶段:
    .filter(e->e.getValue()>1)
    @BoristheSpider我马上就摆脱了这个推断问题:)