treeset Questions
8
Solved
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) {
...
Pastrami asked 7/3, 2014 at 13:44
6
I want to print an ordered list in Map using the following:
Map<Float, String> mylist = new HashMap<>();
mylist.put(10.5, a);
mylist.put(12.3, b);
mylist.put(5.1, c);
SortedSet<Fl...
2
Solved
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 TreeS...
2
Solved
Let's say I am building a TreeSet of an object for which the ordering depends on only one value.
I cannot do
TreeSet<Foo> tree = new TreeSet<>(Comparator.comparingInt(Foo::getX));
beca...
Kaylakayle asked 2/10, 2022 at 11:44
3
Solved
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
...
Midsection asked 17/1, 2013 at 12:49
2
I am looking for time complexity of subset method of treeset. Is it O(n) where n is the number of items in the navigation set ?
Succumb asked 16/3, 2015 at 0:24
11
Solved
What if I want to retrieve and update objects that stored in a TreeSet?
The reason I'm asking, is that I want to be able to maintain some data stracture that will store Students.
I want it to be s...
Gentry asked 3/12, 2013 at 12:50
3
Solved
I need to use a Tree Set data structure(available in java) in C++, and make use of functions like TreeSet.lower(i) and TreeSet.higher(i) - > which returns the element just lower, and just higher th...
Questionnaire asked 28/4, 2018 at 15:41
5
Solved
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 ...
Becca asked 19/6, 2020 at 19:8
14
Solved
I've always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I've ever known has asked me pointedly why I would use a TreeSet. From a CS background, I d...
5
Solved
Here is the piece of code that I have used for Java 5.0
TreeSet<Integer> treeSetObj = new TreeSet<Integer>( Collections.reverseOrder() ) ;
Collections.reverseOrder() is used to obtain ...
Diannediannne asked 7/7, 2009 at 8:10
3
Solved
I have a TreeSet which contains > 100k objects.
I have another method which requires ArrayList as an param.
Is there any way I can accomplish this without iterating whole TreeSet and then adding e...
3
Solved
I am trying to merge multiple sorted lists into one TreeSet.. And then I am thinking to apply Binary Search algorithm on that TreeSet to retrieve the element in O(log n) time complexity..
Below is...
5
Solved
I have created a Student class like this:
public class Student implements Comparable<Student> {
private String firstName;
private String lastName;
public Student(String firstName, Strin...
Chaunce asked 10/7, 2015 at 7:14
10
Solved
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 construc...
Punctuate asked 17/4, 2015 at 12:22
2
From the following code, I understand that, there is no need of overriding equals() and hashCode() method for TreeSet and TreeMap, neither for sorting, nor searching.
public class ComparableTest i...
6
Solved
I recently came across some Java code that simply put some strings into a Java TreeSet, implemented a distance based comparator for it, and then made its merry way into the sunset to compute a give...
Saddlebow asked 26/4, 2010 at 1:26
1
Solved
Using both Java 8 and Java 11, consider the following TreeSet with a String::compareToIgnoreCase comparator:
final Set<String> languages = new TreeSet<>(String::compareToIgnoreCase);
l...
Knesset asked 1/1, 2020 at 13:40
8
Solved
I have the following code where I am trying to put the StringBuffer objects as keys in a TreeSet. The reason I do this is to see if I can put mutable objects as keys. I do not get any compile error...
Characharabanc asked 22/8, 2013 at 19:33
7
import java.util.TreeSet;
class Test
{
public static void main(String[] args)
{
TreeSet t=new TreeSet();
t.add(null);
System.out.println(t);
}
}
output: NullPointerException.
I read in ma...
Iorgo asked 30/6, 2014 at 11:32
7
Solved
With this code I get this output:
TreeSet<String> t=new TreeSet<String>();
t.add("test 15");
t.add("dfd 2");
t.add("ersfd 20");
t.add("asdt 10");
Iterator<String> it=t.it...
8
Maybe I am not using the right data structure. I need to use a set, but also want to efficiently return the k-th smallest element. Can TreeSet in Java do this? There seems no built-in method of Tre...
Nathalie asked 13/1, 2012 at 20:28
2
I'm having some problems with TreeSet: why does this one accept duplicates? I thought TreeSets detected them through the Comparator, and automatically remove them. Please help me, I'm kind of new t...
Panama asked 2/5, 2018 at 23:14
5
What are the advantages of each structure?
In my program I will be performing these steps and I was wondering which data structure above I should be using:
Taking in an unsorted array and
adding...
Lusty asked 23/9, 2010 at 0:9
3
For the code:
Set<Phone> set = new TreeSet<>();
set.add(new Phone("Harry"));
I get error:
Phone cannot be cast to java.base/java.lang.Comparable
Why Phone has to implement Comp...
1 Next >
© 2022 - 2024 — McMap. All rights reserved.