java中是否有一个构造可以做这样的事情(这里是用python实现的):
[] = [item for item in oldList if item.getInt() > 5]
今天我正在使用类似的东西:
ItemType newList = new ArrayList();
for( ItemType item : oldList ) {
if( item.getInt > 5) {
newList.add(item);
}
}
对我来说,第一种方式看起来更聪明一些。
Java 7 可能或可能不实现闭包,因此支持这样的功能,但目前不支持,因此在Java VM上,您可以选择在Groovy、Scala或Clojure中执行此操作(也可能是其他),但在 java 中,你只能通过使用像 Guava 的 Collections2.filter().
这样的帮助器来接近这一点。JDK 7 示例代码:
findItemsLargerThan(List<Integer> l, int what){
return filter(boolean(Integer x) { x > what }, l);
}
findItemsLargerThan(Arrays.asList(1,2,5,6,9), 5)
Groovy 示例代码:
Arrays.asList(1,2,5,6,9).findAll{ it > 5}
番石榴示例代码:
Collections2.filter(Arrays.asList(1, 2, 5, 6, 9),
new Predicate<Integer>(){
@Override
public boolean apply(final Integer input){
return input.intValue() > 5;
}
}
);
Scala 示例代码(感谢 Bolo):
Array(1, 2, 5, 6, 9) filter (x => x > 5)
没有什么是不可能的(-:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListCleaner {
public static void main(String[] args) {
final List<Integer> oldList = Arrays.asList(new Integer[] { 23, 4, 5,
657 });
System.out.println(oldList);
List<Integer> newList = new ArrayList<Integer>() {
{
for (Integer element : oldList) {
if (element > 5) {
this.add(element);
}
}
}
};
System.out.println(newList);
}
}
唯一的限制是 oldList 必须是最终的。
纯Java就可以完成,但是需要写一个支持类Filter,下面的代码才能成功运行:
List<Integer> oldList = Arrays.asList(new Integer[] { 1, 2, 5, 6, 9 });
List<Integer> newList = new Filter<Integer>(oldList) {
{
findAll(it > 5);
}
}.values();
System.out.println(newList); // [6, 9]
如果您想知道为什么此代码会编译,请查看Java 的隐藏功能:双括号初始化。这将创建 Filter 类的匿名实例,其中包含 it 变量并提供 findAll() 方法。
Filter 类本身有一个缺点,即为每个列表元素创建一个新实例来评估 findAll() 中的布尔条件:
public abstract class Filter<T> {
protected List<T> values = new ArrayList<T>();
protected T it;
public Filter(List<T> values) {
if (values != null) {
this.values.addAll(values);
}
if (values.isEmpty()) {
throw new RuntimeException("Not for empty collections!");
}
it = values.iterator().next();
// instance initializer gets executed here, calls findAll
}
protected void findAll(boolean b) throws Throwable {
// exit condition for future calls
if (values.size() > 1) {
// only repeat for each entry, if values has multiple entries
Iterator<T> iterator = values.iterator();
while (iterator.hasNext()) {
// don't evalute again for the first entry
if (!b) {
iterator.next();
iterator.remove();
b = true;
} else {
// for each other entry create an argument with one element
List<T> next = new ArrayList<T>();
next.add(iterator.next());
// get constructor of anonymous class
Constructor<?> constructor = this.getClass().getDeclaredConstructors()[0];
// invoke constructor and thus execute instance initializer again
Filter<T> filtered = (Filter<T>) constructor.newInstance(new Object[] { null, next });
// if values is empty, the condition didn't match and the element can be removed
if (filtered.values.isEmpty()) {
iterator.remove();
}
}
}
} else {
// one element can be checked directly
if (!b) {
values.clear();
}
}
}
public List<T> values() {
return values;
}
}
但是,由于如今实例创建相当便宜,并且 Filter 类可用于所有对象,因此可能值得将其包含在 Utils 包中。
您好, 哈德
不,Java 还不支持这种动态语言结构 :-) 所以你必须接受你的选择 2
我相信 Java 8 Stream Api 可以节省一些代码行:
List<ItemType> newList =
oldList.stream().filter(item -> item.getInt() > 5).collect(Collectors.toList());