I have Java code containing a TreeSet
. I want to convert the code to C#.
Which equivalent collection can I use?
If there is none please suggest alternatives.
You can use std::set here.
TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided.
The TreeSet uses a self-balancing binary search tree, more specifically a Red-Black tree. Simply put, being a self-balancing binary search tree, each node of the binary tree comprises of an extra bit, which is used to identify the color of the node which is either red or black.
When we implement a TreeSet, it creates a TreeMap to store the elements. It sorts the elements either naturally or using the user define comparator. When the object of a TreeSet is created, it automatically invokes the default constructor and creates an object of TreeMap and assigns comparator as null.
Java TreeSet class. Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements NavigableSet interface. The objects of TreeSet class are stored in ascending order. The important points about Java TreeSet class are: Contains unique elements only like HashSet.
Java TreeSet class contains unique elements only like HashSet. Java TreeSet class access and retrieval times are quiet fast. Java TreeSet class doesn't allow null element.
TreeSet implements the SortedSet interface so duplicate values are not allowed. Objects in a TreeSet are stored in a sorted and ascending order. TreeSet does not preserve the insertion order of elements but elements are sorted by keys. TreeSet does not allow to insert Heterogeneous objects.
Java TreeSet class doesn't allow null element. Java TreeSet class is non synchronized. Java TreeSet class maintains ascending order. As shown in the above diagram, Java TreeSet class implements the NavigableSet interface.
That would be System.Collections.Generic.SortedSet<T>
.
It does have the methods and complexity guarantees that one would expect from a balanced tree-backed data structure. You can find maximum, minimum, iterate through all elements in order, everything.
See Add to SortedSet<T> and its complexity for more on that.
I think there is no treeset in C#.
There was similar question asked in msdn, check that may be useful. http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/823c46ca-4a60-4429-a606-e76c3195d4cc/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With