我经常使用 JavaScript,并发现 underscorejs 在操作数据集(如数组或对象)方面非常方便。
我对 Java 很陌生,想知道是否有类似的库?
我对 Java 很陌生,想知道是否有类似的库?
var words = ["Gallinule", "Escambio", "Aciform", "Entortilation", "Extensibility"];
var sum = _(words)
.filter(function(w){return w[0] == "E"})
.map(function(w){return w.length})
.reduce(function(acc, curr){return acc + curr});
alert("Sum of letters in words starting with E... " + sum);
String[] words = {"Gallinule", "Escambio", "Aciform", "Entortilation", "Extensibility"};
int sum = Arrays.stream(words)
.filter(w -> w.startsWith("E"))
.mapToInt(w -> w.length())
.sum();
System.out.println("Sum of letters in words starting with E... " + sum);
有一个叫做underscore-java的库。示例
import com.github.underscore.U;
public class Main {
public static void main(String args[]) {
String[] words = {"Gallinule", "Escambio", "Aciform", "Entortilation", "Extensibility"};
Number sum = U.chain(words)
.filter(w -> w.startsWith("E"))
.map(w -> w.length())
.sum().item();
System.out.println("Sum of letters in words starting with E... " + sum);
}
}
// Sum of letters in words starting with E... 34