treeset

Computational Complexity of TreeSet methods in Java

六月ゝ 毕业季﹏ 提交于 2019-12-07 02:30:48
问题 Is the computational complexity of TreeSet methods in Java, same as that of an AVLTree? Specifically, I want to know the computational complexity of the following methods: 1.add 2.remove 3.first 4.last 5. floor 6. higher Java Doc for method description: http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html For an AVL Tree, there are all O(logn)? Whats the complexity of the above TreeSet Methods? 回答1: Operations which work on a single element are all O(ln n) comparisons except first

Keeping mutable objects sorted in TreeSets at all times

喜欢而已 提交于 2019-12-06 19:41:11
问题 It came to my notice that a TreeSet doesn't keep the mutable objects in sorted order if object attribute values are changed later on. For example, public class Wrap { static TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){ @Override public int compare(Student o1, Student o2) { return o1.age - o2.age; } }); public static void main(String []args){ Student s = new Student(10); ts.add(s); ts.add(new Student(50)); ts.add(new Student(30)); ts.add(new Student(15)); System.out

java TreeSet: comparing and equality

邮差的信 提交于 2019-12-05 13:53:54
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 toString() { return id+":"+sort_1; } public static void main(String[] args) { TreeSet<MyObj> lst=new TreeSet

Computational Complexity of TreeSet methods in Java

偶尔善良 提交于 2019-12-05 07:55:55
Is the computational complexity of TreeSet methods in Java, same as that of an AVLTree? Specifically, I want to know the computational complexity of the following methods: 1.add 2.remove 3.first 4.last 5. floor 6. higher Java Doc for method description: http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html For an AVL Tree, there are all O(logn)? Whats the complexity of the above TreeSet Methods? Operations which work on a single element are all O(ln n) comparisons except first and last which are O(1) comparisons or O(ln N) node search time. comparator(), iterator(), clear(), first(),

Keeping mutable objects sorted in TreeSets at all times

百般思念 提交于 2019-12-05 02:15:44
It came to my notice that a TreeSet doesn't keep the mutable objects in sorted order if object attribute values are changed later on. For example, public class Wrap { static TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){ @Override public int compare(Student o1, Student o2) { return o1.age - o2.age; } }); public static void main(String []args){ Student s = new Student(10); ts.add(s); ts.add(new Student(50)); ts.add(new Student(30)); ts.add(new Student(15)); System.out.println(ts); s.age = 24; //Here I change the age of a student in the TreeSet System.out.println(ts); } }

Alphabetical sorting in treeset not working

橙三吉。 提交于 2019-12-05 01:15:51
问题 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. 回答1:

Should I use a `HashSet` or a `TreeSet` for a very large dataset?

China☆狼群 提交于 2019-12-04 11:20:44
问题 I have a requirement to store 2 to 15 million Accounts (which are a String of length 15) in a data structure for lookup purpose and checking uniqueness. Initially I planned to store them in a HashSet , but I doubt the speed of the lookup will be slow because of hash collisions and will eventually be slower than a TreeMap (using Binary search). There is no requirement for Data to be sorted. I am using Java 7. I have 64G system with 48G dedicated for this application. This question is not a

What are the pros and cons of a TreeSet [closed]

一个人想着一个人 提交于 2019-12-04 09:00:16
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Just wondering what the pros and cons of a TreeSet is, if anyone could tell me please? Thanks! 回答1: One of the Collection classes. It lets you access the elements in your collection by key, or sequentially by key. It has considerably more overhead than ArrayList or HashMap. Use

What is complexity of size() for TreeSet portion view in Java

雨燕双飞 提交于 2019-12-04 03:01:56
问题 I'm wondering what is the time complexity of size() for portion view of TreeSet. Let say I'm adding random numbers to set (and I do not care about duplicities): final TreeSet<Integer> tree = new TreeSet<Integer>(); final Random r = new Random(); final int N = 1000; for ( int i = 0; i < N; i++ ) { tree.add( r.nextInt() ); } and now I'm wodering what is complexity for size() calls as: final int M = 100; for ( int i = 0; i < M; i++ ) { final int f = r.nextInt(); final int t = r.nextInt(); System

TreeSet constructor with Comparator<?> parameter

…衆ロ難τιáo~ 提交于 2019-12-03 23:29:50
问题 In Java’s documentation for its class TreeSet one of the constructors is shown to have the following header: TreeSet(Comparator<? super E> c) Can someone help explain why there is a constructor for TreeSet which takes a comparator object as its argument? I have no clue why this is done. 回答1: The elements in a TreeSet are kept sorted. If you use a constructor that has no Comparator, the natural ordering of the element class (defined by the implementation of Comparable ) would be used to sort