- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的测试应用程序中,我试图创建一个 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/
将一个数组列表分配给另一个数组列表与在两个数组列表之间使用 addAll 方法有什么区别? 1 > arrayList = arrayList;//应该将后面的arrayList的值分配给第一个。 2
所以我在将 ArrayList 添加到我的 ArrayList 时遇到了一些问题。将其想象成一张 table 。 下面是一些示例代码: ArrayList currentRow = new Arra
我一直在尝试转换 ArrayList> to ArrayList> 的字符串 这是我尝试构建的代码。 public void convertString (ArrayList> templist) {
我有一个 ArrayList (alal) 和一个 ArrayList(al) 的 ArrayList。我想将 al 插入 alal,但我希望 alal.get(0) 包含 al 拥有的所有内容以及添
很抱歉标题太长,如果您能想到更好的标题,请告诉我! 我正在做的是尝试创建一个 ArrayList 的 ArrayList 并将 ArrayList 逐个添加到其中。两个AL>我所拥有的称为三角形和正方
我有带有值的 mList2。存在具有相同 id 的值。如何获取具有相同 id 的对象分组的 List 或 ArrayList 并将其添加到 ArrayList>? List mList2 = list
我正在创建一个 ArrayList的 ArrayList并添加 ArrayLists给它。但每次我对 ArrayList 进行更改时, 它反射(reflect)在 ArrayList 中. 示例: L
谁能解释一下ArrayList之间的区别是什么? , ArrayList和 ArrayList是什么时候使用它们?它们在实现层面上是相同的还是各自具有不同的含义? 最佳答案 ArrayList 特别是
这个问题在这里已经有了答案: Java generics: List> = new LinkedList>() is prohibited? (3 个答案) 关闭 9 年前。 为什么这段代码可以编译
我的 arraylistS 在覆盖数组列表中的行为类似于同一个实例。 我用其中一个来操作 i=0; manupulate((ArrayList)theCoveringRootArrayList.get
我们遇到这个错误 java.lang.NullPointerException at java.util.ArrayList.(Unknown Source) at de.mystuf
据我了解,ArrayList 类继承其父“List”类的 equals() 函数来查找两个成员对象是否相同。这是否意味着“contains()”线性搜索(使用“equal”)来查找 ArrayList
这个问题已经有答案了: What is the diamond operator in Java? (2 个回答) 已关闭 7 年前。 正如标题所说,在Java中,这两种语句有什么区别吗? 通常我都能
我正在尝试求解帕斯卡三角形。我有两个用 Java 编写的代码片段,第一个创建 inner ArrayList 几次并且对我来说效果很好。 但是在代码的第二个版本中,如果我修改 inner ArrayL
正如标题所示,我有两个 ArrayList。奇怪的是,在一个数组列表上设置一个值会改变另一个数组列表的值。 一些信息:这些是 Entry 类型的 ArrayList,每个列表都包含一个金额和一个值(这
我已经添加了一个项目到列表 a,然后添加了列表 a 到列表 b 并再次做了同样的事情。 我的问题是,如果我打印 b.get(0) 和 b.get(1),我会得到相同的列表,这两个项目都是 “一”和“二
我正在创建一个 ArrayList of ArrayList of ArrayList 的 ArrayList 并按以下方式填充它。它正确地填充它。我已经通过调试和 println 弄清楚了这一点。但
实现可以在 Arraylist 和 Integer 中存储任何级别的 ArrayList 的 ArrayList 的最佳方法是什么。 List> list = ArrayList(); 仅允许列表中最
在下面的示例中,我将如何将 ArrayList al4 的内容与其他 ArrayList 中的任何一个进行比较?以同样的方式,我将 al1 与 al2 进行了比较。 import java.util.
好的,所以我之前发布了一个线程,它回答了我的很多问题并帮助我改进了我的代码,但是,我遇到了另一个问题,我不知道为什么,但我认为也许该副本只是指向原始对象..(尽管我已尽力避免这种情况) 在我的游戏代码
我是一名优秀的程序员,十分优秀!