Java HashSet remove item by its hash code [duplicate] - java

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))

Related

What is the best way to set empty array for properties in class Java? [duplicate]

This question already has answers here:
Default constructor vs. inline field initialization
(5 answers)
Closed 3 years ago.
I have a class like:
public class TemplateFileResponse {
private String path;
private List<FileView> children;
}
I want to create an instance and set children is empty array. so what is the best way to do it?
You can create an empty list with the new operator:
public class TemplateFileResponse {
private String path;
private List<FileView> children = new ArrayList<>();
}
You may also want to initialize the path field, either in a constructor or inline, because otherwise it will be initialized to null by default.
I suggest that you read a tutorial about Java classes, constructors, methods, and instantiating objects to understand how all of this works.

How to sort list of objects? [duplicate]

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?

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?

Java : Sort Collection<Class> given a custom order [duplicate]

This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 8 years ago.
I would like to sort a List/Set of Class given a personnal order (like A.class < B.class < C.class for instance). But Class doesn't implements Comparable, and I obviously can't customize the class Class without rewriting the whole Java language, so what are my options?
Thanks a lot !
You can use a custom Comparator. Just implement that interface and you can use it with Collections.sort() and sorted sets (pass the comparator as constructor)
Comparator<Class<?>> c=new Comparator<Class<?>>{
#Override
int compare(Class<?> a, Class<?> b) {
// your comparison logic
}
};
List<Class<?>> list= ...
Collections.sort(list, c);
Set<Class<?>> set=new TreeSet<>(c);

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);

Categories