Here is my collection. Here I try to make my own implemetation with special Comparator that sorts Integer elements by its absolute values. (and other methods) Main class. Everything seems to work correct except toString() method. When I overwrite this method without lambdas it works. But! This method is in tests and I mustn’t change it. I just copied it
Tag: treeset
Collection cannot be converted to Object[]
Need help with a problem for a class! So I have created an array of names that gets passed to a method named runA in the class Proj04Runner. I need to create a TreeSet of these names to put them in naturally ascending order with no repeating values. However when I run my code, I get an error telling me
How to extract values from a list of class objects, remove the duplicates and sort alphabetically?
I have a class Tag in java and I am extracting descriptions from a list of Tag objects rawTags to a set (I need to remove duplicate values): but I also want to have the resulting set (or list of unique descriptions) alphabetically ordered. Is there a way how to use TreeSet directly with Collectors or what would be the
Counting Inversions using TreeSet Java
I’m using TreeSet for solving counting inversions problem. I’m using following approach which uses gnu_pbds which works in O(logn) time. Algorithm Insert the first element of the array in the Ordered_Set. For all the remaining element in arr[] do the following: Insert the current element in the Ordered_Set. Find the number of element strictly less than current element + 1
Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet
I had a interview today and the person taking my interview puzzled me with his statement asking if it possible that TreeSet equals HashSet but not HashSet equals TreeSet. I said “no” but according to him the answer is “yes”. How is it even possible? Answer Your interviewer is right, they do not hold equivalence relation for some specific cases.
StringBuffer not giving ClassCastException when trying to add in a treeset without Comparator
I was expecting the ClassCastException when trying to run the following piece of code: I have commented the Comparator code and was trying to use without the Comparator. But I am still getting the correct output. How is this possible because StringBuffer doesn’t implement Comparable interface. I am using Java 11. Was there any changes made in the later version
TreeSet constructor with Comparator parameter
In Java’s documentation for its class TreeSet one of the constructors is shown to have the following header: 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. Answer The elements in a TreeSet are kept sorted. If you use a constructor that
How to find the index of an element in a TreeSet?
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