How to sort list of objects? [duplicate] - java

This question already has answers here:
How to sort a List/ArrayList?
(21 answers)
Closed 4 years ago.
I have a list of objects, each object has a method that returns distance from player to said object like that
object.distance(player)
now i need to sort that list from loqwest distance to furthest

You can use interface java.lang.Comparable in java
Example (pseudo code) assuming object.distance(player) returns an Integer value
class Distance implements Comparable<Distance>{
/**
* Compare a given Distance with this object.
*/
public int compareTo(Distance o) {
return this.distance(player).compareTo(o.distance(o.player));
}
}
now you can sort your list like
Collections.sort(YourListOfDistance)
here some reference
When should a class be Comparable and/or Comparator?

Related

Java HashSet remove item by its hash code [duplicate]

This question already has answers here:
Remove object from ArrayList with some Object property
(5 answers)
Remove objects from an ArrayList based on a given criteria
(10 answers)
How to remove specific object from ArrayList in Java?
(17 answers)
Closed 1 year ago.
I have a class with its own hashCode() method. I am adding this class to a HashSet. How can I remove an item by its hashCode, without knowing the object itself?
For example, if I have the following code
HashSet<Data> set = new HashSet<>();
set.add(new Data(10, 5));
...
class Data {
public int importantVal;
public int notImportantVal;
//... constructor ...
#Override
public int hashCode() {
return importantVal;
}
}
and I knew the importantVal of a Data object, but not the object itself. How would I remove it? set.remove(10) does not work.
Best solution I can think of is to also override equals() to return if importantVal is the same, and then do set.remove(new Data(10, anyPlaceholderValue))

HashCode implementation for generic objects [duplicate]

This question already has answers here:
HashCode for Generic objects in Java
(3 answers)
Closed 4 years ago.
I've a Node class with this parameters:
public class Node < T > {
private T value;
private int priority;
}
I know that I need to override the hashcode method but I don't know how to do it because value is a generic object. I read that it could be done by using the address of the object, but is not recommended because the JVM can change the address of the object during the execution of the program.
You should combine value.hashCode() with the priority value in order to calculate a Node's hashCode().
For example:
#Override
public int hashCode()
{
return 31 * value.hashCode() + priority;
}
This relies on users of your Node class overriding hashCode() in the relevant classes (the classes used as types of Node value).
Note that this is the way JDK collection classes implement hashCode. For example, the default implementation of hashCode() for Sets is the sum of the hashCode()s of the Set's elements.

Custom compare function in Priority Queue [duplicate]

This question already has answers here:
Anonymous inner class using an interface in Java
(1 answer)
Multiple inheritance for an anonymous class
(6 answers)
Closed 5 years ago.
So here is code which implements a min-heap :
PriorityQueue<String> minHeap = new PriorityQueue<>(k,new Comparator<String>(){
public int compare(String s1, String s2){
return Integer.compare(s1.length(), s2.length());
}
});
I am confused about the compare function. It appears we are overriding the compare function in Comparator but how? Where can I learn about overriding methods upon object instantiation in java?

Sort an ArrayList of object by title property [duplicate]

This question already has answers here:
Sorting a list of points with Java [duplicate]
(4 answers)
How do I use Comparator to define a custom sort order?
(9 answers)
Closed 9 years ago.
I have an objects class that holds the properties title, director, genre, rating. I have created an arraylist and have filled it with instances of this base class
ArrayList<Movie> movieCatalog
I am wanting to sort this ArrayList in alphabetical order by the title property and change their positions within the ArrayList. From my research I understand I need to use a Comparator class but I am confused about how to do this.
You can create a custom comparator and than call the Collections.sort(movieCatalog,comparator); method.
E.g.
public static final Comparator<movieCatalog> movieComparator = new Comparator<movieCatalog>() {
public int compare(movieCatalog a1, movieCatalog a2) {
return a1.name.compareTo(a2.name);
}
};
Collections.sort(movieCatakig,movieComparator);
Your Movie class needs to implement Comparable<Movie>. Then you need to implement the compareTo method, which in this case can just call the String class compareTo method.
int compareTo(Movie other) {
return this.title.compareTo(other.title);
}
After that, if movies is an ArrayList of movies, you can simply do
Collections.sort(movies);

Generating hashCode from multiple fields? [duplicate]

This question already has answers here:
Creating a hashCode() Method - Java
(4 answers)
Closed 9 years ago.
How do I generate a hashCode from two fields in my class?
For example, I want Pair classes with the same objects V to have the same hashCode:
public class Pair<V> {
V from, to;
}
Should I multiply their hashCodes together? Add them? Multiply them with a prime?
One way to do it is adding the hash code of the first field to hash code of the second field, multiplied by a small prime number, like this:
public int hashCode() {
return 31 * from.hashCode() + to.hashCode();
}

Categories