小编Pra*_*een的帖子

如何在Java中拆分字符串

我有一个字符串,"004-034556"我想分成两个字符串:

string1="004";
string2="034556";
Run Code Online (Sandbox Code Playgroud)

这意味着第一个字符串将包含之前的字符'-',第二个字符串将包含之后的字符'-'.我还想检查字符串是否包含'-'在其中.如果没有,我会抛出异常.我怎样才能做到这一点?

java string

1564
推荐指数
23
解决办法
373万
查看次数

Java将文件读入ArrayList?

你如何将文件的内容读入ArrayList<String>Java中?

这是文件内容:

cat
house
dog
.
.
.
Run Code Online (Sandbox Code Playgroud)

只需阅读每个单词ArrayList.

java

60
推荐指数
6
解决办法
27万
查看次数

语句lambda可以用表达式lambda替换

我使用Optional工具进行用户和邀请验证

@DeleteMapping("/friends/{username}")
public
HttpEntity<Boolean> removeFriend(
        @ApiParam(value = "The user's name", required = true) @PathVariable String username
) {
    Long fromId = authorizationService.getUserId();

    return userService.findByUsername(username)
            .map(user -> {
                 return friendshipService.findFriendship(fromId, user.getId())
                        .map(friendship -> {
                            friendshipService.removeFriendship(friendship);

                            friendship.setToId(friendship.getFromId());
                            friendship.setFromId(friendship.getToId());

                            friendshipService.removeFriendship(friendship);

                            return ResponseEntity.ok(true);
                        }).orElseGet(() -> ResponseEntity.notFound().build());
            }).orElseThrow(() -> new ResourceNotFoundException("User not found"));
Run Code Online (Sandbox Code Playgroud)

但是,IntelliJ正在着色我的灰色返回 https://zapodaj.net/2f48b1a26c392.png.html 但是当我删除返回时,它向我突出显示没有返回https://zapodaj.net/37605f08165c9.png.html

有人可以解释它是如何工作的以及它的全部内容?

java lambda spring spring-mvc optional

55
推荐指数
2
解决办法
3万
查看次数

如何模拟结果集并使用Java中的Mockito填充它

我有代码,我填充ResultsetCallableStatement.executeQuery().我已经嘲笑了ResultSet,CallableStatement但为了测试我必须填充的方法ResultSet.

这是我正在测试的方法的代码

ResultSet rset = cs.executeQuery();
while (rset.next()) {
IndexVolatilityImpl tsImpl = new IndexVolatilityImpl();
tsImpl.setTradeDate(rset.getString("trade_date"));
tsImpl.setTradeTime(rset.getString("trade_time"));
tsImpl.setExprDate(rset.getString("expr_date"));
tsImpl.setSymbol(rset.getString("symbol"));
tsImpl.setTradePrice(rset.getDouble("trade_price"));
tsImpl.setContractMonth(rset.getString("contract_month"));
tsImpl.setMilliSecs(rset.getString("trade_time_thou"));
colIndexVolatilityImpl.add(tsImpl);
Run Code Online (Sandbox Code Playgroud)

我已经嘲笑了CallableStatement和ResultSet,因为他们被嘲笑我的rset是空的.我想填充Resultset并按如下方式执行

resultSetMock = Mockito.mock(ResultSet.class);
Mockito.when(resultSetMock.getString("trade_date")).thenReturn("03/10/2011");
Mockito.when(resultSetMock.getString("trade_time")).thenReturn("12:24:56");
Mockito.when(resultSetMock.getString("expr_date")).thenReturn("03/19/2011");
Mockito.when(resultSetMock.getString("symbol")).thenReturn("VIX1");
Mockito.when(resultSetMock.getDouble("trade_price")).thenReturn(Double.valueOf("20.96"));
Mockito.when(resultSetMock.getString("contract_month")).thenReturn("1");
Mockito.when(resultSetMock.getString("trade_time_thou")).thenReturn("165");

Mockito.doReturn(resultSetMock).when(callableStatementMock).executeQuery();
Run Code Online (Sandbox Code Playgroud)

不过rsetnull.

java junit4 mockito

35
推荐指数
4
解决办法
5万
查看次数

在GroupingBy中等效的Java 8 Streams API HAVING子句?

我遇到了一个难以解决的问题Streams API.嗯,它是可以解决的但不是优雅的,在我能说的一个电话中.下面,FeatureContentWeight我将根据特征内容对一组对象进行分组,并获得每个特征和内容的最大权重.我从最后得到了值,Map因为我不需要维护Map.问题是,我只想要包含超过3个项目的组.所以我想要超过给定计数的功能,内容对的每个功能和内容的最大权重.在SQL这只是一个简单的HAVING条款.在Streams API它看起来并不微不足道,但我只在Streams API几天了.

任何想法都赞赏.以下是我的方法,

List<FeatureContentWeight> nearestNeighbors = neighborPostings
    .stream()
    .collect(
    groupingBy(
        p -> FeatureContent.Create(p.getFeatureId(), p.getContentId()), 
        collectingAndThen(maxBy(comparingDouble(FeatureContentWeight::getWeight)),Optional::get))).values();
Run Code Online (Sandbox Code Playgroud)

java stream java-8

5
推荐指数
1
解决办法
2788
查看次数

Netty - 客户端/服务器聊天

为了我的项目目的之一,我需要在 netty 中进行客户端/服务器通信。所以我刚开始动手改进。我正在学习 netty,我是初学者。

我尝试过一个简单的客户端服务器与 netty 聊天。

客户端和服务器正在初始化,我可以看到服务器能够获取用于建立连接的客户端管道,但是当客户端发送消息时,它没有进入 ServerAdapterHandler 的 messageReceived 部分。下面是我的源代码,

客户:

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class ContainerClient {

    String server;
    int port;
    int containerPort;

    public ContainerClient(String server, int port, int containerPort) {
        this.server = server;
        this.port = port;
        this.containerPort = containerPort;
    }

    public static void main(String[] args) {
        String server = "localhost";
        int port = 5252;
        int containerPort = 8094;
        new ContainerClient(server, port, containerPort).start();
    }

    public void start() {
        EventLoopGroup group …
Run Code Online (Sandbox Code Playgroud)

java client-server netty

5
推荐指数
1
解决办法
2万
查看次数

Mockito - Mock没有被注入其中一个测试用例

我有一个jsf spring应用程序并使用mockito进行单元测试.NullPointerException当我junitiEmployeeService模拟中运行我的测试时,我不断得到.有没有Exception进行iSecurityLoginService.

被嘲笑的方法

@Autowired
IEmployeeService iEmployeeService;
@Autowired
ISecurityLoginService iSecurityLoginService;
public void addEvent() {

    entityEventsCreate.setTitle(entityEventsCreate.getTitle());
    entityEventsCreate.setModifiedBy(iSecurityLoginService
                .findLoggedInUserId());

    int eventId = iEmployeeService.addEmployeeTimeOff(entityEventsCreate);
}
Run Code Online (Sandbox Code Playgroud)

我的JUnit测试用注释 @RunWith(MockitoJUnitRunner.class)

@Mock
ISecurityLoginService iSecurityLoginService;

@Mock
IEmployeeService iEmployeeService;

@InjectMocks
ServiceCalendarViewBean serviceCalendarViewBean  = new ServiceCalendarViewBean();

@Before public void initMocks() {
           MockitoAnnotations.initMocks(this);
}

@Test
public void testSaveEvent() {
    Mockito.when(iSecurityLoginService.findLoggedInUserId()).thenReturn(1);
    serviceCalendarViewBean.getEntityEventsCreate().setTitle("Junit Event Testing");

    Mockito.when(iSecurityLoginService.findLoggedInUserId()).thenReturn(1);
    Mockito.when(iEmployeeService.addEmployeeTimeOff(Mockito.any(Events.class))).thenReturn(2);

    serviceCalendarViewBean.addEvent();
}
Run Code Online (Sandbox Code Playgroud)

java junit spring unit-testing mockito

4
推荐指数
2
解决办法
1万
查看次数

dumps() 需要 1 个位置参数,但给出了 2 个

import json
a={"name": "abc", "age": 20, "sal": 20000}
with open("test.json", "w") as p1:
    json.dumps(a, p1)
Run Code Online (Sandbox Code Playgroud)

下面是错误
dumps()需要 1 个位置参数,但给出了 2 个

Python 版本:3.6.4

json python-3.x

1
推荐指数
1
解决办法
4982
查看次数