treeset

Converting a TreeSet to ArrayList?

一笑奈何 提交于 2019-12-29 06:41:10
问题 I have a TreeSet which contains > 100k objects. I have another method which requires ArrayList as an param. Is there any way I can accomplish this without iterating whole TreeSet and then adding each object manually to ArrayList ? 回答1: How about this: new ArrayList<T>(set); 回答2: ArrayList has a convenience method addAll that fits the bill nicely: final Set<Object> set = ... List<Object> list = new ArrayList<Object>(someBigNum); list.addAll(set); 来源: https://stackoverflow.com/questions/9322405

Converting a TreeSet to ArrayList?

佐手、 提交于 2019-12-29 06:41:02
问题 I have a TreeSet which contains > 100k objects. I have another method which requires ArrayList as an param. Is there any way I can accomplish this without iterating whole TreeSet and then adding each object manually to ArrayList ? 回答1: How about this: new ArrayList<T>(set); 回答2: ArrayList has a convenience method addAll that fits the bill nicely: final Set<Object> set = ... List<Object> list = new ArrayList<Object>(someBigNum); list.addAll(set); 来源: https://stackoverflow.com/questions/9322405

Comparator and equals()

只愿长相守 提交于 2019-12-29 06:09:36
问题 Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't matter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put them in TreeSet . So, question: what disadvantages I'll have from code like this: class Foo implements Comparable<Foo>{} new TreeSet<Foo>(new Comparator<Foo>(){ @Override public int compare(Foo o1, Foo o2) { int res = o1.compareTo(o2); if(res == 0 || !o1.equals(o2)){ return o1.hashCode() -

TreeSet Custom Comparator Algo .. String Comparision

对着背影说爱祢 提交于 2019-12-23 16:52:31
问题 From the input string provided: { "200,400,7,1", "100,0,1,1", "200,200,3,1", "0,400,11,1", "407,308,5,1","100,600,9,1" } , I am adding the same in a TreeSet and want it to be sorted with the 3rd element order, so the expected output will be: (100,0,1,1) (200,200,3,1) (407,308,5,1) (200,400,7,1) (100,600,9,1) (0,400,11,1) But my actual output is: (100,0,1,1)(0,400,11,1)(200,200,3,1)(407,308,5,1)(200,400,7,1)(100,600,9,1) But since the string comparison of 11 is less than 9 but in terms of

java TreeSet: comparing and equality

回眸只為那壹抹淺笑 提交于 2019-12-22 08:48:53
问题 I'd like to have list of object sorted with property 'sort_1'. But when I want to remove I'd like it to use property 'id'. The following code represents the problem. package javaapplication1; import java.util.TreeSet; public class MyObj implements Comparable<MyObj> { public long sort_1; public long id; public MyObj(long sort, long id) { this.sort_1=sort; this.id=id; } @Override public int compareTo(MyObj other) { int ret = Long.compare(sort_1, other.sort_1); return ret; } public String

Treeset.contains() problem

江枫思渺然 提交于 2019-12-21 03:55:29
问题 So I've been struggling with a problem for a while now, figured I might as well ask for help here. I'm adding Ticket objects to a TreeSet, Ticket implements Comparable and has overridden equals(), hashCode() and CompareTo() methods. I need to check if an object is already in the TreeSet using contains(). Now after adding 2 elements to the set it all checks out fine, yet after adding a third it gets messed up. running this little piece of code after adding a third element to the TreeSet,

TreeSet not adding all elements?

喜夏-厌秋 提交于 2019-12-20 17:36:07
问题 I have been looking into the speeds of different Java collection types and have come across something weird. I am adding 1,000,000 objects from a static array to a different collection type and returning the time required. This part of the code works fine. Under further investigation I noticed that the TreeSet is not receiving all of the 1,000,000 objects, and is receiving a different amount each time. Below is the method to transfer the objects from an array to the TreeSet : public int

Error when adding HashMap values to TreeSet

£可爱£侵袭症+ 提交于 2019-12-20 04:30:11
问题 Here is some sample code...I always seem to get a ClassCastException...anybody point out what I am doing wrong? package com.query; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; import org.junit.Test; import com.google.common.collect.Sets; public class ClassCastExceptionTest { @Test public void test() { A<SomeType> a1 = new A<SomeType>(1); A<SomeType> a2 = new A<SomeType>(2); A<SomeType>

What is the time complexity of TreeSet iteration?

孤人 提交于 2019-12-19 17:40:09
问题 In my code, Java TreeSet iteration is the dominant time factor. In looking at the system I believe that it is O(n) complexity. Can anyone verify this? I am thinking that by providing links backward from child node to parent node I could improve the performance. 回答1: TreeSet iteration is of course O(n), as can be expect from any sensible tree-walking algorithm. I am thinking that by providing links backward from child node to parent node I could improve the performance. TreeMap (which TreeSet

Migrating Java TreeMap code to Scala?

你离开我真会死。 提交于 2019-12-18 19:00:08
问题 I am migrating my Java code base to pure Scala and I am stuck on this one piece of code. I have an implementation of an IntervalMap i.e. a data structures that let's you efficiently map ranges [from,to] to values where the set , delete and get operations are all O(log n) (slightly different from an IntervalTree or a SegmentTree). This code uses Java's java.util.TreeMaps and while migrating to Scala, I ran into 2 big issues: Scala has no mutable.TreeMap - I decided to go around it by using