#Where can I use a comparator (collections)?
93 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @barren steppe! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
PriorityQueue and TreeSet? im not quite sure that's what you're asking
list.sort() too. stream().sort()
kinda redundant with List.sort(), no?
java lang developers decided to have it in Collections class, so maybe not
the fun thing, both methods have this: ```java
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
...
both use Arrays.sort()
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
TreeSet
@Override
public int compare(String wordA, String wordB) {
if (wordA.length() < wordB.length()){
return 1;
} else if (wordA.length() == wordB.length()){
return 1;
} else {
return -1;
}
}});```
you don't really need all that
you could just use Comparator.comparingByInt(String::length)
I feel like we arent allowed here bacause we havent learned yet what :: means
But, where can I find the docs to learn about the Comparator, I need something similar for another task. We get a List<> of Objects and shall sort them via their returned time values from their time() methods
comparingByInt(str -> str.length()) also works, what you've written is essentially just the long/outdated form of what ive written
Is this ascending or descending?
if not you can also ust use wordA.length() - wordB.length() instead of your entire condition tree there
asc, just like yours. i think. idk i never remember the exact semantics
Well yes but no. that was another task to argue why we should never do that
oh your compare isn't stable, you need a return 0; for the second condition there
wtf, that's the standard.
I had to change mine to be descending later
Well it has problems with ints when they are too close to the max value
The operation returns minus something if you have MAX_VALUE and -1
you won't ever have that for length comparisons though.
I cant, because otherwise strings of same length get lost
you can do that with any comparator just with .reversed()
If I have string "abc" than "cde" will never be added to the list. Order doesnt matter when we have the same length so I just used 1 instead of 0
Ohhh ok. thanks
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
they don't get lost. it's doing exactly what you're telling it to
only comparing by length
what? no
Output with 0:
Output with 1:
[iernrigfnekfnerj, Meatlable, Zebra, All, No, OK]
Just adding random words
yeah. it's considering strings of equal length as equal so it doesn't add it to the set
but having it be 1 makes your sort unstable
So what shall I do than?
you need to have a definite sorting order for TreeSet
you could just make it sort alphabetically next. the equality check would be accurate there
Well, the task says order doesnt matter when the length is equal but the words just have to be there
having an unstable sort isn't exactly a good way to do that.
So
[iernrigfnekfnerj, Meatlable, Zebra, All, No, OK]
could also be
[iernrigfnekfnerj, Meatlable, Zebra, All, OK, No]
||Dude codeblocks suck on Discord||
||skill issue||
see that's an unstable sort. TreeSet, and other sorting stuff, don't like that. TreeSet just treats equality as exclusionary, since, yknow, it's a set.
||I am adding three ` prior to and after the text||
||looks like 2 in your message||
||you could also just make it inline with single backticks||
||It deletes them for no reason, LITERALLY||
[iernrigfnekfnerj, Meatlable, Zebra, All, No, OK]
[iernrigfnekfnerj, Meatlable, Zebra, All, OK, No]
||This is the way! It worked||
this is a cool way to hold simultaneous conversations.
Right
||Also right||
So can I use Comparators with array lists?
via one-time sorts, yeah. but you can't make it hold the sorting.
you can use a PriorityQueue for pop or dequeue in the desired order, but iteration doesn't give a sorted order
*List, not ArrayList
that's unanswerable
List is just an interface
well, sort exists on that interface
but beyond that, you need to ask about a concrete implementation
Yep, but the task literally says that I get a "list of results"
list doesn't always mean List
: /
list in human terms is just a sequence of elements
if it doesn't say to use List explicitly, then it probably isn't that
Result is a class here btw
but if it's just the result that matters, you could use some implementation of List and sort it before yielding the result
I get a List of "Result" objects of which every has the public methods String name and Time time. I have to sort ascending in time. It sounds like another comparator to me, right?
I found the given code, its literally a List<Result> results-list
yeah you can probably just build the comparator from static methods, really
I am a bit annoyed that I cannot test the thing right away because they didnt even add the list
Wait why did they add a compareTo method in the Time class? That makes no sense to me
why shouldn't you be able to compare times
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
I am just confused that they didnt make a getTimeValue method or something like that
Now I have to use a compareTo method in compare in the comparator
the time value is just.. itself though.
wdym by this
you just.. don't
if a class implements Comparable, then it's considered to have a natural ordering, and it contains its own Comparator
if you just omit the Comparator in the construction, or use Comparator.naturalOrder(), you can use that natural ordering
Comparator.reverseOrder() also exists to reverse the natural ordering
String, Integer, Long, etc also implement Comparable.
Does it? Because in the List the order of the object is not correct