Why do we really need Comparator in Java? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
My questions is basically divided into two sub-questions:
Comparable talks about natural ordering. Who is stopping us from implementing a non-natural ordering login in the compareTo method ?
Comparator can do the same stuff as Comparable (ASC or DESC sort). So the only reason it exists is because that if we have a third party class which we cannot change (make it implement Comparable) then we can externalise the sorting logic using Comparator. Is this correct ?

A class can only have one compareTo method but you can define as many comparators as you like for it. This is useful to define different orderings which is not such an uncommon requirement.

Related

How to find sort order in compare method Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
In short the question is how can we find inside compare method of Comparator implementation whether sort order is ascending (simple) or descending (reversed). One method I found is to place instanceof check for ReverseOrder class. But that is highly unreliable, as there are many implementations of Comparator class for reverse order.
In other words, what if we have to write a comparator, which should not allow reversed(or descending) order.
You don't. There is absolutely no way to prevent a Comparator being converted to a descending-order comparator. If you are given a Comparator, then that defines ascending order, even if it's descending relative to another comparator.
If there is one unique, ascending order that you always want to use, then the type should implement Comparable, instead of having a Comparator, and you should use the natural ordering of the type.

How do I create a HashSet of unique values without comparison in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a complex Java Bean with ~20 attributes.
In my business logic, I am generating around 10^5 unique instances of this Bean.
The bean has a complex and performance costly equals method.
My API signature is old and I can return the data only in a HashSet data structure.
I require to generate this HashSet from the unique instances without invoking the equals method of the bean to have the flow optimized.
Is it possible?
It is guaranteed, the data to be inserted in HashSet are unique beforehand.
IdentityHashMap uses == instead of equals() to compare keys when two of them have the same hash code.
You can create a set from it:
Set<E> set = Collections.newSetFromMap(new IdentityHashMap<>());

Java 1.7 HashMap Integer Key [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm using an HashMap<Integer, MyObject> collection to store a list of few couples of values.
Is it safe use Integer as key? the behavior is the same both on put and get method?
yes it's safe. Integer is just a wrapper class for int.
i know this from own experience. created a captcha functionality that way. (user requests a random integer. my prog creates a captcha-pic and saves the answer[value] and random integer id[key] in a hashmap. btw if you want to create a "cache" use java's LinkedHashMap. it has a protected boolean method called "removeEldestEntry(Map.Entry eldest)" which is called during a put-event. if you override it with, eg "return size() > MAX_SIZE;" it will delete the oldest entry (or the entry based on oldest access - there's an option in the constructor) if the condition you provided returns true). hope this helped. :/

Why hashCode and equals methods are not working when a List implemented object stores its own object as a element in the list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why don't hashCode() and equals() work when a List implemented object stores its own object as a element?
A List bases its equals relation on the contents of the list. If one of the members is the list itself, infinite recursion ensues. The same problem occurs with hashCode.

Java book was unclear on these two topics [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The basic components of a class:
Is that they can be thought of as templates from which objects can be made?
Three things you can assign to a numeric variable:
Are they: Constants, numbers other variables.
Thanks! I'm trying to pick up the basics, but these two topics from my book have no clear cut answer just a header with an example.
1)I think you have the right idea, but really bad terminology. A class is an object that consists of some data and some code. You can create an instance of a class (called instantiating the class). Each instance has its own copy of all non-static variables in the class.
2)You can assign a number to a numeric variable. Yes, that number could be a constant, another variable, or some mathematical combination of other variables (like x+y, or 5+10, or 15*z). Or a numeric result of a function (say Math.max(x,y) which returns the bigger of x and y).

Categories

Resources