【发布时间】:2013-01-14 12:05:51
【问题描述】:
我正在使用Lists.transform 方法。在此我有一个字符串输入和一个 CustomObject 输出。字符串输入应包含一个冒号。然后我在这个冒号上拆分,然后从字符串的两个部分创建自定义对象。
所以输入是
a:b
c:d
e:f
输出是三个CustomObject,包含a, bc, d和e, f
我的代码是这样的
return new ArrayList(Lists.transform(groups, new Function<String, CustomObject>() {
@Override
public CustomObject apply(String input) {
String[] split = input.split(":");
String one = split[0];
String two = split[1];
return new CustomObject(one, two);
}
}));
我的问题是我收到的列表来自用户。如果我输入错误,我想跳过列表中的那个项目
所以如果输入列表包含
a:b
d
e,f
g:h
那么我想要两个包含a:b 和g:h 的CustomObject
这在番石榴中可行吗?
如果我以冗长的方式执行此操作,我只会在 for 循环中继续并记录错误。
例如作为伪代码
for loop {
if no colon present
continue;
create CustomObject
}
谢谢
【问题讨论】:
-
我认为你可以使用旧的 for 循环。
标签: java collections exception-handling guava