英文:
How to Merge Two List in Java Spring
问题
这是我的控制器:
List<Equipement> equipements = equipementService.findEquipmentsByAssignment(affectationId);
List<Equipement> equipements2 = equipementService.selectSotck();
你如何将两个列表合并为一个?
英文:
This is my controller:
List<Equipement> equipements = equipementService.findEquipmentsByAssignment(affectationId);
List<Equipement> equipements2 = equipementService.selectSotck();
How can you merge two lists into one?
答案1
得分: 1
这取决于您使用的Java版本以及是否允许使用外部库(例如Guava),但在此链接中,您将找到大多数情况的几个示例:https://www.techiedelight.com/join-two-lists-java/
英文:
It depends on which version of java you use and if you are allowed to use external libs (like Guava), but here you will find few examples for most scenarios https://www.techiedelight.com/join-two-lists-java/
答案2
得分: 0
package com.elfayq.stock.services.impl;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.transaction.annotation.Transactional;
import com.elfayq.stock.dao.IEquipementDao;
import com.elfayq.stock.entities.Equipement;
import com.elfayq.stock.services.IEquipementService;
@Transactional
public class EquipementServiceImpl implements IEquipementService {
private IEquipementDao equipementsDao;
public void setEquipementsDao(IEquipementDao equipementsDao) {
this.equipementsDao = equipementsDao;
}
@Override
public Equipement save(Equipement entity) {
return equipementsDao.save(entity);
}
@Override
public Equipement update(Equipement entity) {
return equipementsDao.update(entity);
}
@Override
public List<Equipement> selectAll() {
return equipementsDao.selectAll();
}
@Override
public List<Equipement> selectAll(String sortField, String sort) {
return equipementsDao.selectAll(sortField, sort);
}
@Override
public void remove(Long id) {
equipementsDao.remove(id);
}
@Override
public List<Equipement> selectSotck() {
return equipementsDao.selectAllByField("alloted", "0");
}
@Override
public List<Equipement> findEquipmentsByAssignment(Long affectionId) {
return equipementsDao.selectAllByField("idAffectation", String.valueOf(affectionId));
}
@Override
public Equipement getById(Long id) {
return equipementsDao.getById(id);
}
@Override
public Equipement findOne(String paramName, Object paramValue) {
return equipementsDao.findOne(paramName, paramValue);
}
@Override
public Equipement findOne(String[] paramNames, Object[] paramValues) {
return equipementsDao.findOne(paramNames, paramValues);
}
@Override
public int findCountBy(String paramName, String paramValue) {
return equipementsDao.findCountBy(paramName, paramValue);
}
public List<Equipement> addAll() {
{
List<Equipement> list = list1.stream().collect(Collectors.toList());
list.addAll(list2);
return list;
}
}
}
英文:
package com.elfayq.stock.services.impl;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.transaction.annotation.Transactional;
import com.elfayq.stock.dao.IEquipementDao;
import com.elfayq.stock.entities.Equipement;
import com.elfayq.stock.services.IEquipementService;
//L'annotation transactionnel pour dire que la classe contient des m�thode de transaction avec la base de donn�es
@Transactional
public class EquipementServiceImpl implements IEquipementService{
private IEquipementDao equipementsDao;
public void setEquipementsDao(IEquipementDao equipementsDao) {
this.equipementsDao = equipementsDao;
}
@Override
public Equipement save(Equipement entity) {
return equipementsDao.save(entity);
}
@Override
public Equipement update(Equipement entity) {
return equipementsDao.update(entity);
}
@Override
public List<Equipement> selectAll() {
return equipementsDao.selectAll();
}
@Override
public List<Equipement> selectAll(String sortField, String sort) {
return equipementsDao.selectAll(sortField, sort);
}
@Override
public void remove(Long id) {
equipementsDao.remove(id);
}
@Override
public List<Equipement> selectSotck() {
return equipementsDao.selectAllByField("alloted", "0");
}
@Override
public List<Equipement> findEquipmentsByAssignment(Long affectionId) {
return equipementsDao.selectAllByField("idAffectation",String.valueOf(affectionId));
}
@Override
public Equipement getById(Long id) {
return equipementsDao.getById(id);
}
@Override
public Equipement findOne(String paramName, Object paramValue) {
return equipementsDao.findOne(paramName, paramValue);
}
@Override
public Equipement findOne(String[] paramNames, Object[] paramValues) {
return equipementsDao.findOne(paramNames, paramValues);
}
@Override
public int findCountBy(String paramName, String paramValue) {
return equipementsDao.findCountBy(paramName, paramValue);
}
public List<Equipement> addAll() {
{
List<Equipement> list = list1.stream().collect(Collectors.toList());
list.addAll(list2);
return list;
}
}
答案3
得分: 0
你可以使用Stream.concat
来合并两个流的列表:
public List<Equipement> addAll() {
return Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList());
}
或者使用flatMap
来合并列表的列表:
public List<Equipement> addAll() {
return Arrays.stream(Arrays.asList(list1, list2))
.flatMap(List::stream)
.collect(Collectors.toList());
}
或者简单地使用List.addAll
:
public List<Equipement> addAll() {
List<Equipement> all = new ArrayList<>(list1);
all.addAll(list2);
return all;
}
英文:
You could use Stream.concat
for the streams of the two lists:
public List<Equipement> addAll() {
return Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList());
}
or flatMap
for the list of lists:
public List<Equipement> addAll() {
return Arrays.stream(Arrays.asList(list1, list2))
.flatMap(List::stream)
.collect(Collectors.toList());
}
or simply List.addAll
:
public List<Equipement> addAll() {
List<Equipement> all = new ArrayList<>(list1);
all.addAll(list2);
return all;
}
答案4
得分: 0
最简单的方法,如果您不需要去重:
List
List
x.addAll(y);
英文:
Easiest way, if you don't need deduplication:
List<String> x = ...;
List<String> y = ...;
x.addAll(y);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论