在查看Integer.parseInt(String s, int radix)(java 8,1.8.0_131)的源代码时,我发现了以下注释块:
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
Run Code Online (Sandbox Code Playgroud)
虽然我理解了关于IntegerCache的第一部分,但我不明白为什么会出现这样的警告valueOf,以及为什么会出现这种情况.
我看到这valueOf()依赖parseInt(),但我仍然不明白为什么会有这个警告.
有人可以解释一下这个评论的确切含义(以及不应该使用valueOf的上下文),以及可能出现的问题.
编辑:
Integer.valueOf(int i)中的代码似乎已经改变,因为下面的评论中的其他问题被问到,现在是
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Run Code Online (Sandbox Code Playgroud)
并且应该从之前的断言错误中保存.
我有一个奇怪的问题,urlencoding加号+作为针对API的请求的查询参数.API的文档说明:
日期必须采用W3C格式,例如'2016-10-24T13:33:23 + 02:00'.
到目前为止一直很好,所以我使用这个代码(minimalized)生成url,使用Spring的UriComponentBuilder:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX");
ZonedDateTime dateTime = ZonedDateTime.now().minusDays(1);
String formated = dateTime.format(formatter);
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUrl);
uriComponentsBuilder.queryParam("update", formated);
uriComponentsBuilder.build();
String url = uriComponentsBuilder.toUriString();
Run Code Online (Sandbox Code Playgroud)
未编码的查询将如下所示:
https://example.com?update=2017-01-05T12:40:44+01
Run Code Online (Sandbox Code Playgroud)
编码的字符串导致:
https://example.com?update=2017-01-05T12:40:44%2B01
Run Code Online (Sandbox Code Playgroud)
这是(恕我直言)一个正确编码的查询字符串.请参阅%2B替换查询字符串末尾的+in +01.
但是,现在,当我使用编码的url针对API发送请求时,我收到一条错误消息,指出无法处理请求.
但是,如果我在发送请求之前将其替换为%2Ba +,则可以:
url.replaceAll("%2B", "+");
Run Code Online (Sandbox Code Playgroud)
从我的理解,+标志是一个替代whitespace.因此,解码后服务器真正看到的URL必须是
https://example.com?update=2017-01-05T12:40:44 01
Run Code Online (Sandbox Code Playgroud)
我对这个假设是对的吗?
有什么我可以做的,除了联系API的所有者使其使用正确编码的查询,除了奇怪的非标准字符串替换?
更新:
根据规范RFC 3986(第3.4节),+查询参数中的符号不需要编码.
3.4.询问
查询组件包含非分层数据,与路径组件(第3.3节)中的数据一起用于标识
URI方案和命名权限
(如果有)范围内的资源.查询组件由第一个
问号("?")字符表示,并以数字符号("#")字符
或URI的末尾结束.伯纳斯 - 李等人.标准跟踪[第23页] RFC 3986 URI通用语法
2005年1月Run Code Online (Sandbox Code Playgroud)query = …
我需要创建一个包含大约 300 个值的枚举,并且能够通过 id (int) 获取其值。我目前有这个:
public enum Country {
DE(1), US(2), UK(3);
private int id;
private static Map<Integer, Country> idToCountry = new HashMap<>();
static {
for (Country c : Country.values()) {
idToCountry.put(c.id, c);
}
}
Country(int id) {
this.id = id;
}
public static Country getById(int id) {
return idToCountry.get(id);
}
}
Run Code Online (Sandbox Code Playgroud)
该枚举将被大量使用,所以我想知道这是否是性能方面的最佳解决方案。
我一遍又一遍地阅读了http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html,但找不到描述何时
static {
}
Run Code Online (Sandbox Code Playgroud)
块被调用,并且如果保证这只会被调用一次。所以——是吗?
我试图从a中加总多个BigDecimals List.目前,我正在使用两个流,但是如果可能的话,我希望只有一个流.我不确定如何以高效的方式重写下面的内容.
BigDecimal totalCharges = tableRowDataList.stream()
.map(el -> el.getSums().getCharges())
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalFees = tableRowDataList.stream()
.map(el -> el.getSums().getFees())
.reduce(BigDecimal.ZERO, BigDecimal::add);
Run Code Online (Sandbox Code Playgroud)
如您所见,流基本上是相同的,只有对getCharges/getFees的调用不同.
Map<String, BigDecimal>从上面获得结果的最佳方法是什么?(关键是收费/费用)