我在另一个TreeMap中使用TreeMap作为"键"
即
TreeMap<TreeMap<String, String>, Object>
Run Code Online (Sandbox Code Playgroud)
在我的代码中,'object'是一个个人构造,但是对于这种情况,我使用了一个字符串.
我创建了一对TreeMaps来测试TreeMap.CompareTo()和TreeMap.HashCode()方法.这从以下开始......
public class TreeMapTest
public void testTreeMap()
{
TreeMap<String, String> first = new TreeMap<String, String>();
TreeMap<String, String> second = new TreeMap<String, String>();
first.put("one", "une");
first.put("two", "deux");
first.put("three", "trois");
second.put("une", "one");
second.put("deux", "two");
second.put("trois", "three");
TreeMap<TreeMap<String, String>, String> english = new TreeMap<TreeMap<String, String>, String>();
TreeMap<TreeMap<String, String>, String> french = new TreeMap<TreeMap<String, String>, String>();
english.put(first, "english");
french.put(second, "french");
Run Code Online (Sandbox Code Playgroud)
从这里我现在调用英语项目来查看它是否包含密钥
if (english.containsKey(second))
{
System.out.println("english contains the key");
//throws error of ClassCastException: Java.util.TreeMap cannot …Run Code Online (Sandbox Code Playgroud) 我的Hash映射中有两个数组,我想根据timeStampArray中的时间对averageValueArray中存储的值进行排序.我正在使用,TreeMap但我得到的ClassCastException是说ArrayList无法比较.
这就是我在做的事情:
Map<List<Date>,List<Double>> sortMap = new HashMap<List<Date>,List<Double>>();
sortMap.put(timeStampArray, averageValueArray);
for (Map.Entry entry : sortMap.entrySet()) {
System.out.println("Key = " + entry.getKey());
System.out.println(" Value = " +entry.getValue());
}
System.out.println("Unsort Map......");
printMap(sortMap);
System.out.println("Sorted Map......");
TreeMap<List<Date>,List<Double>> treeMap = new TreeMap<List<Date>,List<Double>>(sortMap);
for (Map.Entry entry : treeMap.entrySet()) {
System.out.println("Key = " + entry.getKey());
System.out.println(" Value = " +entry.getValue());
}
printMap(treeMap);
Run Code Online (Sandbox Code Playgroud)
printMap是:
public static void printMap(Map<List<Date>,List<Double>> map) {
for (Map.Entry entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : …Run Code Online (Sandbox Code Playgroud) 我有一个代码,如果我使用HashMap可以工作,但如果我使用的是TreeMap则不行,任何人都可以告诉为什么会这样?
这是我的代码:
package ka.fil;
import java.util.HashMap;
import java.util.Map;
public class ModelInMemory implements Model {
private Map<String,BeanRecord> map = new HashMap<>();
@Override
public void putRecord(BeanRecord beanRecord) {
map.put(beanRecord.getEmail(), beanRecord);
}
@Override
public BeanRecord getRecord(String email) {
BeanRecord r = map.get(email);
return r;
}
@Override
public Iterable<BeanRecord> allRecord() {
return map.values();
}
public ModelInMemory() {
}
}
Run Code Online (Sandbox Code Playgroud)
我的意思是不工作是当我在主方法中使用它时,我得到这个:
Exception in thread "main" java.lang.NullPointerException at
java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source)
at ka.fil.ModelInMemory.putRecord(ModelInMemory.java:11)
at ka.fil.AppBatch.main(AppBatch.java:10)
Run Code Online (Sandbox Code Playgroud) 我想创建一个TreeMap,这样每次在TreeMap中插入一个条目时 - 该条目就会根据运行时的值进行排序.(需要O(logN)时间.)所以,我用下面的构造函数定义一个TreeMap ::我不明白问题出在哪里......我很困惑.任何人都可以解释我的错误/问题?
代码::
Map<String,Integer> tm =
new TreeMap<String,Integer>(new Comparator<Map.Entry<String,Integer>>(){
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
throw new UnsupportedOperationException("Not supported yet."); // implement logic here
}
});
Run Code Online (Sandbox Code Playgroud) 在"TreeMap"的情况下,如果将我们自己的类对象作为键传递,那么需要实现哪个接口Comparable或Comparator以及为什么?
我使用的是TreeMap,其中key是枚举,value是整数.
TreeMap<Ingredient, Integer> inv;
public Inventory(){
inv = new TreeMap<Ingredient, Integer>();
inv.put(Ingredient.COFFEE, 10);
inv.put(Ingredient.DECAF_COFFEE, 10);
// and so on
}
Run Code Online (Sandbox Code Playgroud)
Ingredient类定义为
public enum Ingredient {
COFFEE {
public double getCost() {
return 0.75;
}
@Override
public String toString() {
return "Coffee";
}
},
DECAF_COFFEE {
public double getCost() {
return 0.75;
}
@Override
public String toString() {
return "Decaf Coffee";
}
},
Run Code Online (Sandbox Code Playgroud)
// 等等
但是,当我在添加所有成分后迭代树形图时,枚举键不会按字典顺序打印.
如果不是枚举它是一个我将实现比较的类,我可以为枚举做类似的事情吗?
我有以下Java代码.我希望看到按键排序打印的键(因为我使用的是TreeMap),但它没有对键进行排序.我错过了什么?
码:
public class TreeMapTest {
static TreeMap<String,String> li=new TreeMap<String,String>();
static void readAndPrint(){
for (Map.Entry<String, String> entry:li.entrySet() ){
System.out.println(entry);
}
}
public static void main(String[] args) {
for (int i=0;i<10;i++){
String key = String.valueOf(new Random().nextInt(100));
String item = UUID.randomUUID().toString().substring(30);
li.put(key,item);
System.out.println(MessageFormat.format("inserting ({0},{1})",key,item));
}
readAndPrint();
}
}
Run Code Online (Sandbox Code Playgroud)
样本输出:
inserting (7,f4b66a)
inserting (2,5f417d)
inserting (51,90bb9f)
inserting (99,4bfb73)
inserting (41,a4e9d5)
inserting (14,9286d6)
inserting (44,ec4fbd)
inserting (58,e7dd3a)
inserting (69,c54e66)
inserting (0,d1fbfe)
0=d1fbfe
14=9286d6
2=5f417d
41=a4e9d5
44=ec4fbd
51=90bb9f
58=e7dd3a
69=c54e66
7=f4b66a
99=4bfb73
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,我没有对元素进行排序(我有时会对输出进行排序,有时候它没有像上面那样排序!).我错过了什么或误解了什么?
当我创建一个我只是观察TreeMap基础上Date的一个键,按日期排序,如功能remove(Date key)或containsKey(Date key)不即使日期是在地图以及目前的工作.此外,Date的equals函数运行良好.
那么,有谁知道为什么它不起作用?
我正在使用旧的Java 6u43,我创建了我的Map:
Map<Date, Integer> hourMap = new TreeMap<Date, Integer>(new Comparator<Date>() {
@Override
public int compare(Date d1, Date d2) {
return d1.after(d2) ? 1 : -1;
}
});
Date now = DateUtils.parseDate("04:00:00", "HH:mm:ss");
hourMap.put(now, 12);
hourMap.remove(now); // doesn't work
boolean test = hourMap.containsKey(now); // return false
Run Code Online (Sandbox Code Playgroud) 我有一个树形图:
private Map<String, Integer> dataMap = new TreeMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
后来在代码中我有以下部分:
for (String item : data) {
JSONObject itemJson = new JSONObject(item);
System.out.println("-- before " + itemJson.getString("dataLabel") + " " + itemJson.getInt("dataValue"));
dataMap.put(itemJson.getString("dataLabel"), itemJson.getInt("dataValue"));
}
for(String data : dataMap.keySet()) {
System.out.println("-- after " + data);
}
Run Code Online (Sandbox Code Playgroud)
第一个循环显示已排序的元素,例如:
-- before January 1
-- before February 2
-- before March 3
-- before April 4
.
.
.
Run Code Online (Sandbox Code Playgroud)
但第二个循环混合顺序:
-- after April
-- after February
-- after January
-- after March
.
. …Run Code Online (Sandbox Code Playgroud) 我在本课题中遇到的错误已经解决,并在下面的答案部分写下.
问题是TreeMap的下面定义抛出了编译错误,我想知道原因.
Comparator<Student> comparator=new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.roll<=o2.roll)
return -1;
else return 1;
}
};
TreeMap<Integer, Student> map=new TreeMap<>(comparator);
Run Code Online (Sandbox Code Playgroud)
我不明白Treemap的实现错误的原因.有谁能解释一下这里发生了什么?
java ×10
treemap ×10
hashmap ×4
collections ×2
comparator ×2
dictionary ×2
compareto ×1
date ×1
enums ×1
hashcode ×1
mapping ×1
sorting ×1