treemap

再谈Java数据结构—分析底层实现与应用注意事项

喜欢而已 提交于 2019-11-27 03:07:10
在回顾js数据结构,写《 再谈js对象数据结构底层实现原理-object array map set 》系列的时候,在来整理下java的数据结构。 java把内存分两种:一种是栈内存,另一种是堆内存 基本类型在栈区分配空间,java的基本数据类型共有8种,即int,short,long,byte,float,double,boolean,char(注意,并没有String的基本类型 )。由于大小可知,生存期可知(这些字面值定义在某个程序块里面,程序块退出后,字段值就消失了),出于追求速度的原因,就存在于栈中。 所有的对象都在堆(Heap)中分配空间,关键字new为每个对象申请内存空间(基本类型除外),对象的释放是由垃圾回收机制决定和执行的,这样做确实简化了程序员的工作。但同时,它也加重了JVM的工作。因为,GC为了能够正确释放对象,GC必须监控每一个对象的运行状态,包括对象的申请、引用、被引用、赋值等,GC都需要进行监控。 不要在经常调用的方法中或在循环中创建对象。可以适当的使用hashtable,vector创建一组对象容器,然后从容器中去取那些对象,而不用每次new之后又丢弃。尽量避免在类的构造函数里创建、初始化大量的对象,防止在调用其自身类的构造器时造成不必要的内存资源浪费,尤其是大对象 包装类 基本类型都有对应的包装类:如int对应Integer类

How to iterate over a TreeMap? [duplicate]

最后都变了- 提交于 2019-11-26 23:51:21
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I iterate over each Entry in a Map? I want to iterate over a TreeMap , and for all keys which have a particular value, I want them to be added to a new TreeMap . How can I do this? 回答1: Assuming type TreeMap<String,Integer> : for(Map.Entry<String,Integer> entry : treeMap.entrySet()) { String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key + " => " + value); } (key and Value

Create a SortedMap in Java with a custom Comparator

瘦欲@ 提交于 2019-11-26 21:28:39
问题 I want to create a TreeMap in Java with a custom sort order. The sorted keys which are string need to be sorted according to the second character. The values are also string. Sample map: Za,FOO Ab,Bar 回答1: You can use a custom comparator like this: Comparator<String> secondCharComparator = new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.substring(1, 2).compareTo(s2.substring(1, 2)); } }; Sample: SortedMap<String,String> map = new TreeMap<String,String

Selecting random key and value sets from a Map in Java

淺唱寂寞╮ 提交于 2019-11-26 20:19:51
问题 I want to get random keys and their respective values from a Map. The idea is that a random generator would pick a key and display that value. The tricky part is that both key and value will be strings, for example myMap.put("Geddy", "Lee") . 回答1: HashMap<String, String> x; Random random = new Random(); List<String> keys = new ArrayList<String>(x.keySet()); String randomKey = keys.get( random.nextInt(keys.size()) ); String value = x.get(randomKey); 回答2: This question should be of help to you

d3.nest() key and values conversion to name and children

自作多情 提交于 2019-11-26 20:05:34
问题 I am working on creating a Treemap from a csv file. The data in the csv file is hierarchical, as a result I used d3.nest() . However, the resulting JSON is in the form of {key:"United States", values:[...]} . The zoom-able treemap requires hierarchy as {name:"United States", children:[...]} . I have tried replacing name and children to key and values in the example, but it doesn't work. If anyone has already looked into using key and values on a zoomable treemap, please help. I am new to D3

Java TreeMap Comparator

大城市里の小女人 提交于 2019-11-26 17:43:22
I need a comparator for a TreeMap. Should I write this anonymously in the constructor for my TreeMap? How else could I write my comparator. Currently, Java does not like my code (can I do this anonymously?): SortedMap<String, Double> myMap = new TreeMap<String, Double>(new Comparator<Entry<String, Double>>() { public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { return o1.getValue().compareTo(o2.getValue()); } }); Can I do the above anonymously? How else could I do this? I want to sort myMap by the Value not the Key You can not sort TreeMap on values. A Red-Black tree based

Java语言学习(八):集合类框架

拜拜、爱过 提交于 2019-11-26 16:29:07
Java中提供了各种数据集合类,这些类主要用于保存复杂结构的数据。下面将介绍常用的几种集合类的用法。 ArrayList集合 可以看做一个动态的数组,比普通数组更加灵活,更适合保存未知数量的数据。它突破了普通数组固定长度的限制,可以随时向数组中添加和移除元素,而且不受类型的限制,可以添加任意类型的元素值或对象。如下: //声明集合类型 ArrayList<String> arrList1 = new ArrayList<String>(); arrList1.add("Bob"); arrList1.add("Cily"); //不声明集合类型 ArrayList arrList2 = new ArrayList(); arrList2.add("Angle"); arrList2.add(66); arrList2.add(null); arrList2.add(true); arrList2.add(arrList1); //遍历 for(int i=0;i<arrList2.size();i++){ System.out.println(arrList2.get(i)); } 输出为: Angle 66 null true [Bob, Cily] 通过上面的例子可以看出:ArrayList集合的初始化既可以声明类型,也可以忽略。当声明类型时,只能添加同类型的元素

Using java.util.Map in h:dataTable

≯℡__Kan透↙ 提交于 2019-11-26 11:18:37
问题 I need to display Map using <h:dataTable> . My backing bean has a Map property as below: public class Bean { private Map<Integer,String> map; // +getter @PostConstruct public void init() { map = new TreeMap<Integer,String>(); map.put(1,\"Sasi\"); map.put(2,\"Pushparaju\"); map.put(3,\"Venkat Raman\"); map.put(3,\"Prabhakaran\"); } } Then in JSF page I am trying to bind this Map property to the value attribute of <h:dataTable> . <h:dataTable border=\"1\" value=\"#{bean.map}\" var=\"map\"> <h

Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

徘徊边缘 提交于 2019-11-26 06:36:42
问题 In Java you can build up an ArrayList with items and then call: Collections.sort(list, comparator); Is there anyway to pass in the Comparator at the time of list, creation like you can do with TreeMap ? The goal is to be able add an element to the list and instead of having it automatically appended to the end of the list, the list would keep itself sorted based on the Comparator and insert the new element at the index determined by the Comparator . So basically the list might have to re-sort

Java TreeMap Comparator

拈花ヽ惹草 提交于 2019-11-26 05:33:31
问题 I need a comparator for a TreeMap. Should I write this anonymously in the constructor for my TreeMap? How else could I write my comparator. Currently, Java does not like my code (can I do this anonymously?): SortedMap<String, Double> myMap = new TreeMap<String, Double>(new Comparator<Entry<String, Double>>() { public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { return o1.getValue().compareTo(o2.getValue()); } }); Can I do the above anonymously? How else could I do this? I