这是一个简单的类,说明了我的问题:
package com.example;
import java.util.function.*;
public class App {
public static void main(String[] args) {
App a1 = new App();
BiFunction<App, Long, Long> f1 = App::m1;
BiFunction<App, Long, Void> f2 = App::m2;
f1.apply(a1, 6L);
f2.apply(a1, 6L);
}
private long m1(long x) {
return x;
}
private void m2(long x) {
}
}
Run Code Online (Sandbox Code Playgroud)
f1,指的是App::m1,和被绑定到a1中f1的来电apply,工作完全正常-编译器是幸福的呼叫可以通过f1.apply就好制成.
f2,指App::m2,不起作用.
我希望能够定义一个没有返回类型的未绑定非静态方法的方法引用,但我似乎无法使其工作.
我试图从一个长变量中获取第二个数字.
long mi = 110000000;
int firstDigit = 0;
String numStr = Long.toString(mi);
for (int i = 0; i < numStr.length(); i++) {
System.out.println("" + i + " " + numStr.charAt(i));
firstDigit = numStr.charAt(1);
}
Run Code Online (Sandbox Code Playgroud)
当我firstDigit = numStr.charAt(1)在控制台上打印时.我得到1预计但当循环结束firstDigit了49.有点困惑为什么.
我有一个名为"Book"的数据结构,它包含以下字段:
public final class Book {
private final String title;
private final BookType bookType;
private final List<Author> authors;
}
Run Code Online (Sandbox Code Playgroud)
我的目标是Map<Author, List<BookType>>从List<Book>使用Stream API 派生出来.
为了实现它,我首先制作了一个for-each循环来阐明解决方案的步骤,并在我将其重新编写为基于流的方法后逐步完成:
Map<Author, List<BookType>> authorListBookType = new HashMap<>();
books.stream().forEach(b -> b.getAuthors().stream().forEach(e -> {
if (authorListBookType.containsKey(e)) {
authorListBookType.get(e).add(b.getBookType());
} else {
authorListBookType.put(e, new ArrayList<>(Collections.singletonList(b.getBookType())));
}
}));
Run Code Online (Sandbox Code Playgroud)
但它不是基于Stream API的解决方案,而且我已陷入困境,我不知道如何正确完成它.我知道我必须使用分组收集器Map<Author, List<BookType>>从流中获取所需的直接.
你能给我一些提示吗?
我是Java 8的新手,希望了解这两种方案之间的区别.我知道一旦流被操作和消耗,然后流不能再次重用它将产生错误.
情景1:
List<String> title = Arrays.asList("Java8", "In", "Action");
Stream<String> s = title.stream();
s.forEach(System.out::println);
s.forEach(System.out::println); // THIS WILL GIVE ERROR - streams has been already operated and closed.
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到以下错误...这是公平的.
Java8
In
Action
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.sourceStageSpliterator(Unknown Source)
at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
at com.test.Java8InAction.CH4.TraversableOnlyOnce.main(TraversableOnlyOnce.java:12)
Run Code Online (Sandbox Code Playgroud)
情景2:
// Filtering unique elements
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
numbers.stream().forEach(System.out::println);
numbers.stream().filter(n -> n % 2 == 0).distinct().forEach(System.out::println);
numbers.stream().filter(n -> n % …Run Code Online (Sandbox Code Playgroud) 我有一个对象,
class Object2{
String name;
String id;
Map<String, String> customData;
}
class Object1{
List<Object2> obj1List;
}
Run Code Online (Sandbox Code Playgroud)
我想将object1列表中的这个customData Map转换成一个单独的map,如果键已经存在,我可以覆盖这些值.
所以情况就是这样:我需要在某些日期登记人们的投票.简而言之,提出了一个日期,人们投票支持他们想要的日期.
数据结构如下:
private HashMap<LocalDateTime, Set<Vote>> votes;
Run Code Online (Sandbox Code Playgroud)
投票是:
public class Vote {
private String name;
private VoteType vote;
public Vote(String name, VoteType vote) {
super();
this.name = name;
this.vote = vote;
}
}
Run Code Online (Sandbox Code Playgroud)
其中VoteType只是一个枚举:
public enum VoteType {YES, NO, MAYBE}
Run Code Online (Sandbox Code Playgroud)
现在我已经创建了一个返回可用性投票数量的流(VoteType):
public Map<LocalDateTime, Integer> voteCount(VoteType targetVote) {
return this.votes.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new Integer(
e.getValue().stream().filter(v -> v.getVote() == targetVote).collect(Collectors.toList()).size())));
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:如何使用Java Streams获得最多"YES"的日期.
/* Returns the date that got the most 'YES' votes */
public LocalDateTime winningDate() {
// TODO
}
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
请原谅我,如果解决方案非常明显,但我似乎无法弄清楚如何做到这一点
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("b1", "a1");
map.put("b2", "a2");
map.put("b3", "a1");
Map<String, List<String>> mm = map.values().stream().collect(Collectors.groupingBy(m -> m));
System.out.println(mm);
}
Run Code Online (Sandbox Code Playgroud)
我想根据hashmap中的值进行分组.我想要输出,{a1=[b1, b3], a2=[b2]}但它现在正在出现{a1=[a1, a1], a2=[a2]}
我对溪流很新,所以请帮帮我(并且温柔).
我想做的是以下内容.我有一个BufferedReader从一个文件读取,其中每行看起来像这样:"a,b".例如:
示例输入文件
"a,b"
"d,e"
"f,g"
我想将其转换为LinkedList<String[]>:
例 LinkedList<String[]>
[{"a","b"},{"c","d"},{"f","g"}]
你会如何使用流方法?
这是我试过的:
List numbers = reader.lines().map(s -> s.split("[\\W]")).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这不起作用.我的IDE提供以下反馈:
Incompatible types. Required List but 'collect' was inferred to R: no instance(s) of type variable(s) T exist so that List<T> conforms to List
Run Code Online (Sandbox Code Playgroud)
它显示......我仍在努力想出流.
我有一个String"ishant"和一个Set<String>["Ishant","Gaurav","sdnj"].我需要为此编写谓词.我尝试过如下代码,但它无法正常工作
Predicate<Set<String>,String> checkIfCurrencyPresent = (currencyList,currency) -> currencyList.contains(currency);
Run Code Online (Sandbox Code Playgroud)
如何创建一个Predicate将采取Set<String>与String作为参数,并可以给结果呢?
我有一个列表,其中包含两种不同类型的对象,即折线和文本。我只想创建一个新的折线列表。
我所做的是;
var list2 = list1.SelectMany(x=> x.Type == PolyLine)
Run Code Online (Sandbox Code Playgroud)
错误:“PolyLine”是一种类型,在给定上下文中无效。
我如何在这里过滤这些对象?
java ×9
java-8 ×7
java-stream ×7
lambda ×2
c# ×1
collectors ×1
filtering ×1
hashmap ×1
linq ×1
list ×1
long-integer ×1
predicate ×1
set ×1
void ×1