有人可以建议,为什么我不能在这里应用方法参考?
工作代码。
System.out.println(
Arrays.stream(str.split(" "))
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting())));
Run Code Online (Sandbox Code Playgroud)
编译错误,无法解析方法
System.out.println(
Arrays.stream(str.split(" "))
.collect(Collectors.groupingBy(Function::identity,Collectors::counting)));
Run Code Online (Sandbox Code Playgroud) 尝试以下代码后:
db.getCollection('col').find({}).forEach(function(doc){
var id = new UUID().toString(); // this doesn't work, neither does 'var id = new UUID();'
db.getCollection('pubCol')
.update({
"uuid" : id,
"deleted" : false
}
, {upsert: true});
});
Run Code Online (Sandbox Code Playgroud)
我最终分别得到以下结果
"uuid" : "UUID(\"4554a991-2b7f-4464-b871-5f4e91332630\")"
"uuid" : UUID("4554a991-2b7f-4464-b871-5f4e91332630")
Run Code Online (Sandbox Code Playgroud)
但我正在寻找
"uuid" : "4554a991-2b7f-4464-b871-5f4e91332630"
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ZGC 垃圾收集器运行 Spring Boot 应用程序,并传入以下 JVM 选项build.gradle:
bootRun {
jvmArgs = ["-XX:+UnlockExperimentalVMOptions", "-XX:+UseZGC", "-Xlog:gc*"]
}
Run Code Online (Sandbox Code Playgroud)
运行应用程序 ( gradle bootRun) 时出现以下错误:
> Task :bootRun FAILED
Error occurred during initialization of VM
Option -XX:+UseZGC not supported
Run Code Online (Sandbox Code Playgroud)
java - 版本:
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10, mixed mode)
Run Code Online (Sandbox Code Playgroud)
如果有帮助,我正在运行 10.15.2 (19C57) 版本的 macOS (Catalina)。只是 macOS 的问题吗?
java8中的以下代码没有返回短时区名称,而是返回“-08:00”
ZonedDateTime dateTime1 = ZonedDateTime.parse("2020-01-22T08:07:59.179-08:00");
ZoneId.of("America/Los_Angeles");
System.out.println(dateTime1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")));
Run Code Online (Sandbox Code Playgroud)
此输出:2020-01-22 08:07:59.179 -08:00
请知道哪种格式输入会产生“2020-01-22 08:07:59.179 PST”
我有一组 String 并且想将它们中的每一个映射到一个空列表然后返回。我知道 for 循环可以解决它,但是有没有使用 Java Stream 的好方法?
例如:我有一组 {"a", "b", "c"}
我想返回一张地图Map<String, List<Object>>,就像{"a": emptyList(), "b": emptyList(), "c": emptyList()}
我有一个枚举我通话Permission的安全权限,此事对我的申请。在数据库中,用户可能拥有与我的应用程序无关的其他权限。从数据库读取用户时,我得到 aList<String>并且我想建立 a List<Permission>,忽略那些不是枚举值的字符串。
public enum Permission { ADMIN, USER }
List<String> plaintextAuthorities = List.of("ADMIN","USER","COFFEEMAKER")
List<Permission> appPermissions = plaintextAuthorities.stream()
.map(Permission::valueOf) // this will throw an IllegalArgumentException for 'COFFEEMAKER'
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
问题是该map步骤在到达与枚举值不对应的字符串之一时将引发异常。我怎样才能filter去掉这些值,以便在不抛出异常的情况下悄悄地忽略它们?
How can I filter map of map using java stream. I can do it using double loop but I think this is not efficient.
public class MapUsage {
static Map<Integer, Map<Integer, String>> parentMap = new HashMap<>();
static Map<String, String> userRole = new HashMap<>();
public static void main(String ... args){
initializeData();
displayMapForUser("user1", parentMap);
// printMap(parentMap);
}
private static void displayMapForUser(String user1, Map<Integer, Map<Integer, String>> parentMap) {
Integer role = new Integer(userRole.get(user1));
Map<Integer, Map<Integer, String>> userMap = new HashMap<>();
Map<Integer, String> childMap …Run Code Online (Sandbox Code Playgroud) public static void main(String[] args) {
System.out.println(hasPairWithSum(new int[] { 12, 4, 3, 4, 1, 7 }, 9));
System.out.println(hasPairWithSum2(new int[] { 12, 4, 3, 4, 1, 7 }, 9));
System.out.println(hasPairWithSum3(new int[] { 12, 4, 3, 4, 1, 7 }, 9));
}
public static boolean hasPairWithSum(int[] intArray, int sum) {
int len = intArray.length;
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (intArray[i] + intArray[j] …Run Code Online (Sandbox Code Playgroud) 我被问到的问题是:
找到并打印(每行一个)的那些值一个是形式3×+ 1对于一些x的。每个这样的值应该只打印一次,在它第一次出现的位置。
我尝试了许多模的变体来尝试获得正确的数字,但我似乎无法解决这个问题。
这是我开始的代码:
import java.util.*;
import java.util.stream.*;
class Example {
public static void main(String argv[]) {
int a[] =
Arrays.stream(argv)
.mapToInt(s -> Integer.parseInt(s))
.toArray();
}
// fill in here with expression starting Arrays.stream(a)
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助。
我想使用 Java 8 使用多个if和else语句来简化下面的代码。有没有办法使用一些 Java 8 功能来完全摆脱它们,例如Optional?我试图在这里找到一些东西,但找不到答案。
LocalDateTime beDate = someDate;
LocalDateTime aeDate = someDate;
LocalDateTime eDate;
if (beDate == null && aeDate == null) {
eDate = null;
}
else if (beDate != null && aeDate == null) {
eDate = beDate;
}
else if (beDate == null && aeDate != null) {
eDate = aeDate;
}
else if (beDate != null && aeDate != null &&
(beDate.isEqual(aeDate) || beDate.isBefore(aeDate))) {
eDate = …Run Code Online (Sandbox Code Playgroud)