I got two "lists" of objects, which i want to compare if elements are equal. If they are not equal, the loop should take the not equal object and put it into the other list. Very simple. My problem is: the equals method doesnt work as intended.
Here is the object Class with my custom equals method:
public class Profil {
private String vorname;
private String name;
private String adLoginBenutzer;
public Profil() {
}
public String getAdLoginBenutzer() {
return adLoginBenutzer;
}
public void setAdLoginBenutzer(String adLoginBenutzer) {
this.adLoginBenutzer = adLoginBenutzer;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
if (name == null || vorname == null) {
return "<keiner>";
}
return vorname + ", " + name + " " + adLoginBenutzer;
}
#Override
public boolean equals(Object obj) {
if (getClass() != obj.getClass()) {
return false;
}
Profil other = (Profil)obj;
if(!this.getVorname().equals(other.getVorname()) || !this.getName().equals(other.getName()) || !this.getAdLoginBenutzer().equals(other.getAdLoginBenutzer()))
{
return false;
}
return true;
}
}
And here is the loop: (note: I basically want to merge the list into a comboboxmodel, if the profil-object is not equal than it should add it to the first position in the comboboxmodel)
public void putProfilesIntoCbx(HashSet<Profil> profile)
{
DefaultComboBoxModel<Profil> cbx = (DefaultComboBoxModel <Profil>)cbBearbeiter.getModel();
for(Profil p : profile)
{
for(int i = 0; i< cbx.getSize(); i++)
{
if(!p.equals(cbx.getElementAt(i)))
{
cbx.insertElementAt(p, 0);
}
}
}
cbBearbeiter.setModel(cbx);
}
I debugged the code and took breakpoints at the last if of the equals method. Although there are equal objects, the last if return false for no reason even if the objects are really equal. Even if i invert the equals if-statement it does not work.
As everyone is saying, there is a relationship between the equals() method and the hashcode() method.
If you #Override the equals() method, you need to #Override the hashcode() method as well
Related
I was practicing my Java 8 skills. I came across a strange (for me) code. I have my bean class Person with overridden equals method. Then I tried to implement BiPredicate with equals method. It ran successfully. Can anyone explains how's that possible..because in my opinion equals method takes 1 argument and BiPridicate's test method takes two arguments. How is it satisfying this condition?
My code--
Method_Ref1
package method_referencing;
import java.util.function.BiPredicate;
import method_referencing.Person;
//1. static ....
//2. instance ...
//3. arbitary object
//4. constructor
public class Method_Ref1 {
public static void main(String[] args) {
System.out.println(checkHere(Person::equals));
}
static boolean checkHere(BiPredicate<Person,Person> pc) {
Person p1 = new Person(11,"Tom","Male","coder");
Person p2 = new Person(21,"Tom","male","coder");
return pc.test(p1, p2);
}
}
Person
package method_referencing;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 5721690807993472050L;
int id;
String name;
String gender;
String note;
public Person() {
}
public Person(int id, String name, String gender, String note) {
this.id = id;
this.name = name;
this.gender = gender;
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
#Override
public String toString() {
return "id=" + id + ", name=" + name + ", gender=" + gender + ", note=" + note + "";
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((note == null) ? 0 : note.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Object.equals() accepts a single parameter. It is right. But here your introduced a function that accepts both the object to compare (this) and the parameter expected for equals (the other object).
So you need a BiPredicate<Person,Person> to allow to pass both information.
I think that the origin of your confusion is the method reference :
checkHere(Person::equals);
Convert it into a lambda, it should do things clearer :
(o1, o2) -> o1.equals(o2)
You indeed need to pass two arguments to the function to allow it substitute o1 and o2 and you do that :
return pc.test(p1, p2);
I have a problem with Spring Boot.
I am making a REST application, and I have a service that returns a Map(Share, Integer)
Share is a class written by me:
public class Share {
private String ticker;
private String name;
private Double value;
public Share() {
super();
}
public Share(String ticker, String name, Double value) {
super();
this.ticker = ticker;
this.name = name;
this.value = value;
}
public String getTicker() {
return ticker;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ticker == null) ? 0 : ticker.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Share other = (Share) obj;
if (ticker == null) {
if (other.ticker != null)
return false;
} else if (!ticker.equals(other.ticker))
return false;
return true;
}
#Override
public String toString() {
return "Share [ticker=" + ticker + ", name=" + name + ", value=" + value + "]";
}
}
And the #RestController is:
public class ShareController {
#Autowired
private ShareBussines shareBussines;
#RequestMapping("/getShare/{ticker}")
public Share getShare(#PathVariable("ticker") String ticker) throws BrokerNotFoundException, BrokerArgumentException, BrokerGeneralException {
return shareBussines.getShare(ticker);
}
#RequestMapping(value="/buyShares", method=RequestMethod.POST)
public Map<Share, Integer> buyShares(#RequestBody Map<String,Double> sharesToBuy) throws BrokerGeneralException, BrokerArgumentException, BrokerInsufficientStockException {
return shareBussines.buyShares(sharesToBuy);
}
}
The problem is when I call the service from Postman.
The result is:
{
"Share [ticker=AMZN, name=Amazon, value=259.32126508258295]": 1,
"Share [ticker=GOOGL, name=Google, value=249.35339337497606]": 1,
"Share [ticker=FB, name=Facebook, value=181.15005639608364]": 55
}
The Map key is share.toString()... I want the key to be the share JSON.
I try to remove the toString method from Share class, but the result was:
{
"Share#1eb87f": 1,
"Share#40d9fab": 1,
"Share#8db": 54
}
It is using the Object's toString().
Thank you for your advice.
First, it works as you coded it to work:
#RequestMapping(value="/buyShares", method=RequestMethod.POST)
public Map<Share, Integer> buyShares(#RequestBody Map<String,Double> sharesToBuy) throws BrokerGeneralException, BrokerArgumentException, BrokerInsufficientStockException {
return shareBussines.buyShares(sharesToBuy);
}
Share is a key here. And that is kinda weird. Why not create some object like:
public class ShareResponse {
private Share share;
private Integer someVal; // that's the one you have in your Map as a value
// getters and setters
}
And afterward change your service a bit:
#RequestMapping(value="/buyShares", method=RequestMethod.POST)
public List<ShareResponse> buyShares(#RequestBody Map<String,Double> sharesToBuy) throws BrokerGeneralException, BrokerArgumentException, BrokerInsufficientStockException {
// do your business here, create a list of ShareResponse and return it
return shareBussines.buyShares(sharesToBuy); // instead of this
}
And you should get a valid, nicely 'formatted' JSON. If you need each item to be identifiable by some unique value just add some ID field to ShareResponse.
Does it make any sense?)
I have a doubt, I want to check if TreeSet in java really uses shallow copy for its clone(), but as per my program if I remove a element from parent treeset, its not reflecting in its cloned treeset object.
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<Name> nameTreeSet = new TreeSet<>();
nameTreeSet.add(new Name("Compiere"));
nameTreeSet.add(new Name("Aristotle"));
nameTreeSet.add(new Name("CompierE"));
nameTreeSet.add(new Name("COmpiere"));
nameTreeSet.add(new Name("ArisTotle"));
nameTreeSet.add(new Name("arisTotle"));
nameTreeSet.add(new Name("aristotle"));
System.out.println(nameTreeSet);
TreeSet<Name> cloneNameTreeSet = (TreeSet<Name>) nameTreeSet.clone();
System.out.println(nameTreeSet);
Iterator<Name> itr = nameTreeSet.iterator();
/*while (itr.hasNext()) {
if (itr.next().getName().equals("aristotle"))
itr.remove();
}*/
for(Name name: nameTreeSet) {
if(name.getName().equals("aristotle"))
nameTreeSet.remove(name);
}
System.out.println(nameTreeSet);
System.out.println(cloneNameTreeSet);
}
}
/*
*Name class which is used in my treeset to store its objects
*/
public class Name implements Cloneable, Comparable<Name>, Comparator<Name> {
#Override
public String toString() {
return "Name [name=" + name + "]";
}
private String name;
public Name(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Name other = (Name) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
#Override
public int compare(Name name1, Name name2) {
return name1.name.compareTo(name2.name);
}
#Override
public int compareTo(Name name) {
return (this.name).compareTo(name.name);
}
}
if I remove a element from parent treeset, its not reflecting in its cloned treeset object.
You are misunderstanding what shallow copy means. It means that for each element of the TreeSet, the reference is copied to the new TreeSet. So if you mutate one of the Name objects located in one Set, the corresponding element in the other Set will also be mutated, since both refer to the same object.
For example, this will affect both Sets :
for(Name name: nameTreeSet) {
if(name.getName().equals("aristotle"))
name.setName("new name");
}
However, the cloned TreeSet is a different object than the original TreeSet, and removing elements from one doesn't affect the other. Removing elements from one Set would affect the other only if instead of cloning you just copy the reference - TreeSet<Name> cloneNameTreeSet = nameTreeSet;.
I have a list of an Object and I want to detect whether Object Id is duplicate or not.
Here is the object:
public class data{
private String id;
private String value;
private String status;
}
All duplicate data will have "invalid" status except the first one.
What is the most effective way for this?
You could consider overriding the .equals() method of the data class.
Doing so would allow you to do the following to check for duplicate elements:
ArrayList<data> array_list = new ArrayList<data>();
// add some elements to array list
// check for duplicates
for(int i =0; i < array_list.size(); i++){
for(int j=0; j<array_list.size(); j++){
// compare for equality if it is not the same element
if(i != j){
if(array_list.get(i).equals(arrayList.get(j))){
// than we know there is a duplicate at index i,j
System.out.println("duplicate indexes: " + i + ", " + "j");
}
}
}
}
Here is an example of how you would override the .equals method of the data class.
#Override
public boolean equals(Object obj) {
if (!(obj instanceof data)){ return false; }
if (obj == this) { return true; }
// compare strings to see if they are equal
data other_data = (data)obj;
boolean id_equal = other_data.id.equals(this.id);
boolean value_equal = other_data.value.equals(this.value);
boolean status_equal = other_data.status.equals(this.status);
return id_equal && value_equal && status_equal
}
Edit
If you only want to know whether the id's are equal or not you don't need to override .equals() of the data class.
In this case you need only need to use the first loop and compare the id stings instead of the data objects.
So instead of array_list.get(i).equals(arrayList.get(j),
you would do (assuming you have getter methods for the private members of data):
array_list.get(i).get(id).equals(array_list.get(j).get(id));
Alternatively you could use a method similar to the first one and override .equals() to only compare the id strings.
use java ConcurrentHashMap instead of arraylist.
ConcurrentHashMap<yourid, YourBean> chp = new ConcurrentHashMap<>();
chp.putIfAbsent(yourid, YourBean);
and to list all your id do something like this
for (Entry<yourid, YourBean> e : chp.entrySet())
{
YourBean object = (YourBean )chp.get(e.getKey());
//do what u want with your object, guess that helps
}
Try like this first you should override equals method to check duplicates
private class data{
private String id;
private String value;
private String status;
public data(String id, String value, String status) {
this.id = id;
this.value = value;
this.status = status;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#Override
public String toString() {
return "data{" +
"id='" + id + '\'' +
'}';
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof data)) return false;
data data = (data) o;
return !(id != null ? !id.equals(data.id) : data.id != null);
}
#Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
Then test like this
public class Test {
public static void main(String[] args ) {
List<data> dataList=new ArrayList<>();
dataList.add(new data("1","somevalue","somestatus"));
dataList.add(new data("1","somevalue","somestatus"));
dataList.add(new data("1","somevalue","somestatus"));
dataList.add(new data("2","somevalue","somestatus"));
dataList.add(new data("3","somevalue","somestatus"));
List<data>validList=new ArrayList<>();
List<data>duplicateList=new ArrayList<>();
for (data data:dataList){
if (!(validList.contains(data))){
validList.add(data);
System.out.println(validList);
}else{
duplicateList.add(data);
System.out.println(duplicateList);
}
}
}
Make a list of the id of objects. Loop over the list of objects. See if the id of each object is already in the list. If the id is already present, then change the status of the object. Otherwise, add the id of the object to the list.
Hello my goal is to get the user to input some customerID value and some videoID how would I check to make sure the user inputs a correct customerID and videoID in my array I have made for these fields and tell the user they have inputted the wrong ID. This is what i have so far for my code
public static void HireVideo(){
System.out.println("Enter Customer ID");
String customerID = sc.next();
System.out.println("Enter Video ID");
String videoID = sc.next();
HireList.add(new Hire(customerID, videoID));
}
in an effort to be as close to your code as possible (i.e. what I assume is a static list wrapper class).
public class HireList {
private static List<Hire> hires = new ArrayList<Hire>();
public static void add(Hire hire) {
hires.add(hire);
}
public static boolean contains(Hire other) {
return hires.contains(other);
}
}
the magic however happens in the Hire class:
public class Hire {
private String customerId;
private String videoId;
public Hire(String customerId, String videoId) {
this.customerId = customerId;
this.videoId = videoId;
}
#Override
public int hashCode() {
int hash = 3;
hash = 67 * hash + Objects.hashCode(this.customerId);
hash = 67 * hash + Objects.hashCode(this.videoId);
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final Hire other = (Hire) obj;
if (!this.customerId.equals(other.customerId)) {
return false;
}
if (!this.videoId.equals(other.videoId)) {
return false;
}
return true;
}
}
List<T>.contains(T other) uses the equals method of T, therefore overriding it will allow us to control the behavior of contains.
test:
public static void main(String[] args) {
HireList.add(new Hire("1", "1"));
HireList.add(new Hire("2", "2"));
System.out.println(HireList.contains(new Hire("2", "2"))); //output: true
}
if contains is all you are concerned with though I would suggest using a HashSet as opposed to a ArrayList.