简体   繁体   中英

How to compare two objects in an array and replace it's values if they are equal

For instance, I am creating and storing objects with three parameters (Bob, Marley, 55) with a Scanner in an array of objects and while input does not equal to stop it keeps storing. I need to find out while looping if the first name equals to any object's first name in an array if so I want to replace its age.

Thank you in advance!

According to my understanding, You can use a map where your key will be first name and the the object will be value. Pseudo code will be like.Let me know if I missed anything.

yourObject obj;
Map<String, yourObject> mapBasedOnFirstName = new Hashmap<>();

if (mapBasedOnFirstName.contains(obj.getFirstName)) {
yourObject obj2 = mapBasedOnFirstName.get(obj.getFirstName);
obj2.setAge(obj.getAge);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM