我有一个例子如下.我只想转换由','分隔的字符串,并将其转换为长数组而不使用空字符串.productIdParams包含[1],但是当我执行它时,我得到一个例外.
java.lang上的java.lang.System.arraycopy(Native Method)中的java.lang.ArrayStoreException java.util.stream.Nodes上的java.util.stream.SpinedBuffer.copyInto(SpinedBuffer.java:194).$ SpinedNodeBuilder.copyInto(Nodes.java) :1290)java.util.stream上的java.util.stream.SpinedBuffer.asArray(SpinedBuffer.java:215)java.util.stream.Nodes $ SpinedNodeBuilder.asArray(Nodes.java:1296)java.util.stream.ReferencePipeline.toArray( ReferencePipeline.java:439)
String test = "1,";
String[] productIdParams = Iterables.toArray(com.google.common.base.Splitter.on(",").omitEmptyStrings().split(test), String.class);
try {
Long[] productIds = Arrays.stream(productIdParams).filter(productId -> !productId.isEmpty()).toArray(Long[]::new);
System.out.println(productIds[0]);
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
有什么不对?
谢谢.
我有这段代码.我该如何改造它?功能风格?事实上,我有List<X>.每个X包含List<V>.此列表中的每个V都有List<M>一个参数.我需要构建Map<X,Y>,其中Y是存储在对象X中聚合的所有V对象中的所有M个对象的数量.
HashMap<Country, Integer> modelsPerCountryMap = new HashMap<>();
int count;
for (Country country : CountryDataSingleton.getCountryDataCollection()) {
count = 0;
for (CarMaker cm : country.getListOfMakers()) {
count += cm.getModels().size();
}
modelsPerCountryMap.put(country, count);
}
Run Code Online (Sandbox Code Playgroud) 如何使用Optional和lambda编写下面的逻辑? 我有两个列表
List success = new ArrayList <>();
List failures = new ArrayList <>();
并且有一个对象
RoomTypes值
对于这value.getErrorType()可能是空或不和value.getId()回报整数.
我想更新success,并failure与value.getId()根据列表value.getErrorType()为空.
就像是:
if(value.getErrorType().equals(NULL)){
success.add(value.getId())
}
else{
failure.add(value.getId())
}
Run Code Online (Sandbox Code Playgroud) 我需要比较地图A,地图B中存在的关键字.如果关键字存在于两个地图(A和B)中,则添加该特定关键字和值需要使用lamda表达式添加到新地图c中.请在下面找到我的示例代码:
mapA.forEach((key, value) -> mapC.put(mapB.get(Key), value)));
Run Code Online (Sandbox Code Playgroud)
上面的代码当前不会在添加到mapC之前检查mapA中是否存在key,mapB.
在我的Java应用程序中我有
BlockingQueue<HashMap<Integer, double[]>> q
Run Code Online (Sandbox Code Playgroud)
我该如何克隆它?
我有一段代码:
table.stream().filter(row -> !hash.containsKey(row[keyColumnNumber]))
.map(row -> row[keyColumnNumber]).map(hash::get)
Run Code Online (Sandbox Code Playgroud)
最后一步:.map(hash::get)退货Collection<String[]>.因此我需要将所有这些收集到List.
.collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
返回List>期望的内容,但是
.flatMap(Stream::of).collect(Collectors.toList()).collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
返回相同的结果.
想要在下面的代码段中使用Streams API并删除当前for循环:
public List<MyObject> performSomeAction(){
List<String> myList= Arrays.asList("abc","xyz","def");
List<MyObject> resultList=new ArrayList<MyObject>();
int startIndex=0;
int totalNumOfRecords=1000;
for(int i=0;i<totalNumOfRecords;i++){
SomeObject o= xService.doSomething();
String type = o.getType();
int length = o.getLength();
if(myList.contains(type)){
int[] myarray=getMyArray(startIndex+20, startIndex+length);
String id=o.getSomeId();
OtherObject obj= xService.performOperation(myarray,"abcd");
resultList.add(new MyObject(obj,id));
}
startIndex+=length;
}
return resultList;
}
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
IntStream.range(0,totalNumOfRecords).forEach(i -> xService.doSomething());
Run Code Online (Sandbox Code Playgroud)
但我陷入困境,没有得到进一步的进展,需要从派生对象'o'中取出多个值,并使用它在它上面执行过滤器再次使另一个方法调用得到一些数组,将其传递给其他方法返回我需要创建resultList的其他对象
这听起来很简单,但是我找不到在不使用Java 8+的情况下将String值(可能为null)转换Integer为单行的方法if else.答案可能涉及ofNullable和的使用isPresent.
我试过的一些事情:
String x = ...;
Integer.valueOf(x); // fails if x is null
Optional.ofNullable(Integer.valueOf(x)).orElse(null); // NullPointerException
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法在JAVA 8中编码?
if (info1 != null && info1.getId() != null && info1.getPartyNumber()!= null) {
billing.setSenderId(info1.getId());
billing.setSenderNumber(info1.getPartyNumber());
}
if (info2 != null && info2.getId() != null && info2.getPartyNumber()!= null) {
billing.setReceiverId(info2.getId());
billing.setSellerNumber(info2.getPartyNumber());
}
..
..
Run Code Online (Sandbox Code Playgroud)
提前致谢.注意:我调查Optional.ofNullable()但不确定这是否真的有助于多次检查?
在下面的代码中,我尝试将info方法称为供应商.(info方法重载:一个是String,另一个是供应商.)编译器抱怨"方法info(String)不适用于参数Supplier<Double>".我的期望是通过发送供应商对象来调用info方法来获取供应商.我可以帮助理解这个错误吗?
Supplier<Double> randomSupplier = new Supplier<Double>()
{ public Double get()
{ return Math.random(); }
};
logger.info(randomSupplier); <----
Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×7
java-stream ×4
lambda ×2
optional ×2
clone ×1
collections ×1
conditional ×1
if-statement ×1
integer ×1
null ×1
queue ×1
string ×1
supplier ×1