考虑这个打印出一些设备类型统计数据的例子.("DeviceType"是一个带有十二个值的枚举.)
Multiset<DeviceType> histogram = getDeviceStats();
for (DeviceType type : histogram.elementSet()) {
System.out.println(type + ": " + histogram.count(type));
}
Run Code Online (Sandbox Code Playgroud)
以频率顺序打印不同元素的最简单,最优雅的方法是什么(最常见的类型是第一种)?
随着快速浏览一下Multiset界面,有一个为这个没有现成的方法,并没有番石榴的的Multiset实现(HashMultiset,TreeMultiset,等)似乎自动保持要素频率有序无论是.
我有这个HashMap,我需要根据其中包含的值(而不是键)以升序打印出来.
但是我打印出来的顺序似乎是随机的.
以升序价值顺序打印出来的最佳方法是什么?
Map<String, String> codes = new HashMap<String, String>();
codes.put("A1", "Aania");
codes.put("X1", "Abatha");
codes.put("C1", "Acathan");
codes.put("S1", "Adreenas");
Run Code Online (Sandbox Code Playgroud)
换句话说,上面的例子应该打印出来:
A1, Aania
X1, Abatha
C1, Acathan
S1, Adreenas
Run Code Online (Sandbox Code Playgroud) 我使用map接口从文件中读取,然后将值存储为键值对.文件格式如下
A 34
B 25
c 50
Run Code Online (Sandbox Code Playgroud)
我将从该文件中读取数据并将其存储为键值对,然后我将向用户显示该数据.我的要求是以这种格式显示结果
C 50
A 34
B 25
Run Code Online (Sandbox Code Playgroud)
因此,我需要按值的降序对地图进行排序.所以我将能够显示这些作为我的结果..我已经阅读了这个并找到下面的代码
static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
new Comparator<Map.Entry<K,V>>() {
@Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
int res = e1.getValue().compareTo(e2.getValue());
return res != 0 ? res : 1; // Special fix to preserve items with equal values
}
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
Run Code Online (Sandbox Code Playgroud)
我希望这会按升序排列值,我只是想知道这种方法是否正确,或者其他一些有效的方法对我有用吗?
我需要在Java中有一个自动按值排序的映射 - 这样当我添加新的键值对或更新现有键值对的值时,它会随时保持排序,甚至删除一些条目.
还请记住,这张地图将会非常庞大(数百万,甚至是数百万条的大小).
所以基本上我正在寻找以下功能:
假设我们有一个实现上述功能的"SortedByValuesMap"类,我们有以下代码:
SortedByValuesMap<String,Long> sorted_map = new SortedByValuesMap<String, Long>();
sorted_map.put("apples", 4);
sorted_map.put("oranges", 2);
sorted_map.put("bananas", 1);
sorted_map.put("lemons", 3);
sorted_map.put("bananas", 6);
for (String key : sorted_map.keySet()) {
System.out.println(key + ":" + sorted_map.get(key));
}
Run Code Online (Sandbox Code Playgroud)
输出应该是:
bananas:6
apples:4
lemons:3
oranges:2
Run Code Online (Sandbox Code Playgroud)
特别是,对我来说真正重要的是能够随时获得具有最低值的条目 - 使用如下命令:
smallestItem = sorted_map.lastEntry();
Run Code Online (Sandbox Code Playgroud)
哪个应该给我'橘子'条目
编辑:我是一个Java新手所以请详细说明你的答案 - 谢谢
EDIT2:这可能会有所帮助:我正在使用它来计算大文本文件中的单词(对于那些熟悉的人:特别是n-gram).所以我需要建立一个地图,其中键是单词,值是这些单词的频率.但是,由于限制(如RAM),我想只保留X最常用的单词 - 但事先你不能知道哪些是最常用的单词.因此,我认为它可能起作用的方式(作为近似)是开始计算单词,当地图达到上限(如1 mil条目)时,将删除最不频繁的条目,以便将地图的大小保持为总是1密耳.
我的问题可能过于宽泛,可能答案很简单,但我不得不问.
Java 7中是否有(Java 8)流*的等效实现?
我熟悉(Java 8)流,但我的项目要求是使用Java 7.
*不要与inputStream和outputStream混淆.
我有这个HashMap:
HashMap<String, Integer> m
Run Code Online (Sandbox Code Playgroud)
它基本上存储任何单词(String)及其频率(整数).以下代码按值排序HashMap:
public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> m1, Map.Entry<String, Integer> m2) {
return (m2.getValue()).compareTo(m1.getValue());
}
});
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
现在情况已经改变,我有这个:
HashMap<String, doc>;
class doc{
integer freq;
HashMap<String, Double>;
}
Run Code Online (Sandbox Code Playgroud)
我如何按照与sortByValue相同的方法按值对这个HashMap进行排序?
根据某些条件(除了使用不同的顺序再次编写它们)之外,操作顺序的最佳方法是什么?
假设有一个Person类,Person的每个对象代表一个不同的人.
class Person{
int eatingPriority = 3;
int sleepingPriority = 2;
int recreationPriority = 1;
void eat() {/*eats*/}
void sleep() {/*sleeps*/}
void watchTv() {/*watches tv*/}
void satisfyNeeds() {
//HOW TO DO THIS
}
}
Run Code Online (Sandbox Code Playgroud)
如何satisfyNeeds()根据优先级使方法调用其他三种方法?
注意:我想说明优先级可以在人与人之间发生变化.
我尝试在eclipse上复制下面的代码.我收到一个错误,告诉我必须实现所有继承的方法(因为Comparator是一个接口).
该类型
new Comparator(){}必须实现继承的抽象方法Comparator.reversed().
有许多这些方法,我想要覆盖的唯一方法是比较.我是否必须实现所有其他方法,或者有没有办法指定我不需要实现它们?我知道由于接口的契约性质,我将不得不这样做,但如果我只需要更改一个方法怎么办?
static Map sortByValue(Map map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
编辑 通过在eclipse luna中将合规性级别更改为java8来解决.谢谢!
我有这样的地图:
Map<Integer, MyEntry> map = new HashMap<Integer, MyEntry>();
Run Code Online (Sandbox Code Playgroud)
MyEntry是这样的:
public class MyEntry {
private String title;
private String value;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
将值放入地图后,我想对其进行排序.第一个元素是最小的,最后一个元素是最大的.
我有下面的值与哈希值映射,在值中我已经日期为字符串数据类型。我想比较地图中所有可用的日期,并仅提取一个具有最近日期的键值。
我想与值而不是键进行比较。
我已包含以下代码
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("1", "1999-01-01");
map.put("2", "2013-10-11");
map.put("3", "2011-02-20");
map.put("4", "2014-09-09");
map.forEach((k, v) -> System.out.println("Key : " + k + " Value : " + v));
}
}
Run Code Online (Sandbox Code Playgroud)
预期的输出是:
关键4值2014-09-09