我有一个从Google Places API中获取的Google PlaceSummary对象列表.我想通过他们的Google商家信息ID收集和分组,但也保留元素的顺序.我认为会起作用的是:
Map<String, List<PlaceSummary>> placesGroupedByPlaceId =
places.stream()
.collect(Collectors.groupingBy(
PlaceSummary::getPlaceId,
LinkedHashMap::new,
Collectors.mapping(PlaceSummary::getPlaceId, toList())
));
Run Code Online (Sandbox Code Playgroud)
但它甚至不会编译.它看起来应该根据收集器上的Java API文档.
以前我有这个代码:
Map<String, List<PlaceSummary>> placesGroupedByPlaceId = places.stream()
.collect(Collectors.groupingBy(PlaceSummary::getPlaceId));
Run Code Online (Sandbox Code Playgroud)
但是.collect(),Streams API上的标准不保留后续元素的顺序HashMap(显然因为HashMaps是无序的).我希望输出为a,LinkedHashMap以便Map按每个桶的插入顺序排序.
但是,我建议的解决方案不能编译.首先,它不承认PlaceSummary::getPlaceId它,因为它说它不是一个功能 - 即使我知道它是.其次,它说我不能转换LinkedHashMap<Object, Object>成M. M应该是一个通用的集合,所以它应该被接受.
如何将List转换为LinkedHashMap使用Java Stream API?有简洁的方法吗?如果它太难理解我可能只是采用旧学前Java 8方法.
我注意到在将List转换为LinkedHashMap时还有另一个Stack Overflow答案,但是这没有我想要的解决方案,因为我需要收集'this'我正在迭代的对象.
我编写了一个PHPUnit测试,用于检查调用方法时是否从闭包中抛出异常.闭包函数作为参数传递给方法,并从中抛出异常.
public function testExceptionThrownFromClosure()
{
try {
$this->_externalResourceTemplate->get(
$this->_expectedUrl,
$this->_paramsOne,
function ($anything) {
throw new Some_Exception('message');
}
);
$this->fail("Expected exception has not been found");
} catch (Some_Exception $e) {
var_dump($e->getMessage()); die;
}
}
Run Code Online (Sandbox Code Playgroud)
在ExternalResourceTemplate上指定的get函数的代码是
public function get($url, $params, $closure)
{
try {
$this->_getHttpClient()->setUri($url);
foreach ($params as $key => $value) {
$this->_getHttpClient()->setParameterGet($key, $value);
}
$response = $this->_getHttpClient()->request();
return $closure($response->getBody());
} catch (Exception $e) {
//Log
//Monitor
}
}
Run Code Online (Sandbox Code Playgroud)
有关为什么会调用fail assert语句的任何想法?你能不能捕获PHP中从闭包中抛出的异常,或者是否有一种我不知道的特定方式来处理它们.
对我来说,异常应该只是传播出返回堆栈,但它似乎没有.这是一个错误吗?仅供参考我正在运行PHP 5.3.3
基本上我在Scala中定义了一个Double,我希望它以二进制表示形式.例如:
val myDouble: Double = 0
val myDoubleAs64BitString = "00000000"+
"00000000"+
"00000000"+
"00000000"+
"00000000"+
"00000000"+
"00000000"+
"00000000"
Run Code Online (Sandbox Code Playgroud)
这可能听起来很疯狂,但基本上我正在做的事情并非如此.我基本上是在编写一个Chess应用程序,我需要一种在测试位板表示时获得反馈的好方法.
我有一个 HTTP Spring Security 配置,当我注释掉每个单独的方面时它似乎可以工作,但是当我将 Spring Security 规则组合在一起时它不起作用,所以我知道问题不在于 theregexMatcher或 the,antMatcher而是应用了规则组合。
这是我的 Spring Security 课程:
package com.driver.website.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.security.web.header.writers.StaticHeadersWriter;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
import org.springframework.security.web.util.matcher.RequestMatcher;
import javax.servlet.http.HttpServletRequest;
import java.security.AccessControlContext;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${widget.headers.xframeoptions.domains.allowed}")
private String allowedXFrameOptions;
@Value("${widget.headers.origins.allowed}")
private String allowedOrigins;
@Override
public void configure(HttpSecurity http) throws Exception …Run Code Online (Sandbox Code Playgroud) 我有一个使用HikariCP jdbc连接池运行的Play应用程序.
该应用程序将运行一段时间,但在很短的时间后它将关闭连接池,这意味着我不能再使用该应用程序.
SBT建设:
name := "virtual-betting"
version := "1.0"
lazy val root = (project in file(".")).enablePlugins(PlayJava).settings(javacOptions ++= Seq("-source", "1.8", "-target", "1.8"))
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
javaJdbc,
javaJpa,
javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.1-api"),
"org.hibernate" % "hibernate-core" % "5.1.0.Final",
"org.hibernate" % "hibernate-entitymanager" % "5.1.0.Final",
"com.typesafe.play" % "play-java-jpa_2.11" % "2.5.3",
"org.springframework" % "spring-context" % "4.2.4.RELEASE",
"org.webjars" % "bootstrap" % "3.3.4",
"mysql" % "mysql-connector-java" % "5.1.42",
cache,
javaWs,
"org.jsoup" % "jsoup" % "1.7.2",
"org.apache.commons" % "commons-email" % "1.4",
"org.apache.cxf" % "cxf-rt-rs-client" % "3.1.6",
"com.google.code.gson" …Run Code Online (Sandbox Code Playgroud) java ×3
closures ×1
collectors ×1
hikaricp ×1
java-8 ×1
java-stream ×1
php ×1
phpunit ×1
sbt ×1
scala ×1
spring ×1
unit-testing ×1