如何在Java Spring中合并两个列表

huangapple go评论77阅读模式
英文:

How to Merge Two List in Java Spring

问题

这是我的控制器:

List<Equipement> equipements = equipementService.findEquipmentsByAssignment(affectationId);
List<Equipement> equipements2 = equipementService.selectSotck();

你如何将两个列表合并为一个?

英文:

This is my controller:

List&lt;Equipement&gt; equipements = equipementService.findEquipmentsByAssignment(affectationId);
List&lt;Equipement&gt; 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&#39;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&lt;Equipement&gt; selectAll() {	
return equipementsDao.selectAll();
}
@Override
public List&lt;Equipement&gt; selectAll(String sortField, String sort) {		
return equipementsDao.selectAll(sortField, sort);
}
@Override
public void remove(Long id) {
equipementsDao.remove(id);	
}
@Override
public List&lt;Equipement&gt; selectSotck() {		
return equipementsDao.selectAllByField(&quot;alloted&quot;, &quot;0&quot;);
}
@Override
public List&lt;Equipement&gt; findEquipmentsByAssignment(Long affectionId) {		
return equipementsDao.selectAllByField(&quot;idAffectation&quot;,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&lt;Equipement&gt; addAll() {
{
List&lt;Equipement&gt; 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&lt;Equipement&gt; addAll() {
    return Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList());
}

or flatMap for the list of lists:

public List&lt;Equipement&gt; addAll() {
    return Arrays.stream(Arrays.asList(list1, list2))
                 .flatMap(List::stream)
                 .collect(Collectors.toList());
}

or simply List.addAll:

public List&lt;Equipement&gt; addAll() {
    List&lt;Equipement&gt; all = new ArrayList&lt;&gt;(list1);
    all.addAll(list2);
    return all;
}

答案4

得分: 0

最简单的方法如果您不需要去重

List x = ...;
List y = ...;
x.addAll(y);

英文:

Easiest way, if you don't need deduplication:

List&lt;String&gt; x = ...;
List&lt;String&gt; y = ...;
x.addAll(y);

huangapple
  • 本文由 发表于 2020年10月12日 03:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64308335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定