Questions tagged [equals]
Refers to Java equals method, indicating whether some object is "equal to" this one.
3,103
questions
0
votes
0
answers
72
views
Comparing numbers by three decimal points [duplicate]
public class DecimalComparator {
public static void main(String[] args) {
System.out.println(areEqualByThreeDecimalPlaces(3.174 , 3.175));
System.out.println(...
0
votes
1
answer
26
views
How to write equals method using Apache commons-lang3?
package org.sudoku;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.util.*;
public class SudokuBoard {
private final ...
3
votes
0
answers
97
views
What does Java's record equals() method actually do under the hood?
In my Java code, I have a simple record class just like so:
record SingleIndexProperties<A>(A property) implements IndexProperties {
// Here go implementations of IndexProperties methods; ...
-2
votes
0
answers
29
views
Why equals() method doesn't work when comparing two array objects in Java? [duplicate]
I just learnt that we can't compare Strings in java using == operator because that will compare the references. Instead we use equals() method as it compares the actual object.
String str1 = "...
-1
votes
1
answer
42
views
DataClasses in Kotlin - Overriding the equals() from 'Any' Class
I've been working on overriding the equals() from Any class. But in the below code. I'm confused why
"if (other is Person)" is necessary to get the "if(this.firstName == other.firstName)...
-2
votes
1
answer
85
views
Objects.equals() vs StringUtils.isNotBlank() in Java
Could you please explain me if there is a difference between the functionality of the following codes?
I know that the Objects.equals() checks for the NULL value first and then compares the two ...
-1
votes
1
answer
41
views
How to use equals & hashcode from generic parent class in child classes? [closed]
I have many Entity classes in my project, and to avoid overriding the equals and hashCode methods in each one, I want to inherit them from a parent class where these methods are already implemented. ...
1
vote
1
answer
207
views
In Java, am I safe to assume that `==` will work to compare objects of type `Class<?>`? [duplicate]
I understand that if an object does not explicitly override equals, it uses the base Object.equals() instead, which is effectively no different from the == operator.
Class<?> does not explicitly ...
0
votes
1
answer
27
views
Hazelcast object equals for replace method callingfrom client
I have configured hazelcast map with in-memory-format: OBJECT.
Now I am trying to use method map.replace(key,oldValue,newVal) assuming that it will use defined equals method in my class. But I make ...
0
votes
1
answer
28
views
Customize Lombok equalsAndHashCode() on list attribute
Is there a way to customize the Lombok equalsAndHashCode() on list attribute?
I have a subclass that looks like so:
public class subClass extends superClass {
private List<Long> ids;
For 2 ...
1
vote
1
answer
96
views
In Java, during Object#equals() call, do both objects necessarily have same hashCode ? Do we need to ever compare hashCode within equals()?
I have read somewhere, in context of HashMap :
hashCode() allows sorting objects by their hash values, and then the Object#equals method only needs to be invoked when objects share the same hash value....
0
votes
0
answers
103
views
Yet Another Implementation of JPA Entity equals()/hashCode()
The history of the JPA entity equals() and hashCode() methods started a long ago. There is a ton of discussions, just google for the "jpa equals hashcode" and you'll find a brilliant Vlad ...
1
vote
1
answer
45
views
Why am I getting "Cannot resolve method 'equals(long)'"
I am trying to build a REST API for an airport app. The issue is when I try to update my Airplane which has a many-to-one relationship with Airline, I try to check if the id of the airline which the ...
1
vote
1
answer
76
views
Why are 2 org.apache.hc.core5.http.Header not equal when created with the same content?
Why does
import org.apache.hc.core5.http.Header;
Assertions.assertEquals(new BasicHeader("name", "value", true), new BasicHeader("name", "value", true));
or
...
0
votes
1
answer
107
views
java record with mutable properties (like set or list) with equals()
I know, Java records are value based, i.e. by default two records are equal if both records have the same values (compared to Java classes where by default two classes are equal if their memory ...
1
vote
0
answers
88
views
Comparing two deques, one implemented by an Array and another implemented with a LinkedList, for equality?
I am working on implementing a custom Deque interface. Everything is working except the equals methods. Whenever I submit my code to an auto-checker, it fails one of the tests for equality and outputs:...
1
vote
4
answers
152
views
Comparing objects with equals
I have a task to compare between two objects in java. One is a Date that has days, months and years. the second one extends it, DateT with hours and minutes. In order to to that:
I can't use any ...
0
votes
0
answers
23
views
Can I compare DB entities by equals in hashcode with different fields?
I have Question DB entity:
public class Question {
@Id
@UuidGenerator
@Column(name = "question_id")
private String questionId;
@Column(name = "text")
...
1
vote
2
answers
141
views
Assertions about comparing different types: redundant and silly?
So I have this Java code that overrides equals for a class:
@Override public boolean equals(Object o) {
if (o instanceof NumeroSeccion) {
NumeroSeccion numSec = (NumeroSeccion) o;
...
0
votes
0
answers
27
views
Element with List (generic) - issues with equals
I had to program a doublyLinkedList and an ArrayList with methods that adds an element with its value and its frequency... If there is another element with the same value the frequency increases. ...
0
votes
0
answers
567
views
4.15.5 Fraction is Comparable Java CodeHS
I am trying to implement a Comparable interface in a Fraction class that I coded for an exercise problem in CodeHS. The objective of the code is to compare two fractions and determine whether they are ...
0
votes
0
answers
37
views
Difference between using == and equals to compare hashmap values (Leetcode question 657) [duplicate]
Link:https://leetcode.com/problems/robot-return-to-origin/
Question: There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ...
0
votes
2
answers
225
views
why override java's equals() method this way?
I try to override equals() method from Object class.
@Override
public boolean equals(Object o){
if(this == o)
return true;
if(!(o instanceof Menu))
return false;
Menu menu =...
0
votes
1
answer
133
views
How JSONAssert will treat int 5 and double 5.0 same
I have two JSON objects represented as Java Strings
Expected:
{
"id" : "1234567"
"balance" : "123"
}
I converted this into JSON object and trying to validate ...
1
vote
1
answer
176
views
Is there a way to check if two lists have the same type of elements in Java?
I have a MyDoublyLinkedList<E> which works essentially like the usual DLLs without dummies. I want to write an equals(Object obj) method for the class, and I realized that I can only check for ...
0
votes
2
answers
106
views
Two different Java objects are always equal regardless of fields value
I have classes defined like below:
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public abstract class A {
private Enum myEnum;
}
@Getter
@Setter
@NoArgsConstructor
@...
0
votes
3
answers
94
views
How to properly define method equals()? [closed]
I'm learning Java language and I have one question for you.
For example:
I have a class Employee like this:
public class Employee {
private int id;
private String name;
public Employee(...
1
vote
1
answer
342
views
Equals Verifier fails with error related to hashcode check for JPA entity
Given we have such class:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class User {
@Id
private Long id;
private String name;
private Integer age;
@...
0
votes
1
answer
166
views
Best practice for implementation equals and hashcode
I've revealed that @Data annotation in Lombok is bad practice now when we use JPA https://dzone.com/articles/lombok-and-jpa-what-may-go-wrong
So what is the best practice for implementation of these ...
1
vote
1
answer
129
views
.equals() on a PriorityQueue returns unexpected results
I'm sorry if this is a dumb question (especially for someone who has developed in java for over 4 years) or if it has been answered (I've searched stack overflow and despite the simplicity of the ...
1
vote
2
answers
127
views
How to correctly implement equals(), hashCode() if the values are within a range of each other?
The criteria is that equals() method where the objects are considered equal if the value of the double variable is within +/- 10 of the other object's value of the double variable.
I'm not sure how to ...
-3
votes
2
answers
257
views
equals and hashCode with many fields in Java? [closed]
In Java apps, I prefer to use unique fields in equals and hashCode methods instead of adding only id field or all the fields. However, I am confused about the following points:
By considering object ...
1
vote
2
answers
374
views
Add unique elements in HashSet based on attributes of framework provided non-editable object classes
I am trying to generate a HashSet containing unique Employee instances. Uniqueness should be established based on the object properties.
The problem is that I end up with having duplicates.
Note that ...
2
votes
2
answers
529
views
Should I use getters or fields in JPA entity equals methods?
When writing an equals method for my JPA entity, should I access the fields directly, or should I go through the getters?
In other words, do I do this?
@Override
public boolean equals(Object o) {
...
1
vote
3
answers
549
views
How to force object equivalence for keys in a Kotlin hashmap?
I have a class 'Foo' (not under my control) which I wish to use as a key in a kotlin (java) hashmap.
The problem is that the 'equals' method for 'Foo' does value equivalence.
For my situation value ...
-1
votes
1
answer
71
views
Best way to compare different elements in equals() method for custom LinkedList
In my class below I have 2 strings and an equals method for my custom LinkedList. Now I know how to compare different objects in my equals method but say I wanted to compare name earlier in my ...
1
vote
2
answers
46
views
return statements are not coming out right
I need to code a method that checks if:
A = all numbers are equal.
B = no numbers are equal.
C = at least two numbers are equal.
Im just beginning to learn all this in uni but I cant seem to figure ...
1
vote
1
answer
142
views
Java equals() for Objects (Super&Subclasses)
Hi i'm just beginner learning about abstract classes & interfaces.
Everything we build our prof is testing by creating clones and comparing objects.
I've learned overriding the equals() method the ...
1
vote
1
answer
71
views
Equals method override messes up other code/changes output?
I'm making a card game for a school project. I need my equals method to be able to correctly tell me if a card has the same rank as another card. However, I notice that as long as an equals override ...
2
votes
2
answers
129
views
Validate Hardcoded Password
I am using Kotlin.
I have a stored password (val password = 1111). I have an editText (passcode) that takes a "numberPassword" input. I would like the user to input the code and if it ...
0
votes
1
answer
53
views
Is it necessary to override hashcode method in non hashed datastructure
What I understand from hashcode method for objects in Java: it is required to calculate the hashcode of objects which in turn is used to calculate the index / bucket location of the object in a hashed ...
1
vote
1
answer
332
views
Why am I not covering totaly hashcode test with lombok.Data
I am looking for the reason why jacoco is it showing me that i am not covering totaly my functions equals and hashCode.
I have this model code :
import lombok.Data;
@Data
public class Building2 {
...
2
votes
1
answer
50
views
List contains method not work on two different classes with the same attributes
I have these two java classes as below:
RelocationDisabledProduct
public class RelocationDisabledProduct {
@JsonProperty("productIdentifierType")
private ProductIdentifierType ...
-1
votes
1
answer
189
views
How to compare efficiently 2 cycles (a circular list) and tell if they equivalent?
A cycle/ring is a data structure representable as a List in which the end is connected with the begin.
Rotating the List doesn't change its meaning since a cycle can be read starting from whatever ...
1
vote
3
answers
56
views
Calling the equals the wrong way?
I was using a PriorityQueue, and I overrode the equals in MyClass,
PriorityQueue<MyClass> pq = ...
I wanted to make some foreign objects equals to MyClass,
if (pq.contains(foreignObj)) {
...
...
-1
votes
2
answers
94
views
Using getClass.equals(otherObj.getClass()) in an implementation of equals(Object otherObj)
A boilerplate implementation of an equals(Object otherObj) reads like this:
@Override
public boolean equals(Object other)
{
if(this == other)
return true;
if(other =...
-2
votes
1
answer
85
views
When I compare Character in java, Why is equals() not working, but == is?
I'm trying to solve the classic Valid Parentheses question on LeetCode.
This is what I did, and it can pass all the test cases:
public boolean isValid(String s) {
Deque<Character> stack = ...
0
votes
2
answers
294
views
Need to write a equals-method to check if a book is the same as another book but one book contains multiple values
Below is my code at this moment, I need to add to the equals method so when I create a two books they will only be equal if both of the attributes are the same. Hopefully you guys can help.
public ...
0
votes
2
answers
331
views
Comapring data class in Kotlin using == operator
I am trying to write an assertion by comparing two Kotlin data classes.I am just simplifying the question by using a minimal class.
data class X(val isThatSo: Boolean) {
val name:String = "xyz&...
2
votes
3
answers
1k
views
Using Primiry Key (id) while overriding equals() and hashCode() methods
I am really too confused with the equals() and hashCode() methods after reading lots of documentation and articles. Mainly, there are different kind of examples and usages that makes me too confused.
...