英文:
How to combine two lists from the same relation in Java-8
问题
我有以下实体 Manager
和 Colleague
。
Manager 实体
@Entity
@Table(name = "Manager")
@Data
public class Manager implements java.io.Serializable {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@OneToMany
@JoinColumn(name = "id")
private List<Colleague> colleagues;
}
Colleague 实体
@Entity
@Table(name = "Colleague")
@Data
public class Colleague implements java.io.Serializable {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
}
上述关系可以表示为 JSON
:
[
{
"id": "101",
"name": "manager1",
"colleagues": [
{
"id": "101",
"name": "colleague1"
},
{
"id": "101",
"name": "colleague2"
}
]
},
{
"id": "101",
"name": "manager2",
"colleagues": [
{
"id": "101",
"name": "colleague3"
},
{
"id": "101",
"name": "colleague4"
}
]
}
]
我正在使用 managerReposiotry.findAll()
检索结果:
List<Manager> managerList = managerReposiotry.findAll();
我想要创建一个包含来自 Manager
和 Colleague
的所有姓名的超级列表。
我目前正在执行以下操作:
List<String> names = new ArrayList<>();
managerList.stream()
.forEach(manager -> {
List<String> nameList =
manager.getColleagues().stream()
.map(colleague -> colleague.getName())
.collect(Collectors.toList());
names.addAll(nameList);
}
);
在 Java 8
中是否有其他方法可以改进上述代码?
谢谢!
英文:
I have below entities Manager
and Colleague
Manager Entity
@Entity
@Table(name = "Manager")
@Data
public class Manager implements java.io.Serializable {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@OneToMany
@JoinColumn(name = "id")
private List<Colleague> colleagues;
}
Colleague Entity
@Entity
@Table(name = "Colleague")
@Data
public class Colleague implements java.io.Serializable {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
}
Above relation can be represented in JSON
as
[
{
"id": "101",
"name": "manager1",
"colleagues": [
{
"id": "101",
"name": "colleague1"
},
{
"id": "101",
"name": "colleague2"
}
]
},
{
"id": "101",
"name": "manager2",
"colleagues": [
{
"id": "101",
"name": "colleague3"
},
{
"id": "101",
"name": "colleague4"
}
]
}
]
I am retrieving the result on managerReposiotry.findAll()
as
List<Manager> managerList = managerReposiotry.findAll();
I want to create a super list of all names from Manager
and Colleague
What I am currently doing is
List<String> names = new ArrayList<>();
managerList.stream()
.forEach(manager -> {
List<String> nameList =
manager.getColleagues().stream()
.map(colleague -> colleague.getName())
.collect(Collectors.toList());
names.addAll(nameList);
}
);
Is there any other way in Java-8
to improve the above code?
Thank you !!
答案1
得分: 3
你可以使用 flatMap
来展开所有 Manager
的 Colleague
,然后仅获取 Colleague
的姓名并以列表形式收集。
List<String> names =
managerList.stream() // ...Stream<Manager>
.flatMap(m -> m.getColleagues().stream()) // ...Stream<Colleague>
.map(c -> c.getName()) // ...Stream<String>
.collect(Collectors.toList());
但更好的方式是,如果所有的同事都有经理,直接从数据库中获取。
@Query("select c.name from Colleagues c")
List<String> findAllColleagueName();
英文:
You can use flatMap
to flatten all Colleague
of Manager
then map Colleague
name only and collect as list.
List<String> names =
managerList.stream() // ...Stream<Manager>
.flatMap(m -> m.getColleagues().stream()) // ...Stream<Colleague>
.map(c-> c.getName()) // ...Stream<String>
.collect(Collectors.toList());
But the better way is directly fetched from the database if all colleagues have manager.
@Query("select c.name from Colleagues c")
List<String> findAllColleagueName();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论