Java 在没有比较器、arraylist或treeset的情况下对映射进行排序

Java 在没有比较器、arraylist或treeset的情况下对映射进行排序,java,hashmap,Java,Hashmap,在存储网页上找到的单词并将其与出现次数进行映射后,您将如何按频率(从高到低)对它们进行排序 我唯一可以访问的导入是数组、HashMap、HashSet、Map和Set。我曾研究过如何做到这一点,但似乎大多数人都建议使用比较器或迭代器,我不想实现 映射设置如下:map found=new HashMap() 以下是我目前掌握的情况: import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import

在存储网页上找到的单词并将其与出现次数进行映射后,您将如何按频率(从高到低)对它们进行排序

我唯一可以访问的导入是数组、HashMap、HashSet、Map和Set。我曾研究过如何做到这一点,但似乎大多数人都建议使用比较器或迭代器,我不想实现

映射设置如下:map found=new HashMap()

以下是我目前掌握的情况:

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import util.WebDoc;

public class Sorting{

public static void main(String[] args) throws IOException {
String url;

url = “INSERT URL HERE”;

final int numPairs = 30; // maximum number of pairs to print

// get body of the web document
String content = WebDoc.getBodyContent(url); 
String word_pattern = "[A-Za-z]{5,}";
Map<String, Integer> found = new HashMap<>(); // (word,frequency)

Matcher match = Pattern.compile(word_pattern).matcher(content);
int unique = 0;
while (match.find()) {
  String word = match.group().toLowerCase();

  System.out.println(word);  

        if (found.containsKey(word)){
            if (found.get(word)==1)
               unique--;
            found.put(word, found.get(word) +1);
        }
        else{ 
            found.put(word, 1);
            unique++;
        }
    }
}
导入java.util.array;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.Map;
导入java.util.Set;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入util.WebDoc;
公共类排序{
公共静态void main(字符串[]args)引发IOException{
字符串url;
url=“在此处插入url”;
final int numPairs=30;//要打印的最大对数
//获取web文档的正文
String content=WebDoc.getBodyContent(url);
字符串单词_pattern=“[A-Za-z]{5,}”;
找到的映射=新HashMap();/(单词,频率)
Matcher match=Pattern.compile(word\u Pattern).Matcher(content);
int unique=0;
while(match.find()){
字符串字=match.group().toLowerCase();
System.out.println(word);
如果(找到。containsKey(word)){
if(find.get(word)==1)
独特--;
found.put(单词,found.get(单词)+1);
}
否则{
发现。放置(单词,1);
唯一++;
}
}
}

如果您改变了使用基本JDK实用程序的想法,这里有一种使用流的方法:

List<String> sorted = found.entrySet()
        .stream()
        .sort(Comparator.comparing(Map.Entry::getValue).reversed())
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
List sorted=found.entrySet()
.stream()
.sort(Comparator.comparing(Map.Entry::getValue).reversed())
.map(map.Entry::getKey)
.collect(Collectors.toList());

如果您改变了使用基本JDK实用程序的想法,这里有一种使用流的方法:

List<String> sorted = found.entrySet()
        .stream()
        .sort(Comparator.comparing(Map.Entry::getValue).reversed())
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
List sorted=found.entrySet()
.stream()
.sort(Comparator.comparing(Map.Entry::getValue).reversed())
.map(map.Entry::getKey)
.collect(Collectors.toList());

您不能这样做。哈希映射不会排序或排序,因为其元素的位置基于对象哈希

在这项任务中有一些很好的替代方案


请同时选中此选项:

您不能。哈希映射没有排序或排序,因为其元素的位置基于对象哈希

在这项任务中有一些很好的替代方案


请同时检查这一条:

你确定你不是吗?“……大多数人建议使用比较器或迭代器……”——这是有原因的——“……我不想实现。”——为什么不呢?如果有人想让你在没有迭代器的情况下编码,他们要么是个巨魔,要么他们不知道迭代器是什么。你确定你不知道吗?“……大多数人建议使用比较器或迭代器……”——这是有原因的——“……我不想实现。”——为什么不呢?如果有人想让你在没有迭代器的情况下编写代码,他们要么是个巨魔,要么不知道迭代器是什么。