Java对值进行哈希映射

Java对值进行哈希映射

本文介绍了Java对值进行哈希映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

边走边学Java(Python背景). Java 7代码中的简单单词计数程序(不能使用J8!).

我有一个word:count对的哈希映射.现在,我需要按计数(降序)进行排序,并按字母顺序使用单词来打破联系.

已经阅读了S/O,我尝试了一个树状图,但是它似乎不能很好地处理联系,所以我认为这是不对的.

我已经看到很多解决方案,它们定义了一个新的类sortbyvalue并定义了一个比较器.这些对我不起作用,因为我需要将解决方案全部保留在现有的类中.

我正在寻找有关此想法的反馈意见

  • 遍历哈希图中的地图项(我)
  • 使用me.getKey = K和me.getValue = V
  • 新Map.Entry reverse_me =(V,K){不确定该语法}
  • 将reverse_me添加到列表中
  • 在地图上为我所有人重复
  • List.sort {我不确定这是如何排序的,也不知道如何编写比较器.此时,每个List元素都将是一个(计数,单词)对,并且sort()应该按递减的顺序排序,如果按字母顺序计数相同的话,则按单词排序)

这将是最终输出.

这是合乎逻辑的进展吗?我可以从许多帖子中看出,关于如何执行此操作有很多意见,但这是我可以全神贯注的观点.

此外,不能使用番石榴.

解决方案

您可以在地图上创建EntryList集.使用Collections.sort()List进行排序.当Value相同时,您可以传递自定义Comparator以便按Key进行排序.

Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
  public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
  {
    int result = (o2.getValue()).compareTo( o1.getValue() );
    if (result != 0) {
      return result;
    } else {
      return o1.getKey().compareTo(o2.getKey());
    }
  }
} );

Learning Java as I go (Python background). Simple word count program in Java 7 code (can not use J8!).

I have a hash map of word:count pairs. Now I need to sort on count (decreasing order), and break ties with using word in alphabetical order.

Have read s/o and I tried a treemap but it does not seem to handle ties very well so I don't think that is right.

I have seen a lot of solutions posted that define a new class sortbyvalue and define a comparator. These will not work for me as I need to keep the solution all contained in the existing class.

I am looking for feedback on this idea:

  • iterate over the map entries (me) in the hashmap
  • use me.getKey = K and me.getValue = V
  • new Map.Entry reverse_me = (V,K) {not sure about this syntax}
  • add reverse_me to a List
  • repeat for all me in map
  • List.sort { this is where I am unsure, on how this will sort and no idea how to write a comparator. At this point each List element would be a (count, word) pair and the sort() should sort by count in decreasing and then by word in case of same counts in alphabetical order)

This would be the final output.

Is this a logical progression? I can tell from the many posts that there are many opinions on how to do this, but this is the one I can wrap my head around.

Also, can not use Guava.

解决方案

You can create an List of Entry set from the map. Sort the List using Collections.sort(). You can pass the custom Comparator for sorting by Key when Value(s) are same.

Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
  public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
  {
    int result = (o2.getValue()).compareTo( o1.getValue() );
    if (result != 0) {
      return result;
    } else {
      return o1.getKey().compareTo(o2.getKey());
    }
  }
} );

这篇关于Java对值进行哈希映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:52