标签: treemap

一致的Equals()结果,但不一致的TreeMap.containsKey()结果

我有以下对象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)

java equals hashcode treemap comparable

2
推荐指数
2
解决办法
3862
查看次数

你如何声明TreeMap - SortedMap或Map?

我编写了一些工作正常的代码,但我对于声明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新手.

java treemap

2
推荐指数
1
解决办法
6672
查看次数

将整数插入TreeMap

我想存储项目的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)

在这种情况下会发生什么?

java treemap

2
推荐指数
1
解决办法
1986
查看次数

TreeMap中的空指针异常

我试图实现一个简单的树形图来计算整数的出现,但它给了我一个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)

java nullpointerexception treemap

2
推荐指数
1
解决办法
7733
查看次数

TreeMap <String,Integer> object的get方法返回null值

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 hashmap key-value treemap

2
推荐指数
1
解决办法
5735
查看次数

除了排序和排序之外,java中的treemap数据结构的优点

除了排序和排序之外,java中的treemap数据结构有什么优势?treemap数据结构如何在内部工作?

java treemap

2
推荐指数
1
解决办法
5328
查看次数

如何迭代TreeMap直到特定键?

我想迭代一个TreeMap直到一个特定的键.

      for (int i = 0 ; i < specifickey  ; i++)
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点 ?

java collections treemap

2
推荐指数
1
解决办法
1250
查看次数

"TreeMap不接受参数"编译错误

这是我的代码:

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)

java compiler-errors treemap

2
推荐指数
1
解决办法
260
查看次数

TreeMap &lt;&gt;操作的时间复杂度:get()和subMap()

根据这篇文章, 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 hashmap treemap red-black-tree

2
推荐指数
1
解决办法
2417
查看次数

哪个更快?使用自定义Comparator类或lambda函数

我试图在java中创建一个有序映射(按键的降序排序).我试过了

Map<Integer,Integer> map = new TreeMap<>((a,b)->(b-a));
Run Code Online (Sandbox Code Playgroud)

如果我为同一个任务编写Comparator类,会不会产生任何性能影响?

heap lambda treemap comparator java-8

2
推荐指数
1
解决办法
106
查看次数