在Java 8中,有没有办法根据条件在流上应用过滤器,
例
我有这个流
if (isAccessDisplayEnabled) {
src = (List < Source > ) sourceMeta.getAllSources.parallelStream()
.filter(k - > isAccessDisplayEnabled((Source) k))
.filter(k - > containsAll((Source) k, substrings, searchString))
.collect(Collectors.toList());
} else {
src = (List < Source > ) sourceMeta.getAllSources.parallelStream()
.filter(k - > containsAll((Source) k, substrings, searchString))
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
我正在添加过滤器
.filter(k - > isAccessDisplayEnabled((Source) k)))
Run Code Online (Sandbox Code Playgroud)
在基于if-else条件的流上.有没有办法避免if-else,因为如果有更多的过滤器出现,那么它将很难维护.
请告诉我
这是我的Rest模板配置,
@Bean
@Qualifier("myRestService")
public RestTemplate createRestTemplate(@Value("${connection.timeout}") String maxConn) {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(maxTotalConn);
connectionManager.setDefaultMaxPerRoute(maxPerChannel);
RequestConfig config = RequestConfig.custom().setConnectTimeout(100000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager)
.setDefaultRequestConfig(config).build();
ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.setErrorHandler(new RestResponseErrorHandler());
restTemplate.setMessageConverters(createMessageConverters());
return restTemplate;
}
Run Code Online (Sandbox Code Playgroud)
我使用PoolingHttpClientConnectionManager来管理连接.
它可以通过以下代码访问,
ResponseEntity<String> response = restClient.exchange( url, HttpMethod.GET, entity , String.class );
Run Code Online (Sandbox Code Playgroud)
我是否需要在上述调用之后释放连接,或者是否由RestTemplate处理.如果我们需要照顾释放连接.
请有人解释/说明如何释放连接.
我正在尝试一个简单的弹簧启动应用程序,它总是自动关闭
:: Spring Boot :: (v1.4.1.RELEASE)
2016-10-23 13:05:21.681 INFO 16532 --- [ main] com.example.RestBootApplication : No active profile set, falling back to default profiles: default
2016-10-23 13:05:21.766 INFO 16532 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e20b53a: startup date [Sun Oct 23 13:05:21 EDT 2016]; root of context hierarchy
2016-10-23 13:05:23.682 INFO 16532 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-10-23 13:05:23.704 INFO 16532 --- [ main] com.example.RestBootApplication : Started RestBootApplication in 2.632 seconds (JVM running …Run Code Online (Sandbox Code Playgroud) 有两个地图,我试图将它们合并到一个地图(finalResp).
Map<String, String[]> map1 = new HashMap<>();
Map<String, String> map2 = new HashMap<>();
HashMap<String, String> finalResp = new HashMap<String, String>();
Run Code Online (Sandbox Code Playgroud)
解决方案 - 在Java 8之前 - 实现如下:
for (Map.Entry<String, String[]> entry : map1.entrySet()) {
if (map2.containsKey(entry.getKey())) {
String newValue = changetoAnother(map1.get(entry.getKey()), map2.get(entry.getKey()));
finalResp.put(entry.getKey(), newValue);
}
}
Run Code Online (Sandbox Code Playgroud)
使用Java 8,我坚持这样:
HashMap<String, String> map3 = new HashMap<>(map2);
map1.forEach((k, v) -> map3.merge(k, v, (i, j) -> mergeValue(i, j) ));
Run Code Online (Sandbox Code Playgroud)
如何检查地图1中是否没有地图2键并修改值?
java ×3
java-8 ×2
java-stream ×2
spring ×2
collections ×1
httpclient ×1
lambda ×1
resttemplate ×1
spring-boot ×1