我有一个简单的spring boot应用程序和一个控制器类。我的控制器内部有一个简单的方法:
@RequestMapping(value = "/heartbeat", method = RequestMethod.GET)
public ResponseEntity<String> heartbeat() {
return new ResponseEntity<>("success", HttpStatus.OK)
}
Run Code Online (Sandbox Code Playgroud)
我从Postman调用此方法,我可以看到完成此方法所花费的时间在每次调用中都不相同。
例如28ms,70ms,15ms ...
如果我们谈论毫秒是可以的,但是我注意到在较大的Web服务中,这种差异更大,有时甚至是几秒钟。
我想这是正常现象,但是是什么原因造成的呢?
我有一个这样的清单
List<String> customList = Arrays.asList(
"5000 Buruli ulcer is an infectious disease",
"6000 characterized by the development",
"7000 of painless open wounds.",
"8000 The disease largely occurs in",
"10000 sub-Saharan Africa and Australia."
);
Run Code Online (Sandbox Code Playgroud)
我想把它转换List成TreeMap<String, List<String>>这样:
"5000", ["Buruli", "ulcer", "is", "an", "infectious", "disease"]
"6000", ["characterized", "by", "the", "development"]
// etc
Run Code Online (Sandbox Code Playgroud)
到目前为止我的代码:
TreeMap<String, List<String[]>> collect = customList.stream()
.map(s -> s.split(" ", 2))
.collect(Collectors
.groupingBy(a -> a[0], TreeMap::new, Collectors.mapping(a -> a[1].split(" "), Collectors.toList())));
Run Code Online (Sandbox Code Playgroud)
我有两个问题。
TreeMap::new可能不起作用,因为顺序与原始List …我尝试将 MapStruct 与 QueryDsl、Spring Boot 3 和 Java 17 一起使用,但maven-compiler-plugin我用于 MapStruct 的似乎阻止了 QueryDsl 资源的生成。
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.0.0</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
<classifier>jakarta</classifier>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
如果我删除maven-compiler-pluginQueryDsl 资源,则会生成但 MapStruct 不会生成。
我还尝试添加 QueryDsl 注释处理器,但没有任何运气。
<path>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
</path>
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
我有以下格式的列表,我想将此列表分组为分钟间隔。
List<Item> myObjList = Arrays.asList(
new Item(LocalDateTime.parse("2020-09-22T00:13:36")),
new Item(LocalDateTime.parse("2020-09-22T00:17:20")),
new Item(LocalDateTime.parse("2020-09-22T01:25:20")),
new Item(LocalDateTime.parse("2020-09-18T00:17:20")),
new Item(LocalDateTime.parse("2020-09-19T00:17:20")));
Run Code Online (Sandbox Code Playgroud)
例如,给定 10 分钟的间隔,列表的前 2 个对象应该在同一个组中,第三个应该在不同的组中,依此类推。
可以使用 Java 的 8groupingBy函数将此 List 分组为间隔吗?
我的解决方案是将列表中的每个日期与列表中的所有其他日期进行比较,并在新列表中添加不同 X 分钟的日期。这似乎是非常缓慢和“hacky”的解决方法,我想知道是否有更稳定的解决方案。
java ×3
spring-boot ×2
httprequest ×1
java-8 ×1
java-stream ×1
java-time ×1
mapstruct ×1
maven ×1
querydsl ×1