treeset

Equals and Comparable with Sets

烈酒焚心 提交于 2019-11-29 02:01:14
I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Comparable class which wrapped the Locale they were working with to achieve what they wanted. I then got to thinking ... as you do ... that I was eliminating duplicates by returning 0 from the compareTo method, not by returning true from an equals implementation as one would need to do to correctly indicate a duplicate in a Set (from the definition of a Set ). I have no objection to using this technique but

Treeset to order elements in descending order

心已入冬 提交于 2019-11-28 08:55:32
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer> treeSetObj = new TreeSet<Integer>( Collections.reverseOrder() ) ; Collections.reverseOrder() is used to obtain a comparator in order to reverse the way the elements are stored and iterated. Is there a more optimized way of doing it? Why do you think this approach won't be optimized? The reverse order Comparator is simply going to be flipping the sign of the output from the actual Comparator (or output from compareTo on the Comparable objects being inserted) and I would therefore imagine it is very fast. An alternative

How to find the index of an element in a TreeSet?

百般思念 提交于 2019-11-28 08:07:58
I'm using a TreeSet<Integer> and I'd quite simply like to find the index of a number in the set. Is there a nice way to do this that actually makes use of the O(log(n)) complexity of binary trees? (If not, what should I do, and does anyone know why not? I'm curious why such a class would be included in Java without something like a search function.) As @Yrlec points out set.headSet(element).size will returns 0 though there is no this element in the set. So we'd better check: return set.contains(element)? set.headSet(element).size(): -1; Here is a test case to show the problem: public static

maintaining TreeSet sort as object changes value

a 夏天 提交于 2019-11-28 06:07:53
I've got a object that defines a 'natural sort order' using Comparable<>. These are being stored in TreeSets. Other than removing and re-adding the object, is there another way to update the sort when the members that are used to define the sort order are updated? As others have noted, there is no in-built way. But you can always subclass that TreeSet, with your constructor(s) of choice, and add in the required functionality: public class UpdateableTreeSet<T extends Updateable> extends TreeSet<T> { // definition of updateable interface Updateable{ void update(Object value); } // constructors

Why does my TreeSet not add anything beyond the first element?

我与影子孤独终老i 提交于 2019-11-28 00:14:43
I have several arrays in the form: private static String[] patientNames = { "John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr" }; Then I make a TreeSet like this: TreeSet<Patient> patTreeSet = new TreeSet<Patient>(); Where Patient is a different class that makes "Patient" objects. Then I loop through each element in my arrays to create several patients and add them to my patTreeSet like this: for(int i = 0; i< patientNames.length; i++){ Date dob = date.getDate("MM/dd/yyyy", patientBirthDates[i]); Patient p = new PatientImpl(patientNames[i], patientSSN[i], dob); patTreeSet.add(p)

Why does Java's TreeSet<E> remove(Object) not take an E

女生的网名这么多〃 提交于 2019-11-27 18:14:06
问题 From the Java 6 TreeSet<E> Documentation: boolean remove(Object o): Removes the specified element from this set if it is present. Why does this accept an Object instead of the generic type E? The only objects that can be added are of type E, so it follows that the only removable type should be of type E. 回答1: remove() , like get() is required to work when given an equal element (in terms of .equals() ). In Java, it is possible (and in some cases, required) for objects of different classes to

Using iterator on a TreeSet

夙愿已清 提交于 2019-11-27 18:03:44
问题 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

Equals and Comparable with Sets

和自甴很熟 提交于 2019-11-27 16:23:32
问题 I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Comparable class which wrapped the Locale they were working with to achieve what they wanted. I then got to thinking ... as you do ... that I was eliminating duplicates by returning 0 from the compareTo method, not by returning true from an equals implementation as one would need to do to correctly

Java - TreeSet and hashCode()

倾然丶 夕夏残阳落幕 提交于 2019-11-27 14:39:26
I have a quick question about TreeSet collections and hashCode methods. I have a TreeSet and I'm adding objects to it, before I add an object, I check to see if it exists in the TreeSet using the contains method. I have 2 distinct objects, each of which produce a distinct hashCode using my implementation of the hashCode method, example below: public int hashCode() { int hash = 7; hash = hash * 31 + anAttribute.hashCode(); hash = hash * 31 + anotherAttribute.hashCode(); hash = hash * 31 + yetAnotherAttribute.hashCode(); return hash; } The hashCodes for a particular run are: 76126352 and

maintaining TreeSet sort as object changes value

↘锁芯ラ 提交于 2019-11-27 05:38:22
问题 I've got a object that defines a 'natural sort order' using Comparable<>. These are being stored in TreeSets. Other than removing and re-adding the object, is there another way to update the sort when the members that are used to define the sort order are updated? 回答1: As others have noted, there is no in-built way. But you can always subclass that TreeSet, with your constructor(s) of choice, and add in the required functionality: public class UpdateableTreeSet<T extends Updateable> extends