我有地图Map<LocalDateTime, String>。
如何按键值对其进行排序?我应该使用什么比较器?
someMap.entrySet().stream().sorted(Comparator.comparing(???))
Run Code Online (Sandbox Code Playgroud)
或者
someMap.entrySet().stream().sorted(??)
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我应该写什么来代替“??” ?
排序:
Key Value
2020-01-09 09:57:58.631 Some info
2020-01-09 09:57:59.224 Some info
2020-01-09 09:59:03.144 Info
Run Code Online (Sandbox Code Playgroud)
不排序:
Key Value
2020-01-09 09:57:58.631 Some info
2020-01-09 09:59:03.144 Info
2020-01-09 09:57:59.224 Some info
Run Code Online (Sandbox Code Playgroud) 我有一个我正在使用的 Java 可选变量,如下所示。
Optional<Forms> formsOptional = input.data().get().forms();
if(formsOptional.isPresent()) {
this.doSomething(myValue, forms.get());
} else {
this.doSomething(myValue, Forms.builder().names(myList).build());
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下, doSomething 方法是一个执行某些操作的 void 方法。由于这是一个空方法,我对如何使用 map().orElseGet() 感到困惑,因为我也没有任何要转换的东西。任何使用任何 Java8 可选技术优化这段代码的建议将不胜感激。
我想转换以下内容
String flString="view1:filedname11,view1:filedname12,view2:fieldname21";
Run Code Online (Sandbox Code Playgroud)
到 aMap<String,Set<String>>获取键/值如下:
view1=[filedname11,filedname12]
view2=[fieldname21]
Run Code Online (Sandbox Code Playgroud)
我想使用 Java 8 流。我试过
view1=[filedname11,filedname12]
view2=[fieldname21]
Run Code Online (Sandbox Code Playgroud)
然而,键也被添加到值列表中。
我一直在使用此代码:
Event rEvent = new Event();
Map<Long, Message> msgMap = getMsg();
Message msg = msgMap.get(rEvent.getMsgId());
Long id = Optional.ofNullable(rEvent.getPkId()).orElse(msg.getPkId());
Run Code Online (Sandbox Code Playgroud)
假设rEvent.getPkId()和 msg 都是null。所以Optional.ofNullable(rEvent.getPkId())会被跳过,然后orElse被执行。这里抛出NPE. 如何使用Optional.ofNullable多个空检查重新编码?
我最近遇到了一个面试问题,要求我从List. 唯一的问题是我不能使用如下 2 个过滤器:
List<Integer> nums = Arrays.asList(1,2,3,4,5);
List<Integer> odd = nums.stream().filter(n -> n%2 != 0).collect(Collectors.toList());
List<Integer> even = nums.stream().filter(n -> n%2 == 0).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我说这是不可能的,因为最终collect方法只能返回 1 个列表。
我错过了什么?
我有以下代码
item.getSubCategory().stream().map(category -> {
return subCategoriesList.add(new SubCategoryViewModel(
category.get(String.format("_%s",categoryProperties[0])).toString(),
category.get(categoryProperties[2]).toString(),
category.get(categoryProperties[3]).toString()));
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这item.getSubCategory()是一个类别模型列表。现在,如果 null 出现在getSubCategory我们会得到一个空指针异常并且不能对其应用蒸汽。在将流应用于列表之前,是否有更好的方法来处理对列表的空检查。
我不想使用IF语句来检查getSubCategory. java steam API 有没有更好的方法?
您好,我来这里是为了解决Locale格式化程序语言的DateTimeFormatter问题,让我向您介绍一下:
堆栈跟踪观察到的错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text '28-dic-2017' could not be parsed at index 3
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1953)
at java.base/java.time.LocalDate.parse(LocalDate.java:429)
at com.examen.Presentation.Principal.parseDate(Principal.java:28)
at com.examen.Presentation.Principal.main(Principal.java:19)
Run Code Online (Sandbox Code Playgroud)
最小的、可重现的代码示例:
public static LocalDate parseDate(String fecha) {
Locale loc = new Locale("es", "ES");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", loc);
LocalDate date = LocalDate.parse(fecha, formatter);
return date;
}
Run Code Online (Sandbox Code Playgroud)
我在尝试来自语言环境的不同事物和方法时遇到了同样的错误,而 DateTimeFormatter 甚至不知道我在做什么,但我认为这显然不会起作用。
.collect(Collectors.groupingBy(Point::getName, Collectors.summingInt(Point::getCount)));
Run Code Online (Sandbox Code Playgroud)
我有一个Point对象列表,我想按某个键(name字段)分组并按count该类的字段求和。上面的代码完成了这项工作,但返回了一个Point对象映射。但是,我想要Point返回的对象列表-而不是 map。
使用 java 8 流执行此操作的最干净方法是什么?
例子:
input = [pt("jack", 1), pt("jack", 1), pt("jack", 1)]
result = [pt("jack", 3)]
Run Code Online (Sandbox Code Playgroud)
谢谢
大家好,我有这样的代码
public static void main(String[] args) {
System.out.println(LocalDate.now().plusYears(1).plusMonths(6).plusDays(5));
System.out.println(LocalDate.now().plusDays(5).plusMonths(6).plusYears(1));
}
Run Code Online (Sandbox Code Playgroud)
我得到两个不同的结果
2022-10-31
2022-11-01
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么吗?谢谢
IntStream.range(x,y)将从x(inclusive)和y(exclusive)返回一个流。
IntStream.rangeClosed(x,y)将从x(inclusive)和y(inclusive)返回一个流。
我希望rangeClosed(x,y)调用range(x,y-1)或range(x,y)调用rangeClosed(x,y-1). 但是在查看range它的源代码时,它就像:
if (startInclusive >= endExclusive) {
return empty();
} else {
return StreamSupport.intStream(
new Streams.RangeIntSpliterator(startInclusive, endExclusive, false /*not closed*/), false);
}
Run Code Online (Sandbox Code Playgroud)
该rangeClosed也有一个非常相似的实现,而不是range(x,y+1)。唯一的区别是,第三个参数Streams.RangeIntSpliterator是true代替false表示该范围被关闭。
然后使用此布尔值来初始化类中的int last字段,Streams.RangeIntSpliterator并针对它提到了以下注释:
1 如果范围是封闭的并且最后一个元素还没有被遍历 否则 0 如果范围是开放的,或者是一个封闭的范围并且所有元素都已经被遍历
为什么这样的实现是必要的,而不是range简单地调用rangeClosed或相反?调用rangeClosed(x,y)而不是调用之间有什么显着区别range(x,y+1)吗?