我需要在地图地图中搜索并返回此元素所属的键.我认为这个实现很慢,你能帮我优化一下吗?我需要使用TreeSet,我不能使用contains因为他们使用compareTo,而equals/compareTo对是以不兼容的方式实现的,我无法改变它.(抱歉,我的英文不好)
Map<Key, Map<SubKey, Set<Element>>> m = new TreeSet();
public String getKeys(Element element) {
for(Entry<Key, Map<SubKey, Set<Element>>> e : m.entrySet()) {
mapSubKey = e.getValue();
for(Entry<SubKey, Set<Element>> e2 : mapSubKey.entrySet()) {
setElements = e2.getValue();
for(Element elem : setElements)
if(elem.equals(element)) return "Key: " + e.getKey() + " SubKey: " + e2.getKey();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在制作一个结合西班牙语动词的GUI.它利用TreeMap作为主要数据结构,其中充满了类Verb的实例化.每个实例化都包含一个字符串,其中包含动词的不定式形式,如"hablar"或"escribir".GUI中有一个功能,允许用户通过键入其不定式及其英文翻译来输入新的动词.
我想知道如何创建一个侦听器,每次从TreeMap添加或删除新动词时都会运行一些代码.我该怎么做呢?
我有一个日期/字符串 TreeMap,我想遍历 N 最后一个条目。
TreeMap<Date,String> map = new TreeMap<Date,String>();
map.put(new Date(2011,1,1), "32,1");
map.put(new Date(2011,3,1), "35");
map.put(new Date(2011,4,5), "38,9");
map.put(new Date(2011,8,2), "57!!");
Run Code Online (Sandbox Code Playgroud)
那我迷路了。我发现了这个:
NavigableSet<Date> dates = donnees.descendingKeySet();
Run Code Online (Sandbox Code Playgroud)
然后我不知道该怎么说:
for(key in dates and i ; i from 0 to N)
{ do something }
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?
我有一个嵌套地图的案例,如下所示:
private final static Map<String, TreeMap<Long,String>> outerConcurrentMap = new ConcurrentHashMap<>();
Run Code Online (Sandbox Code Playgroud)
我知道这ConcurrentHashMap是线程安全的,但我想知道TreeMap这个 CHM 持有的 s ,它们在 CHM 内部也是线程安全的吗?
我正在做的操作是:
get(K)使用.从 CHM 检索 TreeMaptailMap(K,boolean)。clear()CHM。在这种情况下我想要一个线程安全的结构。上述实现是否线程安全?如果没有,请提出解决方案。
lowerKey()Java 实现中操作的时间复杂度是TreeMap多少?
我认为它是 log(n) 但我在文档中的任何地方都找不到它。
更基本操作的复杂性有据可查:
此实现为 containsKey、get、put 和 remove 操作提供有保证的 log(n) 时间成本。
顺便说一句:我也对subMap(). 我猜 log(n) 复杂度lowerKey()将允许 log(n) 时间为常数 size subMap()。
这是我的例子,我如何在 Kotlin 中做到这一点?
var hashMapForTry = HashMap<String,Int>()
hashMapForTry.put("Hi",5)
hashMapForTry.put("What",7)
hashMapForTry.put("How",2)
hashMapForTry.put("Go",1)
hashMapForTry.put("Ford",9)
Run Code Online (Sandbox Code Playgroud) 我希望在地图上获得这种渐变颜色:
ramp <- colorRamp(c("royalblue4", "white"))
ramp.list <- rgb( ramp(seq(0, 1, length = 15)), max = 255)
Run Code Online (Sandbox Code Playgroud)
而且,更重要的是,我希望向图表添加情节特征(特别是悬停文本输出)。这是我的数据:
structure(list(V1 = structure(c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L,
5L, 10L, 13L, 11L, 12L), .Label = c("Apple", "Avocado", "Banana",
"Carrot", "Mango", "Mushroom", "Onion", "Orange", "Pineapple",
"Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"), class = "factor"),
V2 = structure(c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L,
7L, 8L, 1L), .Label = c("23", "24", "36", "42", "43", "46",
"48", "52", "56", "61", "82", "94"), class …Run Code Online (Sandbox Code Playgroud) Map<String, Integer> nonOrderedData = // {b=1, c=2, d=3, e=4, a=0}
List<String> orderSequence = // a, b, c, d, e
Run Code Online (Sandbox Code Playgroud)
我需要应用顺序序列来获取正确排序的数据,我如何使用(首选)流实现这一点?
我使用的是非流方式:
Map<String, Integer> orderedData = new HashMap<>();
for (Map.Entry<String, Integer> nod : nonOrderedData.entrySet()) {
for (String os : orderSequence) {
if (os == nod.getKey()) {
// add nonOrderedData data
} else {
// add data by sequence
}
}
}
Run Code Online (Sandbox Code Playgroud)
想要更清洁的方式来实现我想要的。
我注意到在我的方法中我可以只返回new TreeMap<>(nonOrderedData)并且它会工作得很好,但我不想坚持只应用 asc 顺序 - 我想读取实际序列值然后更改nonOrderedData.
我创建了一个TreeMapwith 产品。
我想计算它们重复的次数,但不知道要编码什么。有什么提示吗?(我期望没有代码,只是建议)
private static Map<Integer, String> shoppingCart() {
Map<Integer, String> result = new TreeMap<>();
result.put(1, "sausage");
result.put(2, "sausage");
result.put(3, "soup");
result.put(4, "egg");
result.put(5, "egg");
result.put(6, "tomato");
result.put(7, "sausage");
return result;
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑添加一个计数变量,但仍然不能解决重复问题。
我在一个TreeMap中存储了一个arrayList作为我的键,但是我得到了这个异常
java.lang.ClassCastException:java.util.ArrayList无法强制转换为java.lang.Comparable
我将数组的内容复制到ArrayList并尝试将该arrayList存储为Map My Code中的我的键:
TreeMap< ArrayList<Integer> , Integer > bandsMap = new TreeMap< ArrayList<Integer> , Integer >();
ArrayList< Integer > erfcn = new ArrayList< Integer >();
for (int index = 0; index < frequencies.length; index++)
erfcn.add(frequencies[index]);
bandsMap.put( erfcn , band_number);
for (Integer value : bandsMap.values()) {
System.out.println("Value = " + value + "\n");
}
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?谢谢