让我们假设我有一个对象:
class MyObject {
private int id;
private HashMap<Integer, OtherObject> otherObjects;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是访问otherObjects列表的属性MyObject并将它们全部添加到a otherObjects list.
我可以使用.forEach和.addAll进入,otherObjects list但我正在尝试看看是否有可能使用lambdas来实现这一点.我想到了类似的东西,但它似乎不起作用:
myObjectList.stream()
.map(o -> o.getOtherObjects())
.map(oo -> oo.values())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但似乎与对象类型存在冲突.我猜这是因为我从一个对象流开始,最后得到一个列表流,它会混淆.我怎样才能做到这一点?更一般地说,如何将许多父对象的对象列表收集到一个列表中?
我试图使用lambda表达式将String数组转换为Integer数组.
我在下面提供了我的代码,以及到目前为止我所尝试的内容的简要说明:
String [] inputData = userInput.split("\\s+");
Integer [] toSort = new Integer [] {};
try {
toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseInt).toArray();
}catch(NumberFormatException e) {
System.out.println("Error. Invalid input!\n" + e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
我上面的lamba表达式是一个映射到int数组的表达式,这不是我想要的,在编译此代码时我收到以下错误消息:
BubbleSort.java:13: error: incompatible types: int[] cannot be converted to Integer[]
toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseIn
t).toArray();
^
1 error
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法允许我使用lambdas或其他方法从String数组到Integer数组?
import org.junit.Test;
import java.util.stream.IntStream;
public class GomanTest {
@Test
public void someTest() {
IntStream.of(2, 3, 1).collect(Container::new, Container::add, null);
}
}
class Container<T> {
void add(T t) {
System.out.println("this is container " + t);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
this is container 2
this is container 3
this is container 1
Run Code Online (Sandbox Code Playgroud)
这在1.8.0_45.jdk上成功运行.Container#add如何被翻译成ObjIntConsumer #accept?
这可以通过Stream | Map完成,这样我就不需要将结果放在外部HashMap中,而是使用.collect(Collectors.toMap(...))收集结果; ?
Map<ReportType, Long> rep = new HashMap<>(); // result is here
Arrays.stream(rTypes).forEach(r -> rep.put(r.reportType, r.calcValue()));
Run Code Online (Sandbox Code Playgroud)
在哪里r.calcValue()计算新结果,然后放在地图中.
我正在学习Java 8,我正在尝试使用lambdas和泛型,我写了这个小例子
import java.util.function.*;
public class LambdaTest<T> {
public T calculate(T x, T y, BiFunction<T, T, T> func) {
return func.apply(x, y);
}
public static void main(String args[]) {
LambdaTest<Integer> l = new LambdaTest<Integer>();
System.out.println("" + l.calculate(10, 10, (x, y) -> x + y));
System.out.println("" + l.calculate(10, 10, (x, y) -> x * y));
System.out.println("" + l.calculate(10, 10, (x, y) -> x / y));
System.out.println("" + l.calculate(10, 10, (x, y) -> x - y));
LambdaTest<Double> l2 = new LambdaTest<Double>();
System.out.println("" …Run Code Online (Sandbox Code Playgroud) 我目前正在学习Java中的Lambda表达式.根据我的理解,它是一个代码块,我们可以传递,以便以后执行.但是当我们想要稍后执行代码时,我无法想到示例.
这有用的各种场景是什么?Lambda Expression和Functional接口之间的联系是什么?
我有一个人的域名
public class Person{
String name;
}
Run Code Online (Sandbox Code Playgroud)
我有一个列表的人名字符串,如
listRange = new ListWith("Bob","John","Mark");
Run Code Online (Sandbox Code Playgroud)
我如何使用Java 8流在resultList中过滤,哪个人在此范围列表中具有名称.可能就像
resultList.stream().filter(p->p.getName().equals(listRange.any).collect();
Run Code Online (Sandbox Code Playgroud)
如何使用Lambda过滤resultList?
我有一个ms的时间戳,并使用SimpleDateFormater格式化,如下所示:
SimpleDateFormat sdfDate = new SimpleDateFormat("MM/d/yyyy h:mm a");
return sdfDate.format(new Date(timeStamp));
Run Code Online (Sandbox Code Playgroud)
现在我想改变它以使用新的Java 8功能.好像我真的找不到一种方法来使用新的Java 8类.有人有提示吗?
通读这篇文章,我可以像这样过滤集合:
Set<Status> statusClone = cloner.deepClone(statusList).stream().filter(s -> !(s instanceof FileMetadata)).collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
但是,我还需要设置属性同时过滤它们.过滤后,我目前遍历每个,然后设置属性:
for (Iterator<Status> iterator = statusClone.iterator(); iterator.hasNext();)
{
Status s = iterator.next();
// if (s instanceof FileMetadata)
// {
// iterator.remove();
// continue;
// }
s.setStatus(JobStatus.UNINITIATED);
s.setLastAccessedTime(0);
s.setOffSet(null);
s.setStreamNo(null);
}
statusList.addAll(statusClone);
Run Code Online (Sandbox Code Playgroud)
如果不使用foreach,这在Java8中是否可行?
编辑:从评论,我同意我可以在过滤器内克隆.谢谢.
我正在学习java 8流,有些问题对我而言.
假设这段代码:
new Random().ints().forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
它在某种程度上内部称IntPipeline,我认为有责任无限期地生成这些内容.通过查看java源代码很难理解Streams实现.
您能否给出一个简短的解释,或者给出一些关于如何生成流以及如何连接管道操作的好/易理解的材料.整数上面的代码示例是随机生成的,如何建立这种连接?
java-8 ×10
java ×9
java-stream ×5
lambda ×5
arrays ×1
collections ×1
datetime ×1
dictionary ×1
java-time ×1