我有以下TreeMap:
TreeMap<String, Integer> distances = new TreeMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
它包含两个字符串,"Face"和"Foo",具有适当的值,这样:
System.out.println(distances);
Run Code Online (Sandbox Code Playgroud)
产量:
{Face=12, Foo=2}
Run Code Online (Sandbox Code Playgroud)
然而,distances.get(Face)返回null,即使distances.get(Foo)正确返回2.以前,distances.get(Face)工作,但由于某种原因,它停止工作.注意我打印出图右呼吁这两个键的get()之前,所以我不小心变脸的值设置为null.有没有人遇到过这个问题?有什么我能做的吗?我只是想弄清楚如何调试这个问题.
注意:在实际代码中,我实际上并没有使用字符串,而是使用不同的对象,所以它是:TreeMap<Object, Integer>.所以它不仅仅是变量名与文字字符串的混淆.
第二个音符:我也觉得非常有信心我的实现hashcode(),并equals()为我使用的对象.(另外,如果我的实现不正确,它不会从一开始就不起作用吗?而不是停止随机工作?)
我有一个包含这样的单词的树图...
TreeMap <String,Integer> occurrence = new TreeMap <String,Integer>();
Run Code Online (Sandbox Code Playgroud)
String = Word
整数=发生的数量.
如何获得最大值 - 整数然后将String映射到最高值?
我有一个TreeMap,其中RobotKey是一个由字符串字段域和长字段时间戳组成的类.RobotKey实现如下可比:
@Override
public boolean equals(Object obj) {
if (this.domain.equals(((RobotKey) obj).getDomain()))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
树图根据以下compareTo函数排序:
@Override
public int compareTo(RobotKey arg0) {
if (this.lastAccessed < arg0.lastAccessed)
return -1;
else if (this.domain.equals(arg0.getDomain()))
return 0;
else
return 1;
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,地图是通过域名访问的,并根据时间戳进行排序.
我做了treemap.get(RobotKey e),其中e与treemap中的现有条目具有相同的域名,但具有不同的时间戳.这应该返回正确的RobotValue,因为Map操作是用equals完成的.但它反而返回null表示找不到RobotKey.知道为什么会这样吗?我做错了什么,我该如何解决?谢谢!
这很好用:
TreeMap <String, ArrayList> x_probs_org = new TreeMap<String, ArrayList>();
Run Code Online (Sandbox Code Playgroud)
但是这个:
TreeMap <String, TreeMap<String, Float>> x_probs = new <String, TreeMap<String, Float>>();
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
error: <identifier> expected
Run Code Online (Sandbox Code Playgroud)
为什么?我究竟做错了什么?我尝试了其他几种方法,但我不明白为什么这是错误的.
我想做的是能够在TreeMap中调整项目的键顺序,所以
以下测试用例适用于numEntries = 6,但不适用于大于7的值.我不知道那里出了什么问题,但我怀疑这个树在更新/复制后会变得不合适.有人可以请一下建议 - 是我的错还是Scala 2.9中的TreeMap有某种错误?
如果循环更新
for (i <- 1 to (numEntries - 1)) {
Run Code Online (Sandbox Code Playgroud)
换成了
for (i <- (numEntries - 1) to 1 by -1) {
Run Code Online (Sandbox Code Playgroud)
一切正常.所以看起来这是TreeMap中的错误?
import org.scalatest.FlatSpec
import collection.immutable.TreeMap
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class TreeMapTest extends FlatSpec {
//val numEntries = 6;
val numEntries = 10;
sealed case class Entry(first: Option[Int], last: Int) extends Ordered[Entry] {
def compare(that: TreeMapTest.this.type#Entry) = {
if (first.isEmpty || that.first.isEmpty) {
last compare that.last …Run Code Online (Sandbox Code Playgroud) 首先,这与帖子中提到的上一个问题不同,因为我想要实现的是d3中整个树形图的渐变,就像我们在谷歌树形图中所拥有的那样.
我正在尝试实现http://bost.ocks.org/mike/treemap/并在同样的工作,但在d3我试图在其中有一个颜色渐变.
目前我正在这样做
color = d3.scale.category20c()
Run Code Online (Sandbox Code Playgroud)
和
.style("fill", function(d) { return color(d.name)})
在我的svg元素上.但是我想要一种更像渐变色的渐变色,以便使树图中更大的盒子更绿,更小的盒子更绿.是否有一种方法在d3/css中指定?
这是一个类似的问题[ FindBugs警告:使用keySet迭代器而不是entrySet迭代器的效率低下
但是,我想尝试做一些不同的事情.我目前的代码在这里:
for (Double key2 : sortedPolygons.keySet()) {
if (sortedPolygons.get(key2).getExteriorRing().equals(hole)) {
sortedPolygons.remove(key2);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
在链接中执行类似解决方案的操作不起作用.以下是所述解决方案的实现:
for(Map.Entry<Double, Polygon> entry : sortedPolygons.entrySet()) {
if (entry.getValue().getExteriorRing().equals(hole)) {
.....
Run Code Online (Sandbox Code Playgroud)
这里的问题是我试图删除该条目.没有entry.remove().如何在没有FindBugs错误的情况下替换我的第一个代码块:
低效使用keySet迭代器而不是entrySet迭代器 - >
此方法使用从keySet迭代器检索的键访问Map条目的值.在map的entrySet上使用迭代器更有效,以避免Map.get(键)查找.
需要注意的是TreeMap,底层结构是,并且无法更改.
我试图在TreeMap中存储字符串的频率,以便我可以获得最常用的字符串,比如说,特定用户.现在我想要做的是编写一个方法,从排序后的地图中返回前n个项目(最常用).
public TreeMap<String,Integer> getKeywords(int n){
//Can check if sorted for efficiency!
println keywords.size();
keywords=keywords.sort{a,b->
//sort the map desc by value
b.value <=> a.value;
}
TreeMap<String,Integer> result=new TreeMap<>();
//need to fill result with first n elements from keywords
return result;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法,比如使用.each()关键字或迭代其keySet,但没有保留其原始顺序,我最终得到的结果未排序.帮助和提示表示赞赏!!
所以,我将Object类声明为TreeMap值.
public class Info {
String name;
String pName;// ????
String psw;//?????
public Info(String name,String pName, String psw){
this.name = name;
this.pName=pName;
this.psw=psw;
}
public Info(String name,String psw){
this.psw=psw;
}
public Info(String name){
this.name=name;
}
}
public class menu {
Scanner sc = new Scanner(System.in);
static TreeMap<String, Info> map = new TreeMap<String, Info>();
public menu() {// constructor
map.put("admin", new Info("admin","1234"));// initialize root
}
private void adminChangePsw() {
System.out.print("new password :");
String data = sc.next();
map.replace(admin, <value>);// howww?
}
Run Code Online (Sandbox Code Playgroud)
我想更改管理员密码,我尝试在类Info下添加以下代码,
public …Run Code Online (Sandbox Code Playgroud) TreeMap treemap=new TreeMap<String,double>();
treemap.put("02.00", 7.5);
treemap.put("03.30", 7.9);
treemap.put("04.00", 8.0);
treemap.put("05.30", 6.8);
treemap.put("10.00", 9.01);
treemap.put("11.30", 8.9);
treemap.put("12.00", 9.30);
System.out.println(treemap);
double min=(double) Collections.min(treemap.values());
Run Code Online (Sandbox Code Playgroud)
树图包含 - {02.00 = 7.5,03.30 = 7.9,04.00 = 8.0,05.30 = 6.8,10.00 = 9.01,11.30 = 8.9,12.00 = 9.30} min包含值:6.8
现在我想迭代键和值
treemap.put("05.30", 6.8);
Run Code Online (Sandbox Code Playgroud)
即
treemap.put("10.00", 9.01);
treemap.put("11.30", 8.9);
treemap.put("12.00", 9.30);
Run Code Online (Sandbox Code Playgroud)
并将最后三个映射键和值存储在另一个树形图中..