How to iterate List of one object array and set to another object list in java 8?

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

How to iterate List of one object array and set to another object list in java 8?

问题

public static class Entitlement {
   public String id;
   public String name;
   public String serviceEntitlementNumber;
   public String serviceType;
   public String alphaId;
   public String modifiedTime;
} //Entitlement obj – 

public static class Card { 
   public String id;
   public String name;
} // card obj

List<Entitlement> entitlements = getEntitlements(merchantId);

List<Card> cards = new ArrayList<>();

for (Entitlement entitlement : entitlements) {
    // 如何将此列表添加到卡片列表中。
}
英文:

Currently I have list of object(Entitlement) array, from that array I have to Iterate and add to the list of Card object, any way to do this. Please let me know.

Now I don't want entire data from entitlements list, need only id and name and want this to be store into Card object.
How i can achieve this, using stream?

public static class Entitlement {
   public String id;
   public String name;
   public String serviceEntitlementNumber;
   public String serviceType;
   public String alphaId;
   public String modifiedTime;
} //Entitlement obj – 


public static class Card { 
   public String id;
   public String name;
} // card obj

List &lt;MerchantGatewayEntitlement&gt; entitlements = 
       getEntitlements(merchantId);

List &lt;Card&gt; cards = new ArrayList &lt;&gt;();
 
for (MerchantGatewayEntitlement entitlement : entitlements) {
     how to add this list to card list.
}

答案1

得分: 2

List&lt;Card&gt; cards = entitlements.stream().map(e -&gt; new Card(e.getId(), e.getName())).toList();
英文:

To create a List of Card objects from the other List, you can use Stream#map.

List&lt;Card&gt; cards = entitlements.stream().map(e -&gt; new Card(e.getId(), e.getName())).toList();

If you want to add to an existing List, an enhanced for loop or List#forEach would be appropriate.

答案2

得分: 2

Why use streams? It has its advantages but it also has quite a bit of overhead. Nothing wrong with using a loop. And don't worry about "improving" existing working code using Java 8 features.

I don't know what your Card class looks like but there is nothing wrong with the following:

List cardList = new ArrayList<>();
for (MerchantGatewayEntitlement entitlement : entitlements) {
cardList.add(new Card(entitlement.getCard(), entitlement.getSomethingElse()));
}

or other Java 8 options.

entitlements.forEach(ent -> {
cardList.add(new Card(ent.getCard(), ent.getSomethingElse()));
});

But if you insist on using a stream solution and you need to be able to modify the returned list, then do it like this.

List cardList = entitlements.stream()
.map(ent -> new
Card(ent.getCard(), ent.getSomethingElse()))
.collect(Collectors.toCollection(ArrayList::new));

英文:

Why use streams? It has its advantages but it also has quite a bit of overhead. Nothing wrong with using a loop. And don't worry about &quot;improving&quot; existing working code using Java 8 features.

I don't know what your Card class looks like but there is nothing wrong with the following:

List&lt;Card&gt; cardList = new ArrayList&lt;&gt;();
for (MerchantGatewayEntitlement entitlement : entitlements) {
   cardList.add(new Card(entitlement.getCard(), entitlement.getSomethingElse()));
}

or other Java 8 options.

entitlements.forEach(ent-&gt; {
    cardList.add(new Card(ent.getCard(), ent.getSomethingElse()));
});

But if you insist on using a stream solution and you need to be able to modify the returned list, then do it like this.

List&lt;Card&gt; cardList = entitlements.stream()
     .map(ent -&gt; new 
         Card(ent.getCard(),ent.getSomethingElse()))
     .collect(Collectors.toCollection(ArrayList::new));



</details>



huangapple
  • 本文由 发表于 2023年3月9日 23:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686411.html
匿名

发表评论

匿名网友

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

确定