怎么TreeMap样?比方说你有以下地图:
TreeMap<String, Integer> treemap = new TreeMap<>();
treemap.put("lol", 1);
treemap.put("Marc", 2);
treemap.put("Jesper", 3);
Iterator ittwo = treemap.entrySet().iterator();
while (ittwo.hasNext()) {
Map.Entry pairs = (Map.Entry)ittwo.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
ittwo.remove();
}
Run Code Online (Sandbox Code Playgroud)
这个输出是:
Jesper = 3
Marc = 2
lol = 1
Run Code Online (Sandbox Code Playgroud)
那么,如果它不按字母顺序排列它是什么呢?
我正在从csv文件创建一个Treemap.作为我使用的结果,csv文件中的数据是分层的d3.nest().
但是,生成的JSON的形式为{key:"United States", values:[...]}.可缩放的树形图需要层次结构{name:"United States", children:[...]}.我已经尝试将名称和子项替换为示例中的键和值,但它不起作用.
如果有人已经考虑在可缩放树形图上使用键和值,请帮助.我是D3的新手,我不知道d.children是否意味着数据的结构或价值.
这是使用d3将世界大陆,地区和国家/地区从CSV转换为层次结构的代码.
$ d3.csv("../Data/WorldPopulation.csv", function (pop) {
var treeData= { "key": "World", "values": d3.nest()
.key(function (d) { return d.Major_Region; })
.key(function (d) { return d.Region; })
.key(function (d) { return d.Country; })
.entries(pop)
};
Run Code Online (Sandbox Code Playgroud)
结果的前几行是:
`[{"key":"AFRICA","values":[{"key":"Eastern Africa","values"
[{"key":"Burundi","values":[.........`
Run Code Online (Sandbox Code Playgroud)
我无法使用zoomable treemap,因为它需要json中的name和children标签,而不是key和values.
我咨询过的大多数地方都说要使用SortedList,但问题是我移植的程序实际上使用了重复键(按顺序区分),这是TreeMap允许的,而不是SortedList.
有什么建议?
在TreeMap中get()和put()的时间复杂度是多少?
实现是否与红黑树相同?
我需要创建一个自定义的d3布局,它有点接近树形图但是采用三角形样式.这是一个截图,以便您可以理解:
金字塔布局
正如你所看到的,它的工作非常整洁,符合我的需要.为了编码,我将代码基于树图布局代码:
d3.layout.pyramid= function () {
var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = 0;
function populate (nodes, currentHeight, currentHeightPadded, currentBase, currentSumedWeight) {
...
}
function populate_layers (layer, nodes,currentHeight,currentLength, currentSumedArea,currentSumedWeight) {
...
}
function pyramid(d) {
var nodes = hierarchy(d), root = nodes[0];
populate(root.children.slice(),0,0,0,0);
return nodes;
}
pyramid.padding = function(x) {
if (!arguments.length) return padding;
padding = x;
return pyramid;
};
pyramid.size = function(x) {
if (!arguments.length) return size;
size = x; …Run Code Online (Sandbox Code Playgroud) 我正在调用返回TreeMap实例的函数,并在调用代码中我想修改TreeMap.但是,我得到了一个ConcurrentModificationException.
这是我的代码:
public Map<String, String> function1() {
Map<String, String> key_values = Collections.synchronizedMap(new TreeMap<String, String>());
// all key_values.put() goes here
return key_values;
}
Run Code Online (Sandbox Code Playgroud)
我的调用代码是:
Map<String, String> key_values =Collections.synchronizedMap(Classname.function1());
//here key_values.put() giving ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud) 我正在阅读文本文件中的200万行,如上一个问题中提到的 Java最快的方式来阅读200万行的文本文件
现在我将这些信息存储到HashMap中,我想通过TreeMap对其进行排序,因为我想使用ceilingkey.以下方法是否正确?
private HashMap<Integer, String> hMap = new HashMap();
private TreeMap<Integer, String> tMap = new TreeMap<Integer, String>(hMap);
Run Code Online (Sandbox Code Playgroud) 我知道这是一个菜鸟问题,但我在其他任何地方都找不到简单的答案。问题是:我需要编写一个返回 a 的方法SortedMap,因此树图应该可以正常工作。我有一个HashMap< String, Skill>,Skill该类具有方法getName和getNumApplicants我需要返回一个SortedMap<String, Long>,以技能名称为键,以申请人数为值。这是我的立场:
private Map<String,Skill> skillMap = new HashMap<>();
public SortedMap<String, Long> skill_nApplicants() {
return skillMap.values().stream().collect(...);
}
Run Code Online (Sandbox Code Playgroud)
这是技能课
public class Skill {
private String name;
private List <Position> reqPosition = new ArrayList<>();
private Long numApplicants;
public void plusOneApplicant() {
this.numApplicants++;
}
public Long getNumApplicants() {
return numApplicants;
}
public Skill(String name) {
super();
this.name = name;
this.numApplicants = 0L;
}
public String getName() {
return …Run Code Online (Sandbox Code Playgroud) 我有一个项目,我正在为我的Java类工作(很明显),我一定错过了关于如何与TreeMaps交互的讲座.我不知道我正在做什么这个部分,我没有从谷歌那里找到很多帮助.
对于程序中的第一种情况,我必须打印TreeMap的所有值.以下是我提供的代码以及我用它完成的工作.案件A的一切都是我的,但它不起作用.任何帮助,将不胜感激.
import java.util.Scanner;
import java.util.Set;
import java.util.Map;
import java.util.TreeMap;
import java.io.File;
import java.io.FileNotFoundException;
public class prog7 {
public static void main(String args[])
throws FileNotFoundException
{
Scanner kb=new Scanner(System.in);
/*here, add code to declare and create a tree map*/
TreeMap treeMap = new TreeMap();
/*here, add code to declare a variable and
let it be the key set of the map
*/
String key;
//temporary variables
String tempWord;
String tempDef;
//the following code reads data from the file glossary.txt
//and …Run Code Online (Sandbox Code Playgroud) treemap ×10
java ×8
d3.js ×2
javascript ×2
c# ×1
equivalent ×1
hashmap ×1
insert ×1
java-stream ×1
layout ×1
search ×1
sorting ×1