gpt4 book ai didi

java - 如何在Java中通过id过滤重复的ArrayList - spring boot data jpa

转载 作者:行者123 更新时间:2023-12-01 19:47:46 25 4
gpt4 key购买 nike

在我的测试应用程序中,我试图创建一个 springboot api。我创建了一个 api,通过它我可以获得包含重复 id(sid) 的对象列表。现在我想按 sid 过滤该列表并将值 (custRefId) 与相应的 id 合并。

列出 Api 的输出:

[
{
"sId": 101,
"sName": "Nike",
"custRefId": "S1234567890a1001INR"
},
{
"sId": 201,
"sName": "Addidas",
"custRefId": "S1234567895a1004INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": "S1234567000a1258INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": "S1234567000a1011INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": "S1234567899a1008INR"
}
]

所需输出:

{
"sId": 101,
"sName": "Nike",
"custRefId": "S1234567890a1001INR"
},
{
"sId": 201,
"sName": "Addidas",
"custRefId": "S1234567895a1004INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": {"S1234567000a1258INR","S1234567000a1011INR","S1234567899a1008INR"}
}
<小时/>
enter code here

我的测试bean

package com.example.easynotes.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.example.easynotes.controller.CompositeKeyEntity;

@Entity(name = "TestApi")
@Table(name="test")
public class TestApi implements Serializable{

/**
*
*/
private static final long serialVersionUID = -6972984895555407355L;

@EmbeddedId
private CompositeKeyEntity CompositeKeyEntity;

@Column(name = "S_Id")
private Long sId;

@Column(name = "S_Name")
private String sName;

@Column(name = "B_Id")
private int bId;

@Column(name = "B_Name")
private String bName;

@Column(name = "Login_Date")
private Date loginDate;

@Column(name = "Amount")
private int amount;

@Column(name = "B_Acc")
private String bAccount;

public TestApi() {
super();
}

public TestApi(com.example.easynotes.controller.CompositeKeyEntity compositeKeyEntity, Long sId, String sName,
int bId, String bName, Date loginDate, int amount, String bAccount) {
super();
CompositeKeyEntity = compositeKeyEntity;
this.sId = sId;
this.sName = sName;
this.bId = bId;
this.bName = bName;
this.loginDate = loginDate;
this.amount = amount;
this.bAccount = bAccount;
}

public CompositeKeyEntity getCompositeKeyEntity() {
return CompositeKeyEntity;
}

public void setCompositeKeyEntity(CompositeKeyEntity compositeKeyEntity) {
CompositeKeyEntity = compositeKeyEntity;
}

public Long getsId() {
return sId;
}

public void setsId(Long sId) {
this.sId = sId;
}

public String getsName() {
return sName;
}

public void setsName(String sName) {
this.sName = sName;
}

public int getbId() {
return bId;
}

public void setbId(int bId) {
this.bId = bId;
}

public String getbName() {
return bName;
}

public void setbName(String bName) {
this.bName = bName;
}

public Date getLoginDate() {
return loginDate;
}

public void setLoginDate(Date loginDate) {
this.loginDate = loginDate;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public String getbAccount() {
return bAccount;
}

public void setbAccount(String bAccount) {
this.bAccount = bAccount;
}

}

测试 API 存储库:

package com.example.easynotes.repository;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.example.easynotes.controller.CompositeKeyEntity;
import com.example.easynotes.model.TestApi;

@Repository
public interface TestRepo extends JpaRepository<TestApi, CompositeKeyEntity>{

@Query(
value = "SELECT * FROM test WHERE S_Id = :sId",
nativeQuery = true
)
public TestApi getSDataById(@Param("sId")long sId);

@Query(value="SELECT h.S_Id,h.S_Name,h.B_Id,h.B_Name,h.Login_Date,h.Amount,h.B_Acc,h.S_Acc,h.B_Ref_Id,h.CRR FROM test h order by h.Login_Date asc",nativeQuery = true )
public List<TestApi> getSellerData();


@Query(
value = "SELECT * FROM test WHERE S_Id = :sId and S_Name =:sName",
nativeQuery = true
)
public List<TestApi> findDataBySIDAndSName(long sId, String sName);

}

TestController 类:

package com.example.easynotes.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.easynotes.model.TestApi;
import com.example.easynotes.model.TestBean;
import com.example.easynotes.repository.TestRepo;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

@RestController
@RequestMapping("/LCRF")
public class TestController {


List<TestApi> lcrf = new ArrayList<TestApi>();
List<TestApi> tmp = new ArrayList<TestApi>();

@Autowired
TestRepo lRepo;
@Autowired
Assembler assembler;

@GetMapping("/allData")
public List<TestBean> getAll()
{
getMergeIds(assembler.sBBeanList(lRepo.getSellerData()));

return assembler.sBBeanList(lRepo.getSellerData());
}

@GetMapping("/allData/{sellerId}")
public TestApi getSellerDetail(@PathVariable(value = "sellerId")Long sellerId)
{

return lRepo.getSDataById(sellerId);
}

@GetMapping("/allData/{sId}/{sName}")
public List<TestApi> getDataByIdAndName(@PathVariable(value = "sId")Long sId,@PathVariable(value = "sId")String sName)
{

lcrf = lRepo.findDataBySIDAndSName(sId, sName);
return lcrf;
}

public List<TestMergeIds> getMergeIds(List<TestBean> testBean)
{
List<TestBean> testList = new ArrayList<TestBean>();
Multimap<Long, String> multiMap = ArrayListMultimap.create();
TestMergeIds testMergeIdsBean = new TestMergeIds();
for(TestBean bean: testBean) {

multiMap.put(bean.getsId(), bean.getCustRefId());

}
Set<Long> keys = multiMap.keySet();
List<String> targetList = new ArrayList<>();
for (Long key : keys) {

}
System.out.println("Print list values ---111---"+targetList);
return null;


}
}

CompositeKeyEntity 类:

package com.example.easynotes.controller;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class CompositeKeyEntity implements Serializable{

private static final long serialVersionUID = 1581433221916043048L;

@Column(name="S_Acc")
private String sAccount;

@Column(name="B_Ref_Id")
private String bRefId;

@Column(name="CRR")
private String crr;

public String getsAccount() {
return sAccount;
}

public void setsAccount(String sAccount) {
this.sAccount = sAccount;
}

public String getbRefId() {
return bRefId;
}

public void setbRefId(String bRefId) {
this.bRefId = bRefId;
}

public String getCrr() {
return crr;
}

public void setCrr(String crr) {
this.crr = crr;
}





}

汇编器类:

package com.example.easynotes.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.example.easynotes.model.TestApi;
import com.example.easynotes.model.TestBean;

@Component
public class Assembler {

public List<TestBean> sBBeanList(final List<TestApi> testApiList)
{
final List<TestBean> SellerBuyerBeanList = new ArrayList<>(testApiList.size());
if(!testApiList.isEmpty())
{
for(final TestApi lcrfApiEntity : testApiList) {

final TestBean sellerBuyerBeanlist = new TestBean();
BeanUtils.copyProperties(lcrfApiEntity, sellerBuyerBeanlist);
sellerBuyerBeanlist.setCustRefId(formSellerBuyerRefId(lcrfApiEntity.getCompositeKeyEntity()));
SellerBuyerBeanList.add(sellerBuyerBeanlist);
}

}


return SellerBuyerBeanList;

}

private String formSellerBuyerRefId(final CompositeKeyEntity compositeKeyEntity)
{
StringBuilder sBuilder=new StringBuilder();
sBuilder.append(compositeKeyEntity.getsAccount());
sBuilder.append(compositeKeyEntity.getbRefId());
sBuilder.append(compositeKeyEntity.getCrr());
return sBuilder.toString();

}
}
enter code here

最佳答案

您可以使用 Java 8 Streams 将其过滤到一个集合中:

Set<TestApi> filteredSet = testApiList.stream()
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TestApi::getSId))));

另一个 Java 8 功能(如果您想将其保留为列表):

//Temporary set of Long that will filter the Ids
Set<Long> tempSet = new HashSet<>();

//Duplicated Ids
List<Long> duplicatedList = new ArrayList<>();

//The same list but filtered
testApiList.removeIf(e -> {
boolean duplicated = !tempSet.add(e.getSId());
if (duplicated) duplicatedList.add(e.getSId());
return duplicated;
});

编辑

添加了重复列表。

关于java - 如何在Java中通过id过滤重复的ArrayList - spring boot data jpa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52503150/

25 4 0