Tag: 树形图

TreeMap object的get方法返回null值

import java.util.*; public class Sort { static class ValueComparator implements Comparator { Map base; ValueComparator(Map 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 map = new HashMap(); […]

获取与Map中相应最大值相关联的键(TreeMap / HashMap)

我编写了下面的代码,以找出在JAVA中使用TreeMap具有最大值(Integer)的键(String)。 public static void maxprofitItem(int[] costs, int[] prices, int[] sales,String[] items) { TreeMapmap=new TreeMap(); int[] profits=new int[items.length]; int maxvalue; for(int i=0;i0){ map.put(items[i],profits[i]); } } Set setOfKeys = map.keySet(); Iterator iterator = setOfKeys.iterator(); while (iterator.hasNext()) { String key = (String) iterator.next(); Integer value = (Integer)map.get(key); System.out.println(“Key: “+ key+”, Value: “+ value); } if(!map.isEmpty()){ System.out.println(“The maximum value […]

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

我有以下对象Node : private class Node implements Comparable(){ 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()); } … } 我在以下TreeMap使用它: TreeMap<Node, TreeSet> nodes = new TreeMap<Node, TreeSet>(); 现在,树图在名为Graph的类中使用,以存储当前在图中的节点,以及它们的一组边(来自类Edge )。 我的问题是当我尝试执行时: public containsNode(n){ for (Node x : nodes.keySet()) […]

什么时候应该在PriorityQueue上使用TreeMap,反之亦然?

似乎他们都让你检索最小值,这是我对Prim算法所需要的,并强制我删除并重新插入一个键来更新它的值。 使用一个优于另一个是否有任何优势,不仅仅是这个例子,但一般来说?