从treemap实例调用的entrySet()函数是返回条目的TreeSet还是仅返回一组条目.是否确保了顺序?
而不是将它作为一组条目得到它如何才能获得一个条目列表?
我想根据key是一个变量的键对树映射进行排序,因此排序应该基于变量值,我们如何实现这一点?我希望在构建的排序方法中使用rathar通过代码实现它,任何回复示例都有很大的帮助.
我正在制作TreeMap<String, String>并希望以降序的方式订购它.我创建了以下比较器:
Comparator<String> descender = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
};
Run Code Online (Sandbox Code Playgroud)
我像这样构造TreeMap:
myMap = new TreeMap<String, String>(descender);
但是,我收到以下错误:
The method compare(String, String) of type new Comparator<String>(){} must override a superclass method
Run Code Online (Sandbox Code Playgroud)
我从未完全弄清楚仿制药,我做错了什么?
在Java 1.6中,引入了NavigableMap(和NavigableSet)接口,并更新了TreeMap以实现新接口.除此之外,NavigableMap对于询问诸如"集合中哪个元素最接近X?"之类的问题非常有用(请参阅FrançoisSarradin的这篇优秀博客文章,以获得示例和讨论).
我希望在Scala 2.8的TreeMap实现中找到类似的东西,但唉,它似乎并非如此(至少,它并不明显).是否有另一个类似于Java的NavigableMap的Scala类或特征?如果没有,是否有一些简单的Scala习语可以用来实现类似的东西?
我意识到我可以使用Java的TreeMap,但我想留在Scala集合框架内(如果只是为了简单).
我正在使用Java JungI图形包和Netbeans 7.我从Java得到以下错误:
Exception in thread "main" java.lang.ClassCastException: graphvisualization.MyVertex cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
Run Code Online (Sandbox Code Playgroud)
以下是与错误相关的代码:
SortedMap<MyVertex, Double> vMap = new TreeMap<MyVertex, Double>();
double curRank = 0;
for(MyVertex v: g.getVertices()) //g is a SparseGraph<MyVertex, MyEdge>
{
curRank = vertexRank.getVertexScore(v);
vMap.put(v, curRank); //**Here is my Error**
}
Run Code Online (Sandbox Code Playgroud)
MyVertex类是我为图表制作的一个类.以下是MyVertex的代码
public class MyVertex
{
int vID; //id for this vertex
double centrality; //centrality measure for this vertex
int degree; //the degree of this vertex
public MyVertex(int id)
{
this.vID …Run Code Online (Sandbox Code Playgroud) 我需要用几条数据线注释一个pylab矩形 - 这些数据行的长度不同.搜索matplotlib文档和谷歌搜索,我找不到一种方法来给注释的不同部分提供不同的大小.
以下代码段演示了此问题:
import pylab
from matplotlib.patches import Rectangle
pylab.rcParams['verbose.level'] = 'debug-annoying'
def draw_rectangle(lower, upper, entry):
ax = pylab.subplot(111)
r = Rectangle( lower, upper[0]-lower[0], upper[1] - lower[1],
edgecolor='k')
ax.add_patch(r)
textRank = str(entry['rank'])
textTeamName = entry['teamName']
textSubmissionDate = entry['submissionDate']
text = textRank + "\n" + textTeamName + "\n" + textSubmissionDate
ax.add_artist(r)
rx, ry = r.get_xy()
cx = rx + r.get_width()/2.0
cy = ry + r.get_height()/2.0
ax.annotate(text, (cx, cy), color='w', weight='bold', ha='center', va='center', size=14)
if __name__ == '__main__':
entry …Run Code Online (Sandbox Code Playgroud) 为什么TreeMap类型Map没有定义方法tailMap或headMap.
Map<String, String> map = new TreeMap<>();
map.tailMap(); //cannot resolve method tailMap
Run Code Online (Sandbox Code Playgroud)
使用显式转换它可以工作:
((TreeMap<String, String>) map).tailMap("a");
Run Code Online (Sandbox Code Playgroud)
随着NavigableMap一切都很好:
NavigableMap<String, String> map1 = new TreeMap<>();
map1.tailMap("a");
Run Code Online (Sandbox Code Playgroud)
如果我是对的,因为界面Map缺少相应的方法,尽管面对对象map是类的具体实现TreeMap,当然确实拥有这样的方法.
只是寻找更详细的解释.
谢谢!
HashMap(myHashMap.entrySet().iterator().next()和myHashMap.keySet().iterator().next()和myHashMap.values().iterator().next())的所有3个集合视图迭代器的时间复杂度在javadoc中有详细记录,所有这3个迭代器都是O(n + c)(n是映射数,c是容量,是物理数量)哈希表中的桶).
但是3个相应的TreeMap集合视图的3个迭代器呢?官方的javadoc没有说什么.它们的复杂性是什么?我确实看过SE8源代码,但我不能从那里判断.
public final Comparator<String> ID_IGN_CASE_COMP = new Comparator<String>() {
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
};
private Map< String, Animal > _animals = new TreeMap< String, Animal >(ID_IGN_CASE_COMP);
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何使用方法get(id)忽略给定的比较器.我希望地图按Case Insensitive排序,但是,当我通过给定键获取值时,我希望它区分大小写.
treemap ×10
java ×7
collections ×2
comparator ×2
annotations ×1
c# ×1
casting ×1
comparable ×1
dictionary ×1
font-size ×1
generics ×1
interface ×1
map ×1
matplotlib ×1
rectangles ×1
scala ×1
sortedmap ×1
winforms ×1