Skip to content

Commit 56dbb72

Browse files
committed
Add java8 streaming example - filter one object types to create another type
1 parent ded26d9 commit 56dbb72

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.openapex.samples.misc.stack;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class StreamingAPI57152335 {
8+
public static void main(String[] args) {
9+
List<AttributeValue> attributeList = createAttributes();
10+
List<AttributeOption> optionList = new ArrayList<AttributeOption>();
11+
exampleCurrentWay(attributeList, optionList);
12+
System.out.println("Attributes: "+attributeList);
13+
System.out.println("Options: "+optionList);
14+
optionList = new ArrayList<AttributeOption>();
15+
exampleStreamingApiWay(attributeList, optionList);
16+
System.out.println("Attributes: "+attributeList);
17+
System.out.println("Options: "+optionList);
18+
}
19+
20+
private static void exampleCurrentWay(List<AttributeValue> attributeList, List<AttributeOption> optionList){
21+
if(attributeList != null){
22+
for(AttributeValue aValue : attributeList){
23+
if(aValue.getAttributeType().equalsIgnoreCase("Select")){
24+
//AttributeOption aOption = service.getAttributeOption(accessToken, value.getId());
25+
AttributeOption aOption = new AttributeOption(aValue.getCode());
26+
aOption.setAttributeCode(aValue.getCode());
27+
optionList.add(aOption);
28+
}
29+
}
30+
}
31+
}
32+
33+
private static void exampleStreamingApiWay(List<AttributeValue> attributeList, List<AttributeOption> optionList){
34+
if(attributeList != null){
35+
optionList.addAll(attributeList.stream().filter(attribute -> attribute.getAttributeType().equals("Select"))
36+
.map(attribute -> new AttributeOption(attribute.getCode())).collect(Collectors.toList()));
37+
}
38+
}
39+
40+
private static List<AttributeValue> createAttributes(){
41+
List<AttributeValue> attribute = new ArrayList<AttributeValue>();
42+
attribute.add(new AttributeValue("Select", "001"));
43+
attribute.add(new AttributeValue("Abc", "002"));
44+
attribute.add(new AttributeValue("Select", "003"));
45+
attribute.add(new AttributeValue("Select", "004"));
46+
attribute.add(new AttributeValue("Xyz", "005"));
47+
return attribute;
48+
}
49+
50+
private static class AttributeValue {
51+
private String attributeType;
52+
private String code;
53+
54+
public AttributeValue(String attributeType, String code){
55+
this.attributeType = attributeType;
56+
this.code = code;
57+
}
58+
59+
public String getAttributeType() {
60+
return attributeType;
61+
}
62+
63+
public void setAttributeType(String attributeType) {
64+
this.attributeType = attributeType;
65+
}
66+
67+
public String getCode() {
68+
return code;
69+
}
70+
71+
public void setCode(String code) {
72+
this.code = code;
73+
}
74+
75+
@Override
76+
public String toString() {
77+
return "AttributeValue{" +
78+
"attributeType='" + attributeType + '\'' +
79+
", code='" + code + '\'' +
80+
'}';
81+
}
82+
}
83+
84+
private static class AttributeOption {
85+
private String attributeCode;
86+
87+
public AttributeOption(String attributeCode){
88+
this.attributeCode = attributeCode;
89+
}
90+
91+
public String getAttributeCode() {
92+
return attributeCode;
93+
}
94+
95+
public void setAttributeCode(String attributeCode) {
96+
this.attributeCode = attributeCode;
97+
}
98+
99+
@Override
100+
public String toString() {
101+
return "AttributeOption{" +
102+
"attributeCode='" + attributeCode + '\'' +
103+
'}';
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)