treeset

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

末鹿安然 提交于 2019-12-17 18:34:25
问题 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.) 回答1: 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

Creating a TreeSet with a non-Comparable class: why a run-time exception, rather than compile-time error?

妖精的绣舞 提交于 2019-12-17 16:34:22
问题 If I create an arbitrary class that does not implement Comparable, and try to use it as a treeset, it throws an exception at run time when an object is inserted: public class Foo { } public TreeSet<Foo> fooSet = new TreeSet<Foo>(); fooSet.add(new Foo()); // Throws a ClassCastException exception here: Foo is not comparable I'm no Java expert, but something about this seemed dynamically typed (ala Python) in a way I wasn't expecting. Is there no way for TreeSet's implementation to specify that

Java - TreeSet and hashCode()

二次信任 提交于 2019-12-17 12:15:14
问题 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 +

How can I use a custom class in a TreeSet?

大城市里の小女人 提交于 2019-12-14 00:46:16
问题 If I was using a Set similar to this: Set<node> s=new TreeSet<node>(); class node { private int x; private int y; } Would this be acceptable, and since it's a TreeSet, would it also sort it? 回答1: It's not going to be able to sort it without you implementing Comparable<Node> , and it won't really be an appropriate for set operations until you override equals() and hashCode() . (You don't have to override equals and hashCode for TreeSet to work, but it would make sense to do so.) Something like

How do I use a TreeSet in Java that allows Duplicates?

两盒软妹~` 提交于 2019-12-13 10:58:46
问题 I want a data structure with priority queue functions in O(logn) time and also be able to delete a specific element that's not necessarily the head in O(logn) time. I heard the TreeSet in java does it but does not allow Duplicates, How do i get around this? 回答1: Use TreeMap which allows insertion in log n time and deletion in log n time. You can have there TreeMap<Integer,Integer> where key stores the value of element and value stores the frequency of the element. If you need to do only

benefit of having a treeset over a manually sorted set

爷,独闯天下 提交于 2019-12-13 01:22:49
问题 In collection we could sort the set or map as per to our requirement. Treeset or TreeMap also provides the sorted collection. Is there any benefit of using treeset when we require sorted collection 回答1: The posters before be have not mentioned an important criterion: If the elements in your collection change their state often after insertion, i.e. you need to re-sort the same collection several times, maybe a TreeSet or TreeMap are not ideal because elements are only sorted during insertion,

Java Object Ordering for Sorted Collections

那年仲夏 提交于 2019-12-12 11:56:09
问题 When I'm looking at the Java Object Ordering tutorial, the last section 'Comparators' of the article confused me a little bit. By defining a class Employee which itself is comparable by employee's name, the tutorial doesn't show if this class has overridden the equals method. Then it uses a customized Comparator in which the employees are sorted by the seniority to sort a list of employees and which I could understand. Then the tutorial explains why this won't work for a sorted collection

Returning a SortedSet [closed]

情到浓时终转凉″ 提交于 2019-12-12 03:25:33
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . When I run the following code: Student student1 = new Student("Billy", 13); Student student2 = new Student("Bob", 12); Student student3 = new Student("Belle", 11); Student student4 = new Student("Barry", 10); Student student5 = new Student("Brian", 10); Student student6 = new Student("Bane", 13); Collection

Is iterating through a TreeSet slower than iterating through a HashSet in Java?

陌路散爱 提交于 2019-12-11 19:59:37
问题 I'm running some benchmarks. One of my tests depends on order, so I'm using a TreeSet for that. My second test doesn't, so I'm using a HashSet for it. I know that insertion is slower for the TreeSet. But what about iterating through all elements? 回答1: From a similar post (Hashset vs Treeset): HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet. HashSet: class offers constant time

Finding entries in a TreeSet that contain a prefix

≯℡__Kan透↙ 提交于 2019-12-11 11:44:00
问题 I have a TreeSet in Java that contains Strings (specifically words). I need to write a method... public boolean isValidPrefix(String prefix) ...which accepts a prefix as an argument and checks the TreeSet to see if any of its contained words begin with the prefix. For example, given the prefix "CA" and a TreeSet containing {"DOG,"CAT","COW"} , my method would need to identify that there is a word "CAT" which starts with the prefix. P.S. I would iterate through the TreeSet, but time complexity