我正在寻找最快的方法来确定一个long值是否是一个完美的正方形(即它的平方根是另一个整数):
Math.sqrt()
函数以简单的方式完成了它,但我想知道是否有办法通过将自己限制为仅整数域来更快地完成它.这是我现在正在做的非常简单直接的方式:
public final static boolean isPerfectSquare(long n)
{
if (n < 0)
return false;
long tst = (long)(Math.sqrt(n) + 0.5);
return tst*tst == n;
}
Run Code Online (Sandbox Code Playgroud)
注意:我在许多Project Euler问题中使用此函数.因此,没有其他人必须维护此代码.而这种微优化实际上可以产生影响,因为部分挑战是在不到一分钟的时间内完成每个算法,并且在某些问题中需要将此函数调用数百万次.
我尝试过不同的问题解决方案:
0.5不需要添加Math.sqrt()的结果,至少在我的机器上没有.Math.sqrt().这可能是因为Math.sqrt()使用类似牛顿方法的东西,但在硬件中实现,因此它比Java快得多.此外,牛顿的方法仍然需要使用双打.Math.sqrt().or在C++中使用语句比使用语句更快switch,但在Java和C#中,or和之间似乎没有区别switch.or我会说,而不是开关或声明if(lookup[(int)(n&0x3F)]) { test } else return false; …我正在尝试以最简单的方式定义自己的异常类,这就是我得到的:
public class MyException extends Exception {}
public class Foo {
public bar() throws MyException {
throw new MyException("try again please");
}
}
Run Code Online (Sandbox Code Playgroud)
这就是Java编译器所说的:
cannot find symbol: constructor MyException(java.lang.String)
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,这个构造函数必须继承自己java.lang.Exception,不是吗?
我SecurityScheme对 java SpringBoot RESTful 应用程序使用 springdoc-openapi有以下定义:
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components().addSecuritySchemes("bearer-jwt",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")
.in(SecurityScheme.In.HEADER).name("Authorization")))
.info(new Info().title("App API").version("snapshot"));
}
Run Code Online (Sandbox Code Playgroud)
是否可以将其全局应用于所有路径,而不必在代码中的任何地方添加@SecurityRequirement注释@Operation?
如果是,如何向不安全的路径添加排除项?
我用过
implementation group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.2'
Run Code Online (Sandbox Code Playgroud)
此依赖项不存在 setSSLSocketFactory() 方法
java httpclient ssl-certificate spring-boot apache-httpclient-5.x
我正在尝试从Guava迁移到Java 8 Streams,但无法弄清楚如何处理iterables.这是我的代码,从iterable中删除空字符串:
Iterable<String> list = Iterables.filter(
raw, // it's Iterable<String>
new Predicate<String>() {
@Override
public boolean apply(String text) {
return !text.isEmpty();
}
}
);
Run Code Online (Sandbox Code Playgroud)
注意,这是一个Iterable,而不是一个Collection.它可能包含无限量的项目,我无法将其全部加载到内存中.什么是我的Java 8替代品?
顺便说一下,使用Lamba,这段代码看起来会更短:
Iterable<String> list = Iterables.filter(
raw, item -> !item.isEmpty()
);
Run Code Online (Sandbox Code Playgroud) 我正在使用 Java 11、Spring Boot 2.1.1 和 Apache CXF 3.2.7 来公开导入 XSD 架构的 SOAP Web 服务。在 WSDL 中,它显示如下:
<wsdl:import location="http://localhost:9000/endpoint/ws?wsdl=WS_endpointSoapPort.wsdl" namespace="http://test.com"> </wsdl:import>
Run Code Online (Sandbox Code Playgroud)
当我发送查询时,它失败并显示以下堆栈:
2018-12-31 12:05:54,908 ERROR se.[Tomcat].[localhost].[/].[CXFServlet]: 175 - Servlet.service() for servlet [CXFServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: org.codehaus.stax2.ri.EmptyIterator.getInstance()Lorg/codehaus/stax2/ri/EmptyIterator;
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
我正在尝试为我的 Java 代码生成 OpenAPI(版本 3.0.1)规范。为了实现这一点,我使用 Swagger Annotations(版本 2.0.8)和 Swagger Maven 插件。
我对枚举有疑问。比如说,我有两个方法返回完全相同的枚举。在 OpenAPI 规范中,我希望有这个 Enum 的单一模式定义,并且$ref在两个 API 操作中都有此枚举和链接的单一架构定义。但我在每个 API 操作中都有重复的 Enum 定义。如何在不手动编辑规范文件的情况下避免这种重复?
下面是 Java 代码,其中两个方法返回相同的 Enum:
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/properties")
@Produces("application/json")
public class TestService {
@GET
@Path("/enum1")
@Operation
@ApiResponses({
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Color.class)))
})
public Color getColor1() {
throw new UnsupportedOperationException();
}
@GET
@Path("/enum2")
@Operation
@ApiResponses({
@ApiResponse(responseCode = "200", …Run Code Online (Sandbox Code Playgroud) 我必须升级几个包才能通过 whitesource 安全扫描,现在升级了依赖项HttpMessageConverter,以前拦截和构建响应的自定义不再有效。相关的依赖升级如下所示。
Tomcat 嵌入核心 8.5.50 -> 9.0.30
Spring Cloud 合约发布 2.0.1.RELEASE-> 2.0.6.RELEASE
Spring Boot 版本 2.0.4.RELEASE -> 2.0.6.RELEASE
杰克逊数据绑定 2.9.6 -> 2.10.0.pr1
杰克逊核心:2.10.1 -> 2.10.0.pr1
这是以前工作的自定义 HttpMessageConverter:
private class JsonApiHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
JsonApiHttpMessageConverter() {
super(MediaType.valueOf(ResponseType.MEDIA_TYPE_JSON_API));
}
@Override
protected boolean supports(final Class<?> clazz) {
return clazz == HttpErrorResponse.class;
}
@Override
protected Object readInternal(final Class<?> clazz, final HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
return null;
}
@Override
protected void writeInternal(final Object o, final HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException …Run Code Online (Sandbox Code Playgroud) 在我们的开发环境中,我们使用 docker-compose 文件管理 20 多个容器,但有时其中一个容器从我们的网络获取一个 IP,导致我们失去连接。发生这种情况时,我们手动关闭容器和网络。
当我们添加更多容器时,有没有办法配置 docker 以避免使用特定的 IP 地址或子网?
我们的 Docker 版本是:
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:45:36 2020
OS/Arch: linux/amd64
Experimental: false
Run Code Online (Sandbox Code Playgroud)
我们的 docker-compose 版本是:
docker-compose version 1.25.4, build 8d51620a
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
Run Code Online (Sandbox Code Playgroud)
主机操作系统是:
Linux lin_env_int_2 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux(Ubuntu 18.04.4 LTS)
随着内存不足,我正在尝试增加spring工具套件的堆大小(内存)。
如果我从Xmx从768m增加到1024m,则会收到错误“无法创建Java虚拟机”。
如果我将Xmx恢复为768m,则可以正常工作,但出现OOM(内存不足)的情况。我有STS 2.9.2。操作系统是win7。
目前,这是我目前在sts.ini中所拥有的。
-startup
plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.2.R36x_v20101222
-product
com.springsource.sts.ide
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
384M
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms256m
-Xmx1024m
-Xss1m
-XX:PermSize=256m
-XX:MaxPermSize=1024m
Run Code Online (Sandbox Code Playgroud)
我应该改变什么?请指教。
java ×8
spring ×3
openapi ×2
swagger ×2
cxf ×1
docker ×1
eclipse ×1
exception ×1
guava ×1
heap-memory ×1
httpclient ×1
inheritance ×1
jackson ×1
java-8 ×1
java-stream ×1
json ×1
math ×1
optimization ×1
soap ×1
spring-boot ×1
spring-mvc ×1
springdoc ×1
stax ×1
web-services ×1