我有以下对象Node:
private class Node implements Comparable<Node>(){
private String guid();
...
public boolean equals(Node o){
return (this == o);
}
public int hashCode(){
return guid.hashCode();
}
public int compareTo(Node o){
return (this.hashCode() - o.hashCode());
}
...
}
Run Code Online (Sandbox Code Playgroud)
我在下面使用它TreeMap:
TreeMap<Node, TreeSet<Edge>> nodes = new TreeMap<Node, TreeSet<Edge>>();
Run Code Online (Sandbox Code Playgroud)
现在,树图被用在一个被调用的类中,Graph用于存储当前在图中的节点,以及它们的一组边(来自类Edge).我的问题是当我尝试执行时:
public containsNode(n){
for (Node x : nodes.keySet()) {
System.out.println("HASH CODE: ");
System.out.print(x.hashCode() == n.hashCode());
System.out.println("EQUALS: ");
System.out.print(x.equals(n));
System.out.println("CONTAINS: ");
System.out.print(nodes.containsKey(n));
System.out.println("N: " + n);
System.out.println("X: " + …Run Code Online (Sandbox Code Playgroud) 我编写了一些工作正常的代码,但我对于声明TreeMap的正确方法感到困惑.
如果SortedMap是Map的子接口,那么如果代码工作正常就可以使用Map吗?如果TreeMap可以正常使用Map,那么SortedMap是否必要?
应该是:
private Map<String, List <Bus>> map = new TreeMap<String, List <Bus>>();
Run Code Online (Sandbox Code Playgroud)
要么
private SortedMap<String, List <Bus>> map = new TreeMap<String, List <Bus>>();
Run Code Online (Sandbox Code Playgroud)
谢谢.对不起,这是如此基础 - 我是Java新手.
我想存储项目的ID及其相应的坐标.为此,我使用的是TreeMap,其中Coordinates是一个包含int x和int y的类.现在,为了将数据插入地图,我可以写:
treeMapObject.put(5,new BasicRow(30,90));
Run Code Online (Sandbox Code Playgroud)
或者我必须写:
treeMapObject.put(new Integer(5),new BasicRow(30,90));
Run Code Online (Sandbox Code Playgroud)
我想只有第二个是正确的,因为Maps处理对象.但现在的问题是,我说有以下代码:
treeMapObject.put(new Integer(5),new BasicRow(30,90));
treeMapObject.put(new Integer(5),new BasicRow(45,85));
Run Code Online (Sandbox Code Playgroud)
在这种情况下会发生什么?
我试图实现一个简单的树形图来计算整数的出现,但它给了我一个NullPointerException,我不知道如何解决它.
Exception in thread "main" java.lang.NullPointerException
at exercises.CountOccurances_20_07.main(CountOccurances_20_07.java:21)
Run Code Online (Sandbox Code Playgroud)
这是代码:
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class CountOccurances_20_07
{
public static void main(String[] args)
{
int[] list = {2, 3, 40, 3, 5, 4, 3, 3, 3, 2, 0};
Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
for(int i: list)
{
int key = list[i];
if(list.length > 1)
{
if(map.get(key) == 0)
{
map.put(key, 1);
}
else
{
int value = map.get(key).intValue(); // line 21
value ++;
map.put(key, value); …Run Code Online (Sandbox Code Playgroud) import java.util.*;
public class Sort {
static class ValueComparator implements Comparator<String> {
Map<String, Integer> base;
ValueComparator(Map<String, Integer> base) {
this.base = base;
}
@Override
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return 1;
} else {
return -1;
}
}
}
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
ValueComparator vc = new ValueComparator(map);
TreeMap<String, Integer> sorted = new TreeMap<String, Integer>(vc);
map.put("A", 1);
map.put("B", 2);
sorted.putAll(map);
for (String key : …Run Code Online (Sandbox Code Playgroud) 除了排序和排序之外,java中的treemap数据结构有什么优势?treemap数据结构如何在内部工作?
我想迭代一个TreeMap直到一个特定的键.
for (int i = 0 ; i < specifickey ; i++)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?
这是我的代码:
import java.util.*;
public class TreeMap {
public static void main(String[] args) {
Map<String,Integer> treemap = new TreeMap<String,Integer>();
Some code to fill the treemap ie treemap.put("Kevin", 36);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此编译器错误:
TreeMap.java:5: error: type TreeMap does not take parameters
Map<String,Integer> treemap = new TreeMap<String,Integer>();
^
Run Code Online (Sandbox Code Playgroud) 根据这篇文章, TreeMap操作的时间复杂度-subMap,headMap,tailMap
subMap()本身为O(1),而O(n)来自迭代子图。
那么,为什么要使用get(key)呢?
我们可以改用subMap(key,true,key,true),
它是O(1),并且迭代此子映射也是O(1)。
比get(key)快,后者是O(log(n))。这里出了点问题...
我试图在java中创建一个有序映射(按键的降序排序).我试过了
Map<Integer,Integer> map = new TreeMap<>((a,b)->(b-a));
Run Code Online (Sandbox Code Playgroud)
如果我为同一个任务编写Comparator类,会不会产生任何性能影响?
treemap ×10
java ×9
hashmap ×2
collections ×1
comparable ×1
comparator ×1
equals ×1
hashcode ×1
heap ×1
java-8 ×1
key-value ×1
lambda ×1