Java: Check if two objects of the same class have identical values - java

I want to know how to check if two objects of the same class have the same values ​​in each attribute.
For example:
public class Person {
String name;
String surname;
String country;
int age;
public Person(String name, String surname, String country, int age) {
this.name = name;
this.surname = surname;
this.country = country;
this.age = age;
}
public boolean samePerson(Person person){
//CODE
}
Person person1 = new Person("Abel", "Smith", "EEUU", 26);
Person person2 = new Person("Alexa", "Williams", "Canada", 30);
Person person3 = new Person("Abel", "Smith", "EEUU", 26);
Person person4 = new Person("Alexa", "Williams", "EEUU", 30)
person1.samePerson(person2) // return false
person1.samePerson(person3) // return true
person2.samePerson(person3) // return false
person2.samePerson(person4) // return false
The only thing I can think of is to compare the attributes one to one. Is there a simpler way?
Sorry for my english
Thanks in advance

The only thing I can think of is to compare the attributes one to one. Is there a simpler way?
Unfortunately not. You'll have to write code to do just that. And if you do, consider putting that code in equals and hashCode methods.

There is no simpler way. You have to implement your own way of doing this because it is a class you made yourself.
You are off to a good start. You can use your samePerson() method to provide this
functionality, or, like #Thilo said, use the equals and hashCode methods.
It should go along the lines of:
public boolean samePerson(Person person){
return this.name.equals(person.name) &&
this.surname.equals(person.surname) &&
this.country.equals(person.country) &&
this.age == person.age;
}
With perhaps some sanity null checking and whatever else you require.

The correct answer is using equals() and hashCode() but if you are looking for something else, you could consider introducing a id for each person - maybe a social security number. Then the comparsion of persons could be implemented in the public boolean isSamePersonAs(Person person) and compare only that.

You need to specify how the object must be compared
and to have it compared properly implement hashcode method.
code is as below, add this in your class and you wil get desired o/p.
#Override
public int hashCode() {
int prime=31;
int sum = prime*this.name.hashCode();
sum=sum+this.surname.hashCode();
sum=sum+this.country.hashCode();
sum=sum+this.age;
return sum;
}
#Override
public boolean samePerson(Object p) {
if(p==null)
return false;
if(! (p instanceof Person))
return false;
Person person = (Person)p;
return this.name.equals(person.name) && this.surname.equals(person.surname) && this.country.equals(person.country) && this.age == person.age;
}

Folks try this one i guess it will work for you
***********This is class one "Person"*********
public class Person {
String name;
String surname;
String country;
int age;
public Person(String name, String surname, String country, int age) {
this.name = name;
this.surname = surname;
this.country = country;
this.age = age;
}
}
********This is main class*******
public class mainclass {
public static void main(String arg[])
{
Person person1 = new Person("Abel", "Smith", "EEUU", 26);
Person person2 = new Person("Alexa", "Williams", "Canada", 30);
Person person3 = new Person("Abel", "Smith", "EEUU", 26);
Person person4 = new Person("Alexa", "Williams", "EEUU", 30);
System.out.println(samePerson(person1,person2));
System.out.println(samePerson(person1,person3));
System.out.println(samePerson(person2,person4));
System.out.println(samePerson(person3,person4));
System.out.println(samePerson(person1,person4));
}
static boolean samePerson(Person personA,Person personB)
{
if(personA.name.equals(personB.name) && personA.country.equals(personB.country)&& personA.surname.equals(personB.surname )&& personA.age==personB.age )
return true;
else
return false;
}
}
Thanks and Regards.

Usually you would use getter for each field. You should use a generic way here and call all the getter via reflection on each instance, after that compare them with equals.
#Edit
See here: Java Reflection: How can i get the all getter methods of a java class and invoke them
and here: http://tutorials.jenkov.com/java-reflection/getters-setters.html

Related

Why Java Method Reference of instance method cannot be assigned to Consumer interface

Here is my Code :
public class SearchByLambda {
private Map<String,Consumer<Person>> searchCritertiaHolder = new HashMap<String,Consumer<Person>>();
private static final String AGED = "aged";
public SearchByLambda(){
searchCritertiaHolder.put(AGED, (Person p)-> {p.filterAgedPerson(p);} );
}
private Consumer<Person> getFilter(String personType){
return searchCritertiaHolder.get(personType);
}
public static void main(String[] args) {
SearchByLambda searchUsage = new SearchByLambda();
Person p = new Person(59,"shailesh");
Person p1 = new Person(58,"ganesh");
searchUsage.getFilter(AGED).accept(p);
searchUsage.getFilter(AGED).accept(p1);
Person.printAgedPersons();
}
}
class Person{
private static List<Person> agedPersons = new ArrayList<>();
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(int age,String name){
this.age = age;
this.name = name;
}
public void filterAgedPerson(Person person){
if(person.getAge() > 58){
agedPersons.add(person);
}
}
public static void printAgedPersons(){
for(Person person : agedPersons){
System.out.println(person.getName());
}
}
}
When I replace following Lambda expression
searchCritertiaHolder.put(AGED, (Person p)-> {p.filterAgedPerson(p);});
with
searchCritertiaHolder.put(AGED, Person::filterAgedPerson);
it gives me compilation error. I am using java 8 and and compiling through eclipse. Why is this so? Why cannot I assign method reference for instance method of any arbitrary object to consumer functional interface?
Your definition of filterAgedPerson takes a Person as an argument, even though it is not a static method. It doesn't need to, and it shouldn't if you want to use it as a Consumer<Person>. What you are ending up with is something compatible with BiConsumer<Person, Person>.
It might help to think of it this way: method references to non-static methods always take an "extra" argument which is used as this.
The easiest way for you to fix this with your current code structure is to modify the filterAgedPerson method to not take a Person as an argument
public void filterAgedPerson() {
if (this.getAge() > 58) {
agedPersons.add(person);
}
}
As an aside, you might want to also consider making your filters Predicate<Person> instead of Consumer<Person> and moving the results handling elsewhere. This will give you more flexibility as things get more complicated.

How do I use Java getters and setters with a collection of data without explicitly typing out the attributes for each item?

I am very new to Java and to programming in general, and I have an assessment to complete where I load employees (with name, age, and department attributes; department can be only one of four enumerated values) into a program that will sort them by age and tell if the age is a prime number. The assignment requires Company, Department, and Employee classes. I am confident that I can figure out age/prime components β€” I know how to google for algorithms. What I am struggling with is putting all the discrete pieces into a cohesive whole.
Here is what I have so far. I've put in one employee, but the way I'm doing it seems completely inelegant and inefficient. I am sure there is a better way, but I've hit a mental block.
EDIT: as was pointed out below, I was unclear. What I am asking help with is populating the data structure.
Company class:
public class Company {
static Employee one = new Employee();
public static void main(String[] args) {
one.setName("Counting Guru");
one.setAge(55);
one.setDepartment(DepartmentList.ACCOUNTING);
}
}
DepartmentList class:
import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
public enum DepartmentList {
ACCOUNTING, MARKETING, HUMANRESOURCES, INFORMATIONSYSTEMS;
public static void main(String[] args) {
Map<DepartmentList,String>
enumMap=new EnumMap<DepartmentList,String>(DepartmentList.class);
enumMap.put(DepartmentList.ACCOUNTING, "Accounting");
enumMap.put(DepartmentList.MARKETING, "Marketing");
enumMap.put(DepartmentList.HUMANRESOURCES, "Human Resources");
enumMap.put(DepartmentList.INFORMATIONSYSTEMS, "Information Systems");
Set<DepartmentList> keySet = enumMap.keySet();
for (DepartmentList department : keySet) {
String value = enumMap.get(department);
System.out.println("ENUMMAP VALUE:"+value);
}
}
}
Employee class:
public class Employee {
String empName;
int empAge;
DepartmentList empDept;
Employee() {
}
public String getName() {
return empName;
}
public void setName(String name) {
this.empName = name;
}
public int getAge() {
return empAge;
}
public void setAge(int age) {
this.empAge = age;
}
public DepartmentList getDepartment() {
return empDept;
}
public void setDepartment(DepartmentList department) {
this.empDept = department;
}
public Employee(String empName, int empAge, DepartmentList empDept){
}
}
I also have a Department class, but it's currently empty.
Am I on the right track? Can someone give me a nudge? Thank you!
Don't hard-code the data inside the Java program. Put the data in a file and write methods to load the data.
If you MUST hardcode the data in the program, use something like this sample:
public class Employee
{
String name;
int age;
public Employee(String name, int age)
{
this.name = name;
this.age = age;
}
// getters, setters, etc.
}
In the main program
private static Employee[] empData =
{
new Employee("John Smith", 50),
new Employee("Fred Jones", 25),
.
.
.
};
Now you have a static array of Employee objects that you can "load" into your data structure.
If you're asking if there is something like a property in Java, no, there isn't (at least not yet).
If you're asking how to populate your objects something like an IOC container, like Spring, would be a better choice.
Now as it comes to your code you have two main methods in two different classes. Only one will be called. If you want to create a static instance you will be better do
static Employee one = new Employee("Counting Guru", 55, DepartmentList.ACCOUNTING);
or
static Employee one = new Employee();
static {
one.setName("Counting Guru");
one.setAge(55);
one.setDepartment(DepartmentList.ACCOUNTING);
}
When it comes to the enum then you'll better define a constructor for it
public enum DepartmentList {
ACCOUNTING("Accounting"), MARKETING("Marketing");
private String displayName;
public DepartmentList(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return diplayName;
}
}
In the Employee constructor you need to assign the field values to the ones received as arguments.

Why are the global lists affected even if i have not changed or set any values into them?

this is my example object
object name: OBJ
object properties: String name, int age
now i have 2 global lists
List<OBJ> lstobj = new ArrayList<OBJ>;
List<OBJ> anotherlist = new ArrayList<OBJ>;
then i added a few records in both lists like this
Name:Ben Age:5
Name:Charles Age: 6
Name:Dan Age:7
Now I needed to change Charles' age to "10"
and so first I should find Charles in the list and get the obj from the lists
OBJ newobj = new OBJ;
for(OBJ obj : lstobj){
if(obj.getName.equals("Charles"){
newobj = obj;
}
}
and now i need to set the retrieved obj's age to 10.
newobj.setAge(10);
this action changes not just the "lstobj" but the "anotherlist" as well. How do i set the retrieved obj without affecting the two global lists?
Lists store references to objects. If you stick the same reference into multiple lists, and then change the object, it'll change everywhere.
If you wish the contents of lstobj and anotherlist to be completely independent of each other, you need to insert different objects into them. This will probably involve making copies of your objects.
Try using a copy constructor
public class Person {
private String name;
private String age;
public Person(String name,String age) {
this.name = name;
this.age = age;
}
public Person(Person person){
this.name = person.getName();
this.age= person.getAge();
}
public Person(){
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "[Name:"+this.name+"][Age:"+this.age+"]";
}
public static void main(String[] args) {
Person person1 = new Person("Charles","20");
Person person2 = new Person("Rob","30");
List<Person> persons = new ArrayList<Person>();
persons.add(person1);
persons.add(person2);
List<Person> morePersons = new ArrayList<Person>();
morePersons.add(new Person(person1));
morePersons.add(new Person(person2));
Person newobj = new Person();
for (Person obj : persons) {
if (obj.getName().equals("Charles")) {
newobj = obj;
newobj.setAge("10");
}
}
System.out.println(persons);
System.out.println("======");
System.out.println(morePersons);
}
}
The output is
run:
[[Name:Charles][Age:10], [Name:Rob][Age:30]]
======
[[Name:Charles][Age:20], [Name:Rob][Age:30]]
BUILD SUCCESSFUL (total time: 1 second)
In your case you are modifying just one object but reflects in both list.
This is because list stores object reference, here both the list refer to the same object that you are modifying so the changes that you make is reflected in both the list.
What i did here is that i am copying the objects details and creating a new object and its reference is different from the original object.
Any changes you make in the old object will not change the new one, since they have different references

Composition vs Bi-directional Association in Java

I understand the differences in theory, but what is the difference in the code implementation? Can somebody provide some examples?
Purpose we have students and universities
class University {
private final Set<Student> students = new HashSet<Student>();
void addStudent(Student s){students.add(s);}
}
class Student {
private final String name;
public Student(String name) {
this.name = name;
}
}
We create this stuff in some way
University university = new University();
Student bob = new Student("Bob");
university.addStudent(bob);
And know we need to know does Bob studies in university.
So we create some new method for university
boolean contains(Student student){
for(Student s : students){
if(s.equals(student)) return true;
}
return false;
}
and, than do smt like university.contains(bob).
But what will be if we havent link to uniwersity. We need to ask it Bob. But Bob doesn't know. So we go from composition to bi-derection and create smt like
class University {
private final Set<Student> students = new HashSet<Student>();
void addStudent(Student s){
students.add(s);
s.setUniversity(this);
}
boolean contains(Student student){
for(Student s : students){
if(s.equals(student)) return true;
}
return false;
}
}
class Student {
private final String name;
private University university;
public Student(String name) {
this.name = name;
}
void setUniversity(University u){
university = u;
}
boolean doYouStudyInUniversity(){
return university != null;
}
}
//ask
bob.doYouStudyInUniversity();
Composition is, in effect, uni-directional association - except that semantically, we interpret it as meaning "that thing is part of this thing" rather than simply "this thing holds a reference to that thing".

Collection with multiple values in key

I'm looking for a Collection type data structure to implement the following. Say I have a class like this:
class Person() {
String homeTown; // key
String sex; // key
String eyeColour; // key
String name;
long height;
// other stuff....
}
I am processing multiple Person objects. I want to organise them into sets whereby each set contains Person objects with the same homeTown, sex and eyeColour. At the moment I am implementing something like this:
Map<String, HashSet<Person>> = new HashMap<String, HashSet<Person>>;
where the key is a concatanation of the homeTown, sex and eyeColour. This works but seems a bit untidy - can anyone suggest a more elegant solution or a better data structure to use, thanks?
You could restructure your class to make the key explicit. This is more robust than simply concatenating the key values and avoids any additional object creation overhead at the point when you wish to store your Person instance in a Map, because you've eagerly created the key in advance.
public class Person {
public class Key {
private final String homeTown;
private final String sex;
private final String eyeColour;
public Key(String homeTown, String sex, String eyeColour) { ... }
public boolean equals(Object o) { /* Override to perform deep equals. */ }
public int hashCode() { /* Could pre-compute in advance if the key elements never change. */ }
}
private final Key key;
private final String name;
private final long height;
public Person(String homeTown, String sex, String eyeColour, String name, long height) {
this.key = new Key(homeTown, sex, eyeColour);
this.name = name;
this.height = height;
}
public Key getKey() {
return key;
}
public String getName() { return name; }
public long getHeight() { return height; }
}
Create an object to model your key. For example class PersonKey { String homeTown, sex, eyeColour } (getters and setters omitted for brevity)
Implement the equals and hashCode method for this object.
Use this object as the key in your Map.
Either remove the attributes from your Person object or replace them with a reference to your PersonKey object.
In addition, consider making the type of your map the following i.e. you don't need to specify what type of Set you are using as the key to your map.
Map<String, Set<Person>> = new HashMap<String, Set<Person>>();
And, if you are using a Set<Person> then you'll need to override equals and hashCode for the Person as well, otherwise the Set cannot correctly determine if two Person objects represent the same person or not, which is needed to make sure the collection contains only unique elements.
org.apache.commons.collections.map.MultiValueMap
You can use guava's Sets.filter method to filter the person objects.
Example:
Person class:
public class Person {
String name;
String hometown;
int age;
public Person(String name, String hometown, int age) {
this.name = name;
this.age = age;
this.hometown = hometown;
}
#Override
public int hashCode() {
int hash = 17;
hash = 37 * hash + name.hashCode();
hash = 37 * hash + hometown.hashCode();
hash = 37 * hash + age;
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
Person p;
if (obj instanceof Person)
p = (Person) obj;
else
return false;
if (this.name.equals(p.name) && this.hometown.equals(p.hometown)
&& this.age == p.age)
return true;
return false;
}
#Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("[name = ").append(name).append("\n");
b.append("home town = ").append(hometown).append("\n");
b.append("age = ").append(age).append("]");
return b.toString();
}
}
TestGuavaFilter class:
public class TestGuavaFilter {
public static void main(String[] args) {
Set<Person> set = new HashSet<Person>();
set.add(new Person("emil", "NY", 24));
set.add(new Person("Sam", "NY", 50));
set.add(new Person("george", "LA", 90));
System.out.println(Sets.filter(set, new FilterHomeTown("NY")));
}
}
class FilterHomeTown implements Predicate<Person> {
String home;
public FilterHomeTown(String home) {
this.home = home;
}
#Override
public boolean apply(Person arg0) {
if (arg0.hometown.equals(this.home))
return true;
return false;
}
}
The advantage of using a filter is that you can filter Person object in any way ,suppose you want to filter only using home-town and not the other 2 attributes this will helpful.More over since guava's filter only produces a view of the the real Set,you can save memory.

Categories