Is it possible to compare attributes of objects in a list & if the attribute "name" happens to be the same then print out the one that is older (with the rest that does not have same name) and if their name & age happens to be the same then print out one of them (with the rest that does not have same name)? I need to compare a list with objects. I sorted the list and started with nestled for-loops but then got stuck. Are there better ways of doing this? Maybe placing some method in the Person class or another way?
enter code here
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args){
Person[] persons = new Student[4];
persons[0] = new Person("Erik",20);
persons[1] = new Person("Bob",21);
persons[2] = new Person("Erik",25);
persons[3] = new Person("Fredrik",20);
Arrays.sort(persons, Comparator.comparing(Person::getName));
for (int i = 0; i<persons.length; i )
for(int j = 0; j<i; j ){
if (persons[i].getAge() == persons[j].getAge())
}
//output: Bob, Erik, Fredrik
}
}
CodePudding user response:
Since you only want each name to be once, and the oldest, you can create what's called a HashMap, ( Key-> value association with unique key) in which you store only the oldest person
Once you have this hashmap, you then can iterate through it to print exactly what you want
Example :
public static void main(String[] args){
Person[] persons = new Student[4];
persons[0] = new Person("Erik",20);
persons[1] = new Person("Bob",21);
persons[2] = new Person("Erik",25);
persons[3] = new Person("Fredrik",20);
HashMap<String, Integer> oldest_people = new HashMap<>();
for (Person p: persons){
int person_age = oldest_people.getOrDefault(p.getName(), -1);
if (person_age != -1) {
if (p.getAge() > person_age) {
oldest_people.put(p.getName(), p.getAge());
}
}
else {
oldest_people.put(p.getName(), p.getAge());
}
}
for (Map.Entry<String, Integer> entry : oldest_people.entrySet()) {
System.out.println(entry.getKey() " age : " entry.getValue().toString());
}
}
CodePudding user response:
Alternate approach based on your program, override these below methods on a Person class
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof Person)) return false;
Person p = (Person) obj;
if(this.name.equals(p.getName())) return true;
return false;
}
And you can add custom comparator to sort person on the array
Comparator<Person> comparePerson = new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
//name in ascending order
int nameCompare = p1.getName().compareTo(p2.getName());
//age in descending order means from higher age
int ageCompare = Integer.compare(p2.getAge(), p1.getAge());
return nameCompare == 0 ? ageCompare : nameCompare;
}
};
Arrays.sort(persons, comparePerson);
After, you can find unique Person. Below, the Set collection will only add a unique Person, this time oldest age person will add.
Set<Person> personSet = new HashSet<>();
for(Person p : persons) {
if(!personSet.contains(p)) personSet.add(p);
}
Now, you can get a unique Person in collection.