我喜欢 Result 的想法。\n我喜欢封装 try/catch。
\n但我\xe2\x80\x99m 对于如何以及何时使用 Result 有点困惑。
\n我目前这样使用它:
\n我的适配器和服务返回结果。记录失败和堆栈跟踪,但不执行其他操作
\nrunCatching{\n .... // do something cool \n}.onFailure {\n logger.error("Something bad happened ", it)\n}\nRun Code Online (Sandbox Code Playgroud)\n我的资源类折叠并处理结果
\nreturn service.method().fold(\n onFailure = {\n Response.serverError().entity("Oops").build()\n },\n onSuccess = {\n Response.ok().entity(doSomethingWith(it)).build()\n }\n)\nRun Code Online (Sandbox Code Playgroud)\n这真的是使用结果的正确方法吗?或者有更惯用的 Kotlin 编码方式吗?
\n我有一个在docker中运行的Jetty应用程序.我想使用我的本地IntelliJ调试此应用程序.我在v 14.1上,所以我安装了Docker Integration插件.
在Clouds下,我使用的是当我点击'+'时显示的默认值.IntelliJ文档说这应该没问题.在这里
API URL: http://127.0.0.1:2376
Certificates folder: <empty>
Run Code Online (Sandbox Code Playgroud)
我不确定这些用途是什么,所以我不知道这些值是否正确.
在运行/调试配置下,我使用的是Docker Deployment,以及以下值:
Deployment: Docker Image
Image ID: The docker image ID
Container name: The name of the container
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到javax.ws.rs.ProcessingException:org.apache.http.conn.HttpHostConnectException:连接到http://127.0.0.1:2376 [/127.0.0.1]失败:连接被拒绝
显然我使用的API URL值不正确.关于该值应该是什么的任何建议?
我的调试选项是:
-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n -Djava.compiler=NONE
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
mostRecentMessageSentDate = messageInfoList
.stream()
.findFirst().orElse(new MessageInfo())
.getSentDate();
unprocessedMessagesCount = messageInfoList
.stream()
.filter(messageInfo -> messageInfo.getProcessedDate() == null)
.count();
hasAttachment = messageInfoList
.stream()
.anyMatch(messageInfo -> messageInfo.getAttachmentCount() > 0);
Run Code Online (Sandbox Code Playgroud)
如您所见,我将相同的列表流式传输3次,因为我想找到3个不同的值.如果我在For-Each循环中执行此操作,我只能循环一次.
这样做是否更好,性能明智的循环呢,所以我只循环一次?我发现这些流更具可读性.
编辑:我运行了一些测试:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
List<Integer> integerList = populateList();
System.out.println("Stream time: " + timeStream(integerList));
System.out.println("Loop time: " + timeLoop(integerList));
}
private static List<Integer> populateList() {
return IntStream.range(0, 10000000)
.boxed()
.collect(Collectors.toList());
}
private static long timeStream(List<Integer> integerList) {
long start …Run Code Online (Sandbox Code Playgroud) 当我在intellij插件上浏览repos时,我明白了
现在我安装了QAPlug,因为它与PMD,Checkstyle等集成.但我很想知道这两者之间有什么区别.
我有一个对象列表,如下所示:
{
value=500
category="GROCERY"
},
{
value=300
category="GROCERY"
},
{
value=100
category="FUEL"
},
{
value=300
category="SMALL APPLIANCE REPAIR"
},
{
value=200
category="FUEL"
}
Run Code Online (Sandbox Code Playgroud)
我想将其转换为如下所示的对象列表:
{
value=800
category="GROCERY"
},
{
value=300
category="FUEL"
},
{
value=300
category="SMALL APPLIANCE REPAIR"
}
Run Code Online (Sandbox Code Playgroud)
基本上将所有值添加到同一类别.
我应该使用flatMap吗?降低?我不明白这些的细微差别来弄明白.
救命?
编辑:
这个问题有一些密切的重复: Java 8 api流中是否有一个aggregateBy方法? 和 Stream API的对象的Sum和属性
但在这两种情况下,最终结果都是地图,而不是列表
根据@AndrewTobilko和@JBNizet的回答,我使用的最终解决方案是:
List<MyClass> myClassList = list.stream()
.collect(Collectors.groupingBy(YourClass::getCategory,
Collectors.summingInt(YourClass::getValue)))
.entrySet().stream().map(e -> new MyClass(e.getKey(), e.getValue()).collect(toList());
Run Code Online (Sandbox Code Playgroud) 项目主要依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.1.2.RELEASE</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>com.rallydev.rest</groupId>
<artifactId>rally-rest-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.2</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
JIRA REST API 依赖项
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency> …Run Code Online (Sandbox Code Playgroud) 我有看起来像这样的JSON:
[
{
"fields": {
"versions": [
{
"id": "36143",
"name": "ST card"
},
{
"id": "36144",
"description": "Acceptance test card",
"name": "AT card"
}
],
"severity": {
"value": "B-Serious",
"id": "14231"
}
}
},
{
"fields": {
"versions": [
{
"id": "36145",
"name": "ST card"
}
],
"severity": {
"value": "C-Limited",
"id": "14235"
}
}
}
]
Run Code Online (Sandbox Code Playgroud)
我想用jq将其转换为:
[
{
"id": "36143",
"name": "ST card"
"value": "B-Serious"
},
{
"id": "36144",
"name": "AT card"
"value": "B-Serious"
},
{
"id": …Run Code Online (Sandbox Code Playgroud) 我有一个有8个成员的Spring组件.
我目前正在通过现场注射自动装配这8名成员.
我现在想让这些成员成为私有final,并进行构造函数注入以设置它们.
这很容易做到,但现在我有一个包含8个参数的组件构造函数.
我知道我可以使用setter注入并在XML文件中设置这些值,但我不想这样做.
还有其他选择吗?
编辑:
这个组件只做了一件事.但这涉及到几个其他服务.因此8次注射