treeset

Computational complexity of TreeSet operations in Java?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 19:15:59
I am trying to clear up some things regarding complexity in some of the operations of TreeSet. On the javadoc it says: "This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains)." So far so good. My question is what happens on addAll(), removeAll() etc. Here the javadoc for Set says: "If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets." Is it just explaining the logical outcome of the operation or is it giving a hint about the complexity? I mean, if the

Migrating Java TreeMap code to Scala?

早过忘川 提交于 2019-11-30 17:13:07
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 mutable.TreeSet (oddly Scala has mutable.TreeSet but no mutable.TreeMap ) for storing the keys and

In TreeSet, Sorting & Uniqueness of custom objects based on different properties

流过昼夜 提交于 2019-11-30 13:59:44
问题 Below is my Student class class Student implements Comparable { String name; int rollNo; @Override public int compareTo(Object obj) { return ((Student)obj).name.compareTo(this.name); } } latest modification: but still no getting the right result @Override public int compareTo(Object obj) { Student s = (Student) obj; if (name.equals(s.name)) { // achieving uniqueness return 0; } else { if (rollNo < s.rollNo) { return -1; } else if (rollNo > s.rollNo) { return 1; } else { // this makes `name`

can StringBuffer objects be keys in TreeSet in Java?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 09:47:32
问题 I have the following code where I am trying to put the StringBuffer objects as keys in a TreeSet. The reason I do this is to see if I can put mutable objects as keys. I do not get any compile error. but when I run this code, I get the error that is below the code. specially, I get this java.lang.StringBuffer cannot be cast to java.lang.Comparable . what does this error indicate? from javadoc I see that StringBuffer class is declared final ( public final class StringBuffer ), doesn't that mean

In TreeSet, Sorting & Uniqueness of custom objects based on different properties

烈酒焚心 提交于 2019-11-30 09:03:24
Below is my Student class class Student implements Comparable { String name; int rollNo; @Override public int compareTo(Object obj) { return ((Student)obj).name.compareTo(this.name); } } latest modification: but still no getting the right result @Override public int compareTo(Object obj) { Student s = (Student) obj; if (name.equals(s.name)) { // achieving uniqueness return 0; } else { if (rollNo < s.rollNo) { return -1; } else if (rollNo > s.rollNo) { return 1; } else { // this makes `name` the second ordering option. // names don't equal here return name.compareTo(s.name); } } } If I create

Java's TreeSet equivalent in Python?

可紊 提交于 2019-11-30 08:13:17
I recently came across some Java code that simply put some strings into a Java TreeSet, implemented a distance based comparator for it, and then made its merry way into the sunset to compute a given score to solve the given problem. My questions, Is there an equivalent data structure available for Python? The Java treeset looks basically to be an ordered dictionary that can use a comparator of some sort to achieve this ordering. I see there's a PEP for Py3K for an OrderedDict, but I'm using 2.6.x. There are a bunch of ordered dict implementations out there - anyone in particular that can be

can StringBuffer objects be keys in TreeSet in Java?

送分小仙女□ 提交于 2019-11-29 16:44:01
I have the following code where I am trying to put the StringBuffer objects as keys in a TreeSet. The reason I do this is to see if I can put mutable objects as keys. I do not get any compile error. but when I run this code, I get the error that is below the code. specially, I get this java.lang.StringBuffer cannot be cast to java.lang.Comparable . what does this error indicate? from javadoc I see that StringBuffer class is declared final ( public final class StringBuffer ), doesn't that mean it is immutable and hence hashable? I am a newbie to the hashing and immutable stuff, so kindly help

Java's TreeSet equivalent in Python?

无人久伴 提交于 2019-11-29 11:11:51
问题 I recently came across some Java code that simply put some strings into a Java TreeSet, implemented a distance based comparator for it, and then made its merry way into the sunset to compute a given score to solve the given problem. My questions, Is there an equivalent data structure available for Python? The Java treeset looks basically to be an ordered dictionary that can use a comparator of some sort to achieve this ordering. I see there's a PEP for Py3K for an OrderedDict, but I'm using 2

Comparator and equals()

橙三吉。 提交于 2019-11-29 03:59:21
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() - o2.hashCode(); } return res; } }); Update : Ok. If it should always be a consistency between the

Using iterator on a TreeSet

狂风中的少年 提交于 2019-11-29 03:58:28
SITUATION: I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet. TreeSet<Custom> ts=new TreeSet<Custom>(); Iterator<Custom> itr=ts.iterator(); while(itr.hasNext()){ Custom c=itr.next(); //Code to add a new element to the TreeSet ts } QUESTION: Well I want to know that if I add a new element to the TreeSet within the while loop, then will that new element get sorted immediately. In other words, if I add a new element within the while loop and it is less than the one which I am currently holding in c, then in the next