是否有最简单的方法可以在下面编写此代码而不使用toStream()?
import io.vavr.collection.List;
import io.vavr.control.Option;
import lombok.Value;
public class VavrDemo {
public static void main(String[] args) {
Foo bar = new Foo(List.of(new Bar(1), new Bar(2)));
Number value = Option.some(bar)
.toStream() // <- WTF?!?
.flatMap(Foo::getBars)
.map(Bar::getValue)
.sum();
System.out.println(value);
}
@Value
static class Foo {
private List<Bar> bars;
}
@Value
static class Bar {
private int value;
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个名为Student的对象列表.对象学生的定义是这样的
public Class Student {
private String studentName;
private String courseTaking;
}
Run Code Online (Sandbox Code Playgroud)
在学生列表中,可以有多个学生对象具有相同的studentName但不同的courseTaking.现在我想将学生列表转换为studentName和courseTaking的地图
Map<String, Set<String>>
Run Code Online (Sandbox Code Playgroud)
关键是studentName,并且该值是同一个学生作为一组放在一起的所有课程.我怎么能用stream()和collect()做到这一点?
我必须使用Streams API从给定文件中找到所有最长的单词.我做了它在几个步骤,但寻找一些"一个班轮",其实我处理整个文件两次,第一次找字和第二的最大长度为所有比较的最大长度,假设它不是最好的表现; P有人能帮帮我吗?看看代码:
public class Test {
public static void main(String[] args) throws IOException {
List<String> words = Files.readAllLines(Paths.get("alice.txt"));
OptionalInt longestWordLength = words.stream().mapToInt(String::length).max();
Map<Integer, List<String>> groupedByLength = words.stream().collect(Collectors.groupingBy(String::length));
List<String> result = groupedByLength.get(longestWordLength.getAsInt());
}
}
Run Code Online (Sandbox Code Playgroud)
我想直截了当:
List<String> words = Files.readAllLines(Paths.get("alice.txt"));
List<String> result = // code
Run Code Online (Sandbox Code Playgroud)
文件每行只包含一个单词,无论如何它并不重要 - 问题是关于正确的流代码.
我有一个Map对象Map<t1, Set<t2>>,我想进入集合并将t2集合转换为新地图的键.原始键t1将是地图的新值.
例如,给定一个包含两个条目的地图
{key1: [a, b, c], key2: [c, d]}
Run Code Online (Sandbox Code Playgroud)
得到的地图将是
{a: [key1], b: [key1], c: [key1, key2], d: [key2]}
Run Code Online (Sandbox Code Playgroud)
[]表示在上面的例子中设置.
我试图了解Java 8 Stream功能.
我想在特定目录路径的所有子目录中捕获数组中的所有文件(也可能是列表).
我的代码抛出了java.lang.ArrayStoreException错误
Run Code Online (Sandbox Code Playgroud)File[] files = Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .toArray(File[]::new);
我尝试的另一件事是将文件名添加到现有的ArrayList:
Run Code Online (Sandbox Code Playgroud)ArrayList<String> existingNames = new ArrayList<String>(); Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .forEach(p -> existingNames.add(p.getFileName()));
这也会抛出java.lang.ArrayStoreException错误.
我用同样的结果尝试的其他东西是:
Run Code Online (Sandbox Code Playgroud)File[] files = Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .toArray(File[]::new);
谁能指出我正确的方向?
完整的堆栈跟踪是:
java.util.stream.Nodes的java.util.stream.SpinedBuffer.copyInto(SpinedBuffer.java:198)java.lang.System.arraycopy(Native Method)中的线程"main"java.lang.ArrayStoreException中的异常$ SpinedNodeBuilder .copyInto(Nodes.java:1290)位于java.util.stream上的java.util.stream.R井(SpinedBuffer.java:215)和java.util的$ SpinedNodeBuilder.asArray(Nodes.java:1296).在nachbearbeitung.CheckExistingMAIDs.main(CheckExistingMAIDs.java:41)的stream.ReferencePipeline.toArray(ReferencePipeline.java:439)
提前致谢!
学生和课程有两个普通对象,如下所示:
public class Student {
List<Course> courses;
...
}
public class Course {
String name;
...
}
Run Code Online (Sandbox Code Playgroud)
如果我们有一个list的Students,我们怎么能由他们的课程名称进行筛选一些学生的?
flatMap回答这个问题,但它返回课程对象而不是学生对象.allMatch(以下代码).但它会返回学生列表,但List始终为空.问题是什么?List<Student> studentList;
List<Student> AlgorithmsCourserStudentList = studentList.stream().
filter(a -> a.stream().allMatch(c -> c.getCourseName.equal("Algorithms"))).
collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) Map<A, List<B>> xFunction() {
Map<A, List<B>> mapList = new HashMap<>();
List<A> aList = x.getAList();
for (A a : aList) {
List<B> bList = getBList(a.getId());
mapList.put(a, bList);
}
return mapList;
}
Run Code Online (Sandbox Code Playgroud)
如何在java 8中使用collect和grouping或映射转换它?
我试着用这样的东西:
x.getAList
.stream()
.map(a -> getBList(a.getId)) //return a list of B
.collect(Collectors.groupingBy (...) )
Run Code Online (Sandbox Code Playgroud)
干杯
如果有人能解释为什么我不能使用Java中的Stream API来比较数组中的String,我将很高兴.
我试图在第一个单词按字典顺序排在第二个单词之前仅过滤对.
String[] input = { "Apple", "Banana" };
Arrays.stream(input)
.filter( (a,b)-> a.compareTo(b)< 0 )
Run Code Online (Sandbox Code Playgroud)
看来Java不明白,"b"是一个字符串,但为什么呢?
我不明白为什么code1工作但code2不编译.请解释.
//Code1:
Stream<String> s = Stream.of("AA", "BB");
s.sorted(Comparator.reverseOrder())
.forEach(System.out::print);
//Code2:
Stream<String> s = Stream.of("AA", "BB");
s.sorted(Comparator::reverseOrder)
.forEach(System.out::print);
Run Code Online (Sandbox Code Playgroud)
两者之间的区别是code2使用Comparator.reverseOrder()时code1使用Comparator::reverseOrder
我有一个CarPOJO:
class Car
{
String make;
int year;
BigDecimal amount;
}
Run Code Online (Sandbox Code Playgroud)
和那样List<Car>的汽车:
make : year : price
honda : 2011 : $20,000
honda : 2011 : $30,000
honda : 2012 : $50,000
ford : 2012 : $12,000
Run Code Online (Sandbox Code Playgroud)
结果我想要的地方:
// Map<make, List<year, totalForYear>>
Map<String, Map<int, BigDecimal>> revenuesByMakeAndYear;
Run Code Online (Sandbox Code Playgroud)
如何使用Streams实现这一目标?到目前为止,我已经到了一半:
// Map<make, List<car>>
Map<String, List<Car>> carsByMake = cars.stream()
.collect(Collectors.toMap(
car -> car.getMake(),
car -> cars.stream()
.filter(subCar -> subCar.getMake().equals(car.getMake())
.collect(Collectors.toList())));
Run Code Online (Sandbox Code Playgroud)
但是我该如何进入下一步呢?
java-stream ×10
java ×9
java-8 ×7
lambda ×2
collections ×1
compare ×1
filter ×1
string ×1
vavr ×1