假设我有3辆汽车,每辆汽车有3个发动机,每个发动机有3个零件.如何map of <engine_model, List<parts>>使用Java8流创建一个?
在常规Java中,我会执行以下操作:
Map<String, List<Parts>> map = new HashMap<String, List<Parts>>();
for (Car car: cars){
for (Engine engine: car.getEngines()){
if (!map.contains(engine.getModel())){
map.put(engine.getModel(), new ArrayList<Part>());
}
for (Part part: engine.getParts()){
map.get(engine.getModel()).add(part);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这map是一个map将引擎的model(type String)映射到与该模型相关的所有部件的列表.不同的车可以有相同的发动机型号.
我试着做以下事情:
carList.stream().flatMap(p -> p.getEngines().stream())
.collect(Collectors.groupingBy(p -> p.getModel(),
Collectors.mapping(Engine::getParts, Collectors.toList())))
Run Code Online (Sandbox Code Playgroud)
问题是上面的使用我得到了
Map<String, List<List<Part>>>
我想要的
Map<String, List<Part>>
我想一个等效的问题是:我怎么改变List of Lists,只是List在上面的例子中的情况下?
我的问题涉及Java 5中引入的Java foreach循环,以及Iterable.forEach()Java 8中引入的.
假设存在一个带有List of String值的程序,该程序的功能是迭代每个单独的字符串并将其用作方法参数.
从Java 5开始,这样的程序看起来像这样:
for (String s : stringList) {
doSomething(s);
}
Run Code Online (Sandbox Code Playgroud)
但是,随着Iterable.forEach()Java 8 的引入,这样的程序看起来像这样:
stringList.forEach(this::doSomething);
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有理由使用Iterable.forEach()foreach循环而不是保存几行?
我正在尝试在Java 8中使用lambda表达式.我想写的表达式的类型是:
BiConsumer<String, BiConsumer<Boolean,Integer>> cc2;
Run Code Online (Sandbox Code Playgroud)
我应该能够写一个看起来像这样的表达式:
cc2 = (s,(b,i)) -> { System.out.println(s+b+i); }; // this does not compile
Run Code Online (Sandbox Code Playgroud)
不幸的是我找不到我必须使用的确切语法.
我已经在我的ubuntu 16.04上安装了JDK 1.8,因为我必须安装JDK 1.7.我安装了它而没有对我的旧版本进行任何操作,并将$ JAVA_HOME更改为指向新安装的位置.
echo $JAVA_HOME工作正常,显示1.7的新安装版本
但是当我这样做$ java -version时显示早期安装的版本是1.8.
为什么会这样?如何解决它.
我需要1.7进一步使用它.
如果我在某个地方出错了请纠正我,我需要对此有好的解释.
谢谢.这是我的终端

我的问题基本上归结为将a减少List到链表,但是reduce函数的推断类型似乎不对.
我的清单看起来像这样
[0, 1, 2]
Run Code Online (Sandbox Code Playgroud)
我希望reduce函数在每个reduce步骤中执行此操作
null // identity (a Node)
Node(0, null) // Node a = null, int b = 0
Node(1, Node(0, null)) // Node a = Node(0, null), int b = 1
Node(2, Node(1, Node(0, null))) // Node a = Node(1, Node(0, null)), int b = 2
Run Code Online (Sandbox Code Playgroud)
但是,reduce函数似乎认为这不起作用,因为我猜它不认为标识是一个节点.
这是我的代码.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Example {
static class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = …Run Code Online (Sandbox Code Playgroud) 我在下面的代码中有一个功能接口,它有一个抽象方法和一个对象方法覆盖.因此,当我为此编写Lambda表达式时,如何实现我的equals方法.
import static java.lang.System.out;
public class Test {
public static void main(String[] args) {
AddToString test = a -> (a + " End");
out.println(test.stringManipulation("some string"));
out.println(test.increment(5));
out.println(test.equals(null));
}
}
@FunctionalInterface
interface AddToString {
String stringManipulation(String a);
default int increment(int a) { return a+1; }
@Override
public boolean equals(Object obj);
}
Run Code Online (Sandbox Code Playgroud)
一种方法是创建Anonymous类,如下所示,但有一个更好的方法使用lambda表达式 -
public class Test {
public static void main(String[] args) {
AddToString test = new AddToString() {
public String stringManipulation(String a) {
return a + " End";
}
@Override …Run Code Online (Sandbox Code Playgroud) 当我们写下面的代码
Stream.of(1,2,3,4,5).filter(i -> (i%2 == 0)).map( i -> i*i );
Run Code Online (Sandbox Code Playgroud)
表达式i -> (i%2 == 0)或i -> i*i将变成私有方法.
在我的用例中,编写了一个junit测试以确保没有方法是私有的(是啊,那是强制的),并且这些lambda表达式失败了.
有人可以提出一些建议,其中我不必更改junit来为lambda表达式添加一些排除,但是让这些表达式在内部创建受保护的方法吗?
流式传输时List,如何将输出收集到链表?
我尝试过以下方法:
public static void main(String[] args) {
List<String> firstList = new ArrayList<>();
firstList.add("pavan");
firstList.add("kumar");
LinkedList<String> filtered= new LinkedList<>();
filtered = (LinkedList<String>) firstList.stream().filter(t->firstList.contains("p")).collect(Collectors.toList());
System.out.println(filtered);
}
Run Code Online (Sandbox Code Playgroud)
但 这是让java.util.ArrayList无法强制转换为java.util.LinkedList.
我有一个数据描述界面:
public interface DataDescription {
int getId();
}
Run Code Online (Sandbox Code Playgroud)
两个实现:
public class DataWithId1 implements DataDescription {
// ... simple getter impl.
}
public class OtherDataWithId implements DataDescription {
// ... simple getter impl.
}
Run Code Online (Sandbox Code Playgroud)
现在我有这个界面:
public interface DataProcessor {
void process(DataDescription data);
}
Run Code Online (Sandbox Code Playgroud)
我想实现DataProcessor一个类,如下所示:
public class DataProcessorImpl implements DataProcessor {
@Override
public void process(DataDescription data) {
// Do something...
}
public void process(DataWithId1 data) {
// Do something specific with this type (e.g. directly store in table of database) …Run Code Online (Sandbox Code Playgroud) 我尝试使用代码在Kotlin中使用javax.time:
import javax.time.calendar.LocalDate
fun main(args: Array<String>){
println("Today is ${LocalDate.now()}");
}
Run Code Online (Sandbox Code Playgroud)
并得到编译错误:
C:\kotlin-hello-world>kotlinc hello.kt -include-runtime -d hello.jar
hello.kt:1:14: error: unresolved reference: time
import javax.time.calendar.LocalDate
^
Run Code Online (Sandbox Code Playgroud)
而谷歌搜索结果显示,在科特林javax.time不能使用,人们推荐使用ThreeTenBP与ThreeTenABP如果机器人.我的用例是在Kotlin的服务器端使用javax.time类.
但是当我尝试使用TreeTenBP时,我必须导入org.threeten.bp.LocalDate而不是javax.time.calendar.LocalDateTreeTen*不是JSR-310的实现而是后端口.
除了那些backport库之外,还有什么方法可以在Kotlin中使用javax.time吗?如果是这样,如何配置这样的环境或如何编写这样的代码?
java-8 ×10
java ×9
lambda ×3
java-stream ×2
collectors ×1
dictionary ×1
foreach ×1
interface ×1
iterator ×1
java-7 ×1
jodatime ×1
jsr310 ×1
kotlin ×1
linked-list ×1
overloading ×1
protected ×1
reduce ×1