英文:
Java Streams functional transform / enrich data
问题
我想编写一个将DataX
转换/丰富为List<DataY>
的函数
class DataX {
private String a;
private List<String> c;
}
class DataY {
private String a;
private String b;
private String cx;
}
JSON表示
输入
{
"a": "abc-123",
"c": ["c1", "c2", "c3", "c4", "c5"]
}
在处理c
的每个元素时,将检查其值与查找表进行对比(可以是数据库、本地缓存或REST查询),并且一些值从1映射到多个。
c1 -> c1
c2 -> c2
c3 -> c31, c32, c33
c4 -> c4
c5 -> c51, c52
期望的输出,
[
{
"a": "abc-123",
"b": "xyz-1",
"cx": "c1"
},
{
"a": "abc-123",
"b": "xyz-2",
"cx": "c2"
},
{
"a": "abc-123",
"b": "xyz-3",
"cx": "c31"
},
{
"a": "abc-123",
"b": "xyz-4",
"cx": "c32"
},
{
"a": "abc-123",
"b": "xyz-5",
"cx": "c33"
},
{
"a": "abc-123",
"b": "xyz-6",
"cx": "c4"
},
{
"a": "abc-123",
"b": "xyz-7",
"cx": "c51"
},
{
"a": "abc-123",
"b": "xyz-8",
"cx": "c52"
}
]
注意:值
b
代表通过UUID.randomUUID()
方法生成的UUID
我的尝试
public class Test {
public static void main(String[] args) {
DataX dataX = new DataX("abc-123", Arrays.asList("c1", "c2", "c3", "c4", "c5"));
List<DataY> collect = dataX.getC().stream()
.map(c -> new DataY(dataX.getA(), UUID.randomUUID().toString(), c))
.map(dataY -> {
if (isOneToMany(dataY.getCx()))
return operationOneToMany(dataY.getCx());
else
return dataY.getCx();
})
.collect(Collectors.toList());
}
private static boolean isOneToMany(String input) {
return (Arrays.asList("c3", "c5").contains(input));
}
private static Optional<List<String>> operationOneToMany(String input) {
if ("c3".equals(input))
return Optional.of(Arrays.asList("c31", "c32", "c33"));
else if ("c5".equals(input))
return Optional.of(Arrays.asList("c51", "c52"));
else
return Optional.empty();
}
}
顺便提一下,它不起作用
可工作的代码,但不确定是否优化
public class Test2 {
public static void main(String[] args) {
DataX dataX = new DataX("abc-123", Arrays.asList("c1", "c2", "c3", "c4", "c5"));
System.out.println("dataX = " + dataX);
List<DataY> collect = dataX.getC().stream()
.map(c -> operationOneToMany(c))
.flatMap(e -> e.stream())
.map(c -> new DataY(dataX.getA(), UUID.randomUUID().toString(), c))
.collect(Collectors.toList());
System.out.println("collect = " + collect);
}
private static List<String> operationOneToMany(String input) {
if ("c3".equals(input))
return Arrays.asList("c31", "c32", "c33");
else if ("c5".equals(input))
return Arrays.asList("c51", "c52");
else
return Arrays.asList(input);
}
}
英文:
I want to write a DataX
to List<DataY>
transform/enrich function
class DataX {
private String a;
private List<String> c;
}
class DataY {
private String a;
private String b;
private String cx;
}
JSON representation
Input
{
"a": "abc-123",
"c": ["c1", "c2", "c3", "c4", "c5"]
}
As processing each element of c the value is checked against a lookup table (can be a database, local cache, or REST lookup) and some get mapped from 1 to many.
c1 -> c1
c2 -> c2
c3 -> c31, c32, c33
c4 -> c4
c5 -> c51, c52
Expected output,
[
{
"a": "abc-123",
"b": "xyz-1",
"cx": "c1"
},
{
"a": "abc-123",
"b": "xyz-2",
"cx": "c2"
},
{
"a": "abc-123",
"b": "xyz-3",
"cx": "c31"
},
{
"a": "abc-123",
"b": "xyz-4",
"cx": "c32"
},
{
"a": "abc-123",
"b": "xyz-5",
"cx": "c33"
},
{
"a": "abc-123",
"b": "xyz-6",
"cx": "c4"
},
{
"a": "abc-123",
"b": "xyz-7",
"cx": "c51"
},
{
"a": "abc-123",
"b": "xyz-8",
"cx": "c52"
}
]
> Note: Value b represent UUIDs generated via UUID.randomUUID() method
My attempt
public class Test {
public static void main(String[] args) {
DataX dataX = new DataX("abc-123", Arrays.asList("c1", "c2", "c3", "c4", "c5"));
List<DataY> collect = dataX.getC().stream()
.map(c -> new DataY(dataX.getA(), UUID.randomUUID().toString(), c))
.map(dataY -> { // use flatMap here?
if (isOneToMany(dataY.getCx()))
return operationOneToMany(dataY.getCx());
else
return dataY.getCx();
})
.collect(Collectors.toList());
}
private static boolean isOneToMany(String input) {
return (Arrays.asList("c3", "c5").contains(input));
}
// Shall I apply this to each element, and return list of 1 or list of many elements
private static Optional<List<String>> operationOneToMany(String input) {
if ("c3".equals(input))
return Optional.of(Arrays.asList("c31", "c32", "c33"));
else if ("c5".equals(input))
return Optional.of(Arrays.asList("c51", "c52"));
else
return Optional.empty();
}
}
> BTW, it's not working
Working code but not sure if optimised
public class Test2 {
public static void main(String[] args) {
DataX dataX = new DataX("abc-123", Arrays.asList("c1", "c2", "c3", "c4", "c5"));
System.out.println("dataX = " + dataX);
List<DataY> collect = dataX.getC().stream()
.map(c -> operationOneToMany(c))
.flatMap(e -> e.stream())
.map(c -> new DataY(dataX.getA(), UUID.randomUUID().toString(), c))
.collect(Collectors.toList());
System.out.println("collect = " + collect);
}
private static List<String> operationOneToMany(String input) {
if ("c3".equals(input))
return Arrays.asList("c31", "c32", "c33");
else if ("c5".equals(input))
return Arrays.asList("c51", "c52");
else
return Arrays.asList(input);
}
}
答案1
得分: 1
按照 @Rono 的建议:
public class Test2 {
public static void main(String[] args) {
DataX dataX = new DataX("abc-123", Arrays.asList("c1", "c2", "c3", "c4", "c5"));
System.out.println("dataX = " + dataX);
List<DataY> collect = dataX.getC().stream()
.flatMap(e -> operationOneToMany(e).stream())
.map(c -> new DataY(dataX.getA(), UUID.randomUUID().toString(), c))
.collect(Collectors.toList());
System.out.println("collect = " + collect);
}
private static List<String> operationOneToMany(String input) {
if ("c3".equals(input))
return Arrays.asList("c31", "c32", "c33");
else if ("c5".equals(input))
return Arrays.asList("c51", "c52");
else
return Arrays.asList(input);
}
}
英文:
As suggested by @Rono
public class Test2 {
public static void main(String[] args) {
DataX dataX = new DataX("abc-123", Arrays.asList("c1", "c2", "c3", "c4", "c5"));
System.out.println("dataX = " + dataX);
List<DataY> collect = dataX.getC().stream()
.flatMap(e -> operationOneToMany(e).stream())
.map(c -> new DataY(dataX.getA(), UUID.randomUUID().toString(), c))
.collect(Collectors.toList());
System.out.println("collect = " + collect);
}
private static List<String> operationOneToMany(String input) {
if ("c3".equals(input))
return Arrays.asList("c31", "c32", "c33");
else if ("c5".equals(input))
return Arrays.asList("c51", "c52");
else
return Arrays.asList(input);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论