我有以下Java代码:
List<BaseQuery> queries;
Map<Long, List<BaseQuery>> map = new HashMap<>();
for (BaseQuery query : queries) {
List<BaseQuery> queryList = map.get(query.getCharacteristicId());
if(queryList == null) {
queryList = new ArrayList<>();
map.put(query.getCharacteristicId(), queryList);
}
queryList.add(query);
}
Run Code Online (Sandbox Code Playgroud)
能否请您展示如何将其转换为Java 8和流?
是否有一种简单/优雅的方法可以在列表中为地图中的每个键求和数字,例如Map<String, List<BigDecimal>>我希望得到的Map<String, BigDecimal>?我找不到/想出来......
有没有办法将a转换List<StringBuilder>为List<String>使用Java Streams?是否有一种简单的方法来进行类型转换?
我使用时的性能list.parallelStream()比使用时差得多list.stream()。您认为为什么会发生这种情况?顺便说一句,这是Java 17,我的CPU 是桌面级的 i5。
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.chrono.ChronoPeriod;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class App {
public static void main(String[] args) throws Exception {
int size = 1;
List<Integer> list = null;
long startTimeN;
long endTimeN;
long startTimeP;
long endTimeP;
long normalStreamCheckedSize;
long normalStreamTime;
long parallelStreamCheckedSize;
long parallelStreamTime; …Run Code Online (Sandbox Code Playgroud) This is the student class definition.
public class Student {
public Student(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
The map function should accept like a Function interface
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Run Code Online (Sandbox Code Playgroud)
That means the parameters should be like that
int func(int a){
return b;
}
Run Code Online (Sandbox Code Playgroud)
We need to make sure have one method parameter.
So why getName() can work?
The method actually change to …
我试图弄清楚stream.of()和之间的差异在哪里stream(),我遇到了这个。谁能向我解释为什么第一个不起作用?
//Given: List<Integer> transactions
transactions.add(1);
transactions.add(2);
//This won't compile,
Stream.of(transactions).filter(x->x<3).collect(Collectors.toList());
// while this one does fine:
transactions.stream().filter(x->x<3).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
Stream.of("d2", "a2", "b1", "b3", "c")
.map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
})
.anyMatch(s -> {
System.out.println("anyMatch: " + s);
return ((String) s).startsWith("A");
});
Run Code Online (Sandbox Code Playgroud)
输出是:
map: d2
anyMatch: D2
map: a2
anyMatch: A2
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么第一个元素进入流而第三个和第四个没有?
任何详细的解释表示赞赏。
我正在尝试使用流将以下代码重构为 java8。我如何获得哈希图中任何特定键的值 请提出建议。
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HashMapUtility {
public static void main(String[] args) {
Map<String, List<Integer>> map = new HashMap<>();
map.put("key1", Arrays.asList(1, 2, 3, 4));
map.put("key2", Arrays.asList(4, 5, 6, 7));
map.put("key3", Arrays.asList(8, 9, 10, 11));
map.put("key4", Arrays.asList(12, 13, 14, 15));
/*how to write it in JAVA8*/
for (Map.Entry<String, List<Integer>> mapIter : map.entrySet()) {
List<Integer> li = mapIter.getValue();
for (Integer num : li) {
if (num % 2 == 0) {
System.out.println(num);
}
} …Run Code Online (Sandbox Code Playgroud) public class Main {
public static void main(String[] args) {
byte[] content = null;
try {
content = Files.readAllBytes(Paths.get("/path/to/file.ext"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(content);
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
Exception in thread "main" java.lang.OutOfMemoryError: Required array size too large
at java.nio.file.Files.readAllBytes(Unknown Source)
at Main.main(Main.java:13)
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以毫无例外地读取数组(流等)?该文件小于允许的 HEAP,因此应该可以在程序中一次存储所有数据。
我问的原因或原则.
我发现Java8中"流"接口的大多数方法都没有声明为"默认",因此它们没有方法体.例如:
boolean anyMatch(谓词谓词);
boolean allMatch(谓词谓词);
流映射(Function mapper);
正如您在源文件Stream.java中看到的那样.
但是这些没有实体的方法似乎能够在java程序中执行.
你知道为什么吗?
谢谢.
java functional-programming lambda-calculus java-8 java-stream
我有两个数组:
a = ["a","b","c"]
b = ["d","e","f"]
Run Code Online (Sandbox Code Playgroud)
如何将它们合并到一个数组中,如下所示:
c = ["a=d", "b=e", "c=f"]
Run Code Online (Sandbox Code Playgroud)
使用等号 ( =) 作为合并字符串之间的分隔符?
java ×11
java-stream ×11
java-8 ×4
arrays ×1
concurrency ×1
file-read ×1
hashmap ×1
java-17 ×1
reduce ×1