Как объединить два списка пользователей, если пользователь является общим слиянием своего атрибута.import java.util.List;
import java.util.Objects;
class User {
String firstName;
String lastName;
Integer count;
Integer marks;
String status;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return Objects.equals(firstName, user.firstName) &&
Objects.equals(lastName, user.lastName);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName);
}
//constructor getter setter....
}
public class MainTest {
public static void main(String[] args) {
List source1 = List.of(new User("A", "A", 1, 0, null),
new User("B", "B", 2, 0, null),
new User("C", "C", 3, 0, null));
List source2 = List.of(new User("A", "A", 0, 10, "FAIL"),
new User("B", "B", 0, 20, "FAIL"),
new User("D", "D", 0, 30, "PASS"));
List combinedExpectedList = List.of(new User("A", "A", 1, 10, "FAIL"),
new User("B", "B", 2, 20, "FAIL"),
new User("C", "C", 3, 0, null),
new User("D", "D", 0, 30, "PASS"));
//List userList3= Stream.concat(source1.stream(), source2.stream()).distinct().collect(Collectors.toList());//Adding 4 records but marks and status not copied
//BeanUtils.copyProperties(model2, model1); - can't user don't have library
for (User t : source2) {
for (User s : source1) {
if (t.getFirstName().equals(s.getFirstName()) && t.getLastName().equals(s.getLastName())) {
t.setCount(s.getCount());
}
}
}// Not adding record with firstName=C and LastName=C
System.out.println(combinedExpectedList.equals(source2));
}
}
Подробнее здесь: https://stackoverflow.com/questions/603 ... st-in-java
Слияйте список двух объектов на Java ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Снимите целые ряды, найденные в двух 2D массивах, затем слияйте [дублировать]
Anonymous » » в форуме Php - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-
-
-
MapStruct: список объектов карты, когда объект отображается из двух объектов.
Anonymous » » в форуме JAVA - 0 Ответы
- 25 Просмотры
-
Последнее сообщение Anonymous
-