treeset

How to return the kth element in TreeSet in Java

好久不见. 提交于 2019-12-03 19:17:33
问题 Maybe I am not using the right data structure. I need to use a set, but also want to efficiently return the kth smallest element. Can TreeSet in java do this? There seems no built-in method of TreeSet to do this. Please help me. 回答1: I don't believe that TreeSet has a method that directly does this. There are binary search trees that do support O(log n) random access (they are sometimes called order statistic trees ), and there are Java implementations of this data structure available. These

Alphabetical sorting in treeset not working

扶醉桌前 提交于 2019-12-03 17:00:14
Hi, my code is like this: TreeSet<String> ts=new TreeSet<String>(); ts.add("Testtxt"); ts.add("Testxml"); ts.add("docdoc"); ts.add("ePeoplexml"); ts.add("fantasyxlsx"); ts.add("idaddedgif"); ts.add("idaddedrtf"); System.out.println("Tree set :: "+ts); Output: Tree set :: [Testtxt, Testxml, docdoc, ePeoplexml, fantasyxlsx, idaddedgif, idaddedrtf] It's not sorting all strings in alphabetical order.Can any one help how to achieve an ascending order of the strings in treeset . Thanks Madhu. The sorting is fine. It is done in case-sensitive manner. Since unicode code point of T comes before d , so,

Treeset.contains() problem

淺唱寂寞╮ 提交于 2019-12-03 12:15:08
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, Ticket temp2 is the object I'm checking for(verkoopLijst). Ticket temp2 = new Ticket(boeking, TicketType

TreeSet not adding all elements?

橙三吉。 提交于 2019-12-03 05:01:47
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 treeSet(int num) { Date before = new Date(); for(int i=0; i<num; i++) { treeSet.add(personsArray[i]); } Date

TreeSet is showing wrong output

一笑奈何 提交于 2019-12-03 03:27:54
问题 While working with a tree set, I found very peculiar behavior. As per my understanding this program should print two identical lines: public class TestSet { static void test(String... args) { Set<String> s = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); s.addAll(Arrays.asList("a", "b")); s.removeAll(Arrays.asList(args)); System.out.println(s); } public static void main(String[] args) { test("A"); test("A", "C"); } } but strangely it prints: [b] [a, b] Why is tree set behaving like this?

TreeSet is showing wrong output

為{幸葍}努か 提交于 2019-12-02 16:59:48
While working with a tree set, I found very peculiar behavior. As per my understanding this program should print two identical lines: public class TestSet { static void test(String... args) { Set<String> s = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); s.addAll(Arrays.asList("a", "b")); s.removeAll(Arrays.asList(args)); System.out.println(s); } public static void main(String[] args) { test("A"); test("A", "C"); } } but strangely it prints: [b] [a, b] Why is tree set behaving like this? This happens because a SortedSet’s Comparator is used for sorting, but removeAll relies on the equals

why use \0 to include highEndPoint as part of the sublist

眉间皱痕 提交于 2019-12-02 01:19:56
问题 I saw the code below from java tutorial oracle. In order to count the number of words between doorbell (inclusive) and pickle (inclusive), the author added \0 after the word pickle . I understand that the effect of adding \0 after pickle , is that the word pickle is now included as part of the subset. But my question is, why use \0 ? Could someone please help me out? Thanks in advance for any help! SortedSet<String> dictionary = new TreeSet<>(entire collection of words from a dictionary); int

HashSet and TreeSet performance test

廉价感情. 提交于 2019-12-01 22:17:39
问题 I read about how TreeSet is slower than HashSet, (that adding elements into a TreeSet is slower) so i made a performance test, i'm trying to find out if it's better to add elements to HashSet and then move them into a TreeSet or put them in there in the first place. It looks like that inserting elements into a HashSet is faster but only when i insert a large amount of elements, why? I've read that if i don't need the elements sorted, always use HashSet, but apparently, sometimes it's slower.

why use \\0 to include highEndPoint as part of the sublist

元气小坏坏 提交于 2019-12-01 21:10:34
I saw the code below from java tutorial oracle. In order to count the number of words between doorbell (inclusive) and pickle (inclusive), the author added \0 after the word pickle . I understand that the effect of adding \0 after pickle , is that the word pickle is now included as part of the subset. But my question is, why use \0 ? Could someone please help me out? Thanks in advance for any help! SortedSet<String> dictionary = new TreeSet<>(entire collection of words from a dictionary); int count = dictionary.subSet("doorbell", "pickle\0").size(); System.out.println(count); Edit: Also, what

A TreeSet or TreeMap that allow duplicates

纵饮孤独 提交于 2019-12-01 16:44:49
I need a Collection that sorts the element, but does not removes the duplicates. I have gone for a TreeSet , since TreeSet actually adds the values to a backed TreeMap : public boolean add(E e) { return m.put(e, PRESENT)==null; } And the TreeMap removes the duplicates using the Comparators compare logic I have written a Comparator that returns 1 instead of 0 in case of equal elements. Hence in the case of equal elements the TreeSet with this Comparator will not overwrite the duplicate and will just sort it. I have tested it for simple String objects, but I need a Set of Custom objects. public