/help

What is wrong with it? I don't understand why I get an error
No description
No description
20 Replies
JavaBot
JavaBot10mo ago
This post has been reserved for your question.
Hey @Yarden! Please use /close or the Close Post button above when your problem is solved. 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.
dan1st
dan1st10mo ago
If you use a TreeMap, you need to tell it how to sort keys which you can do by making the key type (Contact) implement Comparable (meaning two contacts can be compared to each other) alternatively, you can use a custom Comparator or not use a TreeNao
Yarden
YardenOP10mo ago
@dan1st | Daniel Thank you for the help! Like this?
No description
JavaBot
JavaBot10mo ago
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.
dan1st
dan1st10mo ago
yes but make sure your compareTo method properly compares the contacts
Yarden
YardenOP10mo ago
Yes I did it seems like it works
dan1st
dan1st10mo ago
it should - return 0 if they are equal - return a value >0 if this is "bigger" than other - return a value <0 if this is "smaller" than other
john
john10mo ago
There is a possibility you'll explain me why it's necessary? I don't understand why this compareTo function fixed everything. I mean, why I even needed it? why the line:
"if(map.containsKey(contacts[i])) {
map.remove(contacts[i]);
}
"if(map.containsKey(contacts[i])) {
map.remove(contacts[i]);
}
" needed compareTo in orther to work If it's too much that's ok, I'll try to search about it
This message has been formatted automatically. You can disable this using /preferences.
dan1st
dan1st10mo ago
Do you know what TreeMap does? or how it is different from HashMap
Yarden
YardenOP10mo ago
Yes TreeMap getskey and value and automatically sort it with the other ones compared to the key
dan1st
dan1st10mo ago
exactly
Yarden
YardenOP10mo ago
It's a red-black tree as i know
dan1st
dan1st10mo ago
and if you want to sort something, you need to know what's before and what's after
Yarden
YardenOP10mo ago
right
dan1st
dan1st10mo ago
you can't sort stuff if you don't have a way to know which order to put it and if the keys are Comparable, you are specifying an order
Yarden
YardenOP10mo ago
Right Understood that's why it gave the error, because in every insertion of key&value it needs to compare
dan1st
dan1st10mo ago
yes
Yarden
YardenOP10mo ago
Got it Thank you very much for your help
JavaBot
JavaBot10mo ago
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.
JavaBot
JavaBot10mo ago
Post Closed
This post has been closed by <@218019068219883520>.
Want results from more Discord servers?
Add your server