如果我有一个vararg Java方法foo(Object ...arg)
并且我打电话foo(null, null)
,我有两个arg[0]
和arg[1]
作为null
s.但如果我打电话foo(null)
,arg
本身就是空的.为什么会这样?
我怎么称呼foo
这样foo.length == 1 && foo[0] == null
的true
?
JDK 正在引入一个Stream.toList()
带有JDK-8180352的 API 。这是一个基准代码,我试图将其性能与现有的进行比较Collectors.toList
:
@BenchmarkMode(Mode.All)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 20, time = 1, batchSize = 10000)
@Measurement(iterations = 20, time = 1, batchSize = 10000)
public class CollectorsVsStreamToList {
@Benchmark
public List<Integer> viaCollectors() {
return IntStream.range(1, 1000).boxed().collect(Collectors.toList());
}
@Benchmark
public List<Integer> viaStream() {
return IntStream.range(1, 1000).boxed().toList();
}
}
Run Code Online (Sandbox Code Playgroud)
结果总结如下:
Benchmark Mode Cnt Score Error Units
CollectorsVsStreamToList.viaCollectors thrpt 20 17.321 ± 0.583 ops/s
CollectorsVsStreamToList.viaStream thrpt 20 23.879 ± 1.682 ops/s
CollectorsVsStreamToList.viaCollectors avgt 20 0.057 ± …
Run Code Online (Sandbox Code Playgroud) 我创建了一个 for every 循环,并获得了价格代码列表,但我想在不使用任何循环的情况下获得相同的东西,并使用 java8 执行此操作供您参考,我发布了我的旧代码。我只想更改代码的这个位置。
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
List<String> priceCodeList = new ArrayList<String>();
for (ItemPriceCode ipc : itemPriceCodes) {
//get the string value from the list
priceCodeList.add(ipc.getPriceCode());
}
Run Code Online (Sandbox Code Playgroud)
但我不想使用任何循环我想在不使用任何循环的情况下获得相同的结果。为了解决这个问题,我尝试了这种方法,但没有取得任何成功。
itemPriceCodes.stream().map(n -> String.valueOf(n)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这是我的这个函数的完整代码
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
List<String> priceCodeList = new ArrayList<String>();
for (ItemPriceCode ipc : itemPriceCodes) {
//get the string value from the list
priceCodeList.add(ipc.getPriceCode());
}
//pass this string value in query
List<ManufacturerPriceCodes>mpc = manufacturerPriceCodesRepository.
findByManufacturerIDAndPriceCodeInAndRecordDeleted(item.getManufacturerID(),priceCodeList,NOT_DELETED);
//Convert list to map
Map<String, ManufacturerPriceCodes> …
Run Code Online (Sandbox Code Playgroud)