我想使用mapMulti而不是flatMap重构以下代码:
// using flatMap (version 1) => returns Set<Item>
var items = users.stream()
.flatMap(u -> u.getItems().stream())
.collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
进入这个(版本2):
// using mapMulti (version 2) => returns Set<Item>
var items = users.stream()
.<Item>mapMulti((u, consumer) -> u.getItems().forEach(consumer))
.collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
两者都返回相同的元素。然而,我怀疑我是否真的应该用flatMap更冗长的mapMulti. 为什么需要在mapMuli( .<Item>mapMulti)之前添加类型信息。如果我不包含类型信息,它将返回一个Set<Object>. (如何)我可以简化吗mapMulti?
我想将包含字符串的大文件拆分成一组新的(较小的)文件并尝试使用nio2.
我不想将整个文件加载到内存中,所以我尝试使用BufferedReader.
较小的文本文件应受文本行数的限制.
该解决方案有效,但我想询问是否有人通过使用java 8(也许lamdas with stream() - api?)和nio2知道一个具有更好性能的解决方案:
public void splitTextFiles(Path bigFile, int maxRows) throws IOException{
int i = 1;
try(BufferedReader reader = Files.newBufferedReader(bigFile)){
String line = null;
int lineNum = 1;
Path splitFile = Paths.get(i + "split.txt");
BufferedWriter writer = Files.newBufferedWriter(splitFile, StandardOpenOption.CREATE);
while ((line = reader.readLine()) != null) {
if(lineNum > maxRows){
writer.close();
lineNum = 1;
i++;
splitFile = Paths.get(i + "split.txt");
writer = Files.newBufferedWriter(splitFile, StandardOpenOption.CREATE);
}
writer.append(line);
writer.newLine();
lineNum++;
}
writer.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我想要一个空安全比较器,但它不起作用:
Comparator<Item> sort_high = (i1, i2)-> Double.compare(i2.getUser().getValue(), i1.getUser().getValue());
items.sort(Comparator.nullsFirst(sort_high));
Run Code Online (Sandbox Code Playgroud)
但是,如果 item.getUser().getValue() (或 item.getUser()) 为 null,我会得到 NPE。
at Item.lambda$1(Item.java:270)
at java.util.Comparators$NullComparator.compare(Comparators.java:83)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
at java.util.TimSort.sort(TimSort.java:220)
at java.util.Arrays.sort(Arrays.java:1438)
at java.util.List.sort(List.java:478)
Run Code Online (Sandbox Code Playgroud)
怎么了?
我有这个:
// returns true if both are equal (independent of scale) and also checks against null
public static boolean isEqual(BigDecimal val1, BigDecimal val2) {
// 1. check: both will be null or both will be non-null.
if (val1 != null ^ val2 != null) return false;
// 2. check: if not null, then compare if both are equal
return !(val2 != null && val1.compareTo(val2) != 0);
}
Run Code Online (Sandbox Code Playgroud)
我想将布尔表达式合并为一个。所以我用这个:
public static boolean isEqual(BigDecimal val1, BigDecimal val2) {
return !(val1 != null …Run Code Online (Sandbox Code Playgroud) 我完全使用https://material-ui.com/components/bottom-navigation/示例中定义的代码:
// saved in app.tsx:
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import BottomNavigation from '@material-ui/core/BottomNavigation';
import BottomNavigationAction from '@material-ui/core/BottomNavigationAction';
import FolderIcon from '@material-ui/icons/Folder';
import RestoreIcon from '@material-ui/icons/Restore';
import FavoriteIcon from '@material-ui/icons/Favorite';
import LocationOnIcon from '@material-ui/icons/LocationOn';
const useStyles = makeStyles({
root: {
width: 500,
},
});
export default function LabelBottomNavigation() {
const classes = useStyles();
const [value, setValue] = React.useState('recents');
const handleChange = (event: React.ChangeEvent<{}>, newValue: string) => {
setValue(newValue);
};
return (
<BottomNavigation value={value} onChange={handleChange} …Run Code Online (Sandbox Code Playgroud) 我想将包含字符串的大文件合并到一个文件中,并尝试使用nio2.我不想将整个文件加载到内存中,所以我尝试使用BufferedReader:
public void mergeFiles(filesToBeMerged) throws IOException{
Path mergedFile = Paths.get("mergedFile");
Files.createFile(mergedFile);
List<Path> _filesToBeMerged = filesToBeMerged;
try (BufferedWriter writer = Files.newBufferedWriter(mergedFile,StandardOpenOption.APPEND)) {
for (Path file : _filesToBeMerged) {
// this does not work as write()-method does not accept a BufferedReader
writer.append(Files.newBufferedReader(file));
}
} catch (IOException e) {
System.err.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了这个,这个工作,hower,字符串的格式(例如新行等不会被复制到合并文件):
...
try (BufferedWriter writer = Files.newBufferedWriter(mergedFile,StandardOpenOption.APPEND)) {
for (Path file : _filesToBeMerged) {
// writer.write(Files.newBufferedReader(file));
String line = null;
BufferedReader reader = Files.newBufferedReader(file);
while ((line = reader.readLine()) != null) { …Run Code Online (Sandbox Code Playgroud) 我有一个HashMap并希望通过附加另一个字符串"hello"来更改值(这是一个字符串).
HashMap<User, String> all = new HashMap<>();
mymap.forEach((k, v) -> v = v + " hello");
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用,"mymap"没有变化.怎么了?
我有一个"项目"列表,每个项目都有item.posts属性(这是一个后期实例列表).
我想通过两个属性过滤我的"item"-list:
如果"item.isBig"并且如果启用了项目的任何帖子,则收集返回的项目Stream.
但是,我不知道如何使用"i.getPosts#isEnabled"执行"anyMatch".
items.stream()
.filter(Item::isBig)
.anyMatch(i.getPosts()->p.isEnabled) // this does not work
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我有一个比较整数(可以为空)的比较器,所以
我尝试使用Comparator.comparingInt,但它不起作用:
public static final Comparator<Task> sort =
Comparator.comparingInt(x -> x.getNumber(),
Comparator.nullsLast(null));
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误:
The method comparingInt(ToIntFunction<? super T>)
in the type Comparator is not applicable for
the arguments ((<no type> x) -> {}, Comparator.nullsLast(null))
Run Code Online (Sandbox Code Playgroud)
当使用comparing而不是 时comparingInt,它可以工作:
public static final Comparator<Task> sort =
Comparator.comparing(x -> x.getNumber(),
Comparator.nullsLast(null));
Run Code Online (Sandbox Code Playgroud)
但我想知道为什么因为我想比较整数所以compareInt将是正确的选择。
如何Comparator.nullsLast与 ? 结合 使用Comparator.comparingInt?
这两种方法等效吗?
版本1:
var diff = Duration.between(begin, end).toHours();
Run Code Online (Sandbox Code Playgroud)
版本 2;
var diff = ChronoUnit.HOURS.between(begin, end);
Run Code Online (Sandbox Code Playgroud)
是否存在任何隐含的差异?如果是,我应该选择哪一个?
java ×7
java-8 ×5
java-stream ×3
nio2 ×2
equality ×1
java-16 ×1
java-17 ×1
java-time ×1
material-ui ×1
reactjs ×1
simplify ×1
typescript ×1