我正在尝试用lambda制作一个过滤器.有一个字符串列表,其中包含应该排序的值,还有另一个列表,其中包含不同的值(即应该过滤的值).所以我一直想做的是:
stringList = stringList.stream()
.filter(e ->toBeSortedOutList.forEach(outSorted->!e.startsWith(outSorted)))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
不幸的是,我总是得到错误,布尔值无法转换为void.所以我想知道的是,为什么我得到这个错误,如果可以通过迭代通过列表并使用其值来过滤器进行过滤?谢谢你的回答!
我将如何使用Java 8流重写它?我还不熟悉Java 8的功能.
private final ArrayList<Double> ledger = new ArrayList<>();
/**
*
* @return the ledger as a String (e.g. "1.20|2.50|1.90")
*/
public String getLedgerString() {
StringJoiner sj = new StringJoiner("|","[","]");
for (double d : ledger)
sj.add(String.format("%.2f", d));
return sj.toString();
}
Run Code Online (Sandbox Code Playgroud) 在处理转换时(例如:transform List<People> peopleto List<Integer> ages,where People是包含属性的类age),通常有两种方法(对我而言):
使用java8:
people.stream().map(p -> p.getAge()).collect(toList());
或用户guava2:
Lists2.transform(people, People2AgeTransformer.INSTANCE);其中People2AgeTransformer是一个变换器,它实现了Function接口以返回年龄.
最近,我偶然发现java8和guava可以合并,因此代码可以是:
Lists2.transform(people, p->p.getAge());
Run Code Online (Sandbox Code Playgroud)
这段代码符合并运行没有任何错误,这让我感到困惑.该方法Lists2.transform()要求第二个args是接口的实现com.google.common.base.Function,而java8 lambda实际上是接口的实现java.util.function.Function.(好吧,它们都声明了一个B apply(A input)将A转换为B 的方法.)
我不明白为什么这会起作用,因为它们是不同包的两个不同界面.
我试图创建两个列表 - 赔率和均衡如下:
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 5, 8, 13, 21));
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
numbers.stream().forEach(x -> x % 2 == 0 ? evens.add(x) : odds.add(x));
}
Run Code Online (Sandbox Code Playgroud)
但它给了我不兼容的类型错误(lambda表达式缺少返回值的错误返回类型)
将集合过滤到两个新集合的最佳方法是什么?
如何使用Java 8 lambda/stream()以更直观的方式在函数式编程中编写此方法?
int animation = R.style.DialogAnimationSlideHorizontal;
String body;
String subject = mSubjectView.getText().toString();
BaseDialog dialog = dialogFactory.getType(DialogTypes.DIALOG_FULL);
if (!checkFields()) {
// one of the recipients is invalid.
body = getString(R.string.bad_address);
dialog.showDialog(body, animation, new DialogBuilder.Positive() {
@Override
public void handleClick(DialogInterface dialogInterface, View view) {
// do nothing
}
});
} else if (Helpers.isEmpty(subject)) {
// Yup, empty... send the message without a subject?
body = getString(R.string.empty_subject_compose);
dialog.showDialog(body, animation, new DialogBuilder.Positive() {
@Override
public void handleClick(DialogInterface dialogInterface, View view) {
// user …Run Code Online (Sandbox Code Playgroud) 我有一个Map流我怎么能得到具有Map值的Set?
我在这里部分地做了什么
Set<String> jcfTargetTables = measure.getConditionMap().values()
.stream()
.map(Condition::getJoinConditionFilter)
.filter(jcf -> jcf!=null)
.map(JoinConditionFilter::getTableMapping);
Run Code Online (Sandbox Code Playgroud)
最后一行给了我一个Stream<Map<String,String>>,我如何继续获取Set是Map的值?
我试图了解java 8 stream中的不同peek和map.我尝试了以下内容
public static void main(String[] args) {
List<String> arr = new ArrayList<String>();
arr.add("A");
arr.add("B");
List<String> a = arr.stream().peek(t->t.toLowerCase()).collect(Collectors.toList());
System.out.println(a);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有将字母改为小写.但是当我尝试以下内容时
public static void main(String[] args) {
List<String> arr = new ArrayList<String>();
arr.add("A");
arr.add("B");
List<String> a = arr.stream().map(t->t.toLowerCase()).collect(Collectors.toList());
System.out.println(a);
}
Run Code Online (Sandbox Code Playgroud)
字母表被转换为更小的情况.我怀疑这里是否使用地图和下面的偷看
public static void main(String[] args) {
List<String> arr = new ArrayList<String>();
arr.add("A");
arr.add("B");
List<String> a = arr.stream().map(t->t.toLowerCase()).peek(t->toUpper()).collect(Collectors.toList());
System.out.println(a);
}
public static Function<String, String> toUpper(){
return t->{
return t.toUpperCase();
};
}
Run Code Online (Sandbox Code Playgroud)
map方法将A,B转换为较低而Peek不执行任何操作.如果在流式传输时涉及到任何计算,我可以使用peek?有人可以解释吗?
MOdified代码
static List<Employee> e = new …Run Code Online (Sandbox Code Playgroud) 有没有办法使用一些谓词和java流过滤内部和外部列表?例如:
Class outerObject{
int num;
List<innerObject> innerObjectList;
}
Class innerObject{
int num;
}
List <outerObject> outerObjectList
Run Code Online (Sandbox Code Playgroud)
有没有办法过滤列表,使outerObject列表有字段num = 10,相应字段的innerObject's 列表outerObject也被相应的字段过滤num = 10?
我试过了:
outerObjectList.stream()
.filter(i -> i.getNum() == 10)
.filter(i -> i.getOrders().stream().anyMatch(s -> getNum() == 10))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但它似乎没有过滤内部列表.
描述性示例:
如果外部列表中有3个对象,num = 10并且3个对象中的每个对象都有1个内部对象innerObjectList,num = 10那么它应该给我一个3 outerObject的列表,innerObject每个列表为1 .
我有一个从文件中读取的以下线路流
1 2 4.5
1 6 3 5.5 5.3 6
1 7.2 5 7
如何只考虑整数,我如何在单个列表中收集这些行?(请注意,在每一行中,数字由一个或多个空格分隔)
这是我尝试过的,但这给了我一个所有整数元素的列表.
list = reader.lines()
.map(m -> m.split("\\n"))
.flatMap(Arrays::stream)
.map(m -> m.split("\\s+"))
.flatMap(Arrays::stream)
.filter(f -> !f.contains("."))
.map(Integer::parseInt)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我正在使用一些Java 8 StreamAPI.我很困惑看到以下两个解决方案之间的性能差异,即只打印内容Stream.
解决方案1:
int[] array = new int[] { 0, 1, 2, 3, 4, 5 };
start = System.nanoTime();
Arrays.stream(array).forEach(System.out::println);
System.out.println((System.nanoTime() - start) / 1000000.0f);
Run Code Online (Sandbox Code Playgroud)
解决方案2:
int[] array = new int[] { 0, 1, 2, 3, 4, 5 };
start = System.nanoTime();
Arrays.stream(array).forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
System.out.println((System.nanoTime() - start) / 1000000.0f);
Run Code Online (Sandbox Code Playgroud)
执行时,Solution 1约需要 比时间长5-6倍Solution 2.
系统配置:
1.8.0_101 64 bitWindows 10 Home …