我有HashMap以下结构。
Map<String, Container>
本Container类包含一个List。我想清除此列表的内容,以便该列表存在但具有0个元素。稍后,我将再次输入值。
在replaceAll()期望一个BiFunction。由于这个原因,以下给出编译错误,因为返回类型clear()为void:
personMap.replaceAll((k,v) -> v.getMyList().clear());
我有以下问题。有两个大小相等的整数列表a和b。现在,我想比较两个列表值在相同的索引处。我的函数返回带有2个值的列表。第一个值是列表a中更大的值计数。第二个值是列表b中更大的值计数。如果两者的值相同,则计数不增加。
我可以一些想法如何使用流/过滤器/谓词在java8中实现
我已经在较旧的Java版本中重新运行了该方法。
static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
List<Integer> list = new ArrayList<Integer>();
int asize = 0;
int bsize = 0;
for (int i = 0; i <b.size();i++) {
if(a.get(i) > b.get(i)) {
asize++;
}else if(a.get(i) < b.get(i)) {
bsize++;
}
}
list.add(asize);
list.add(bsize);
return list;
}
Run Code Online (Sandbox Code Playgroud)
例1。输入:a = 5 6 7 b = 3 6 10输出:1 1例如2.输入:a = 17 28 30 b = 99 16 8输出:2 1
我想返回所有字符串的字符串排列。当我使用下面的代码时,我得到了java.lang.IllegalStateException: stream has already been operated upon or closed。您能告诉我这种方法有什么问题吗?
public Stream<String> getMyPatterns(
Stream<String> s1,
Stream<String> s2,
Stream<String> s3) {
List<String> out = new ArrayList<String>();
s1.collect(Collectors.toList()).forEach(item1 -> {
s2.collect(Collectors.toList()).forEach(item2 -> {
s3.collect(Collectors.toList()).forEach(item3 -> {
out.add(item1 + "." + item2 + "." + item3);
});
});
});
return out.stream();
}
Run Code Online (Sandbox Code Playgroud)
样品测试
Stream<String> patterns = getMyPatterns(
Stream.of("a1", "a2"),
Stream.of("b1", "b2"),
Stream.of("c1", "c2"));
Run Code Online (Sandbox Code Playgroud) 我有2个List
List<Obligation>,List<ObligationStatus>
结构如下:
public class Obligation {
private String topic;
private String status;
private String comment;
private String text;
}
Run Code Online (Sandbox Code Playgroud)
和
public class ObligationStatus {
private String topic;
private String status;
private String comment;
}
Run Code Online (Sandbox Code Playgroud)
status并且comment里面List<Obligation>的null所有元素,
topic填充在这两个list
我想设置status并comment从每个元素List<ObligationStatus>到的每个元素List<Obligation>基础上topic。
// this is what i have tried, and is working fine
obList.stream().forEach(ob -> {
osList.stream().forEach(os -> {
if (ob.getTopic().equalsIgnoreCase(os.getTopic())) { …Run Code Online (Sandbox Code Playgroud) 如果我有两个对象列表,则可以找到以下交集:
public class MyObject {
String id;
String someField;
String someOtherField;
}
List<MyObject> list1;
List<MyObject> list2;
List<MyObject> intersect = list1.stream()
.filter(list2::contains)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
有类似的方法可以根据的id场找到交点MyObject吗?我无法覆盖equals方法。
我有一个DoubleStream从非常耗时的函数计算出来的值,我想同时计算其元素的平均值和计数。
问题是我不想计算DoubleStream两次,因为提到了耗时的值计算。我想从一个lambda表达式中获得平均值和计数的值。
我已经试过各种与collect和map等,但没有成功。
final long count = products.stream()
.mapToDouble(this::timeConsumingCalculateRating)
.filter(rating -> rating > 0.0D)
.count();
final double averageRating = products.stream()
.mapToDouble(this::timeConsumingCalculateRating)
.filter(rating -> rating > 0.0D)
.average()
.orElse(0.0D);
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助。
假设我有一个pojo Medicine,
class Medicine{
private String xmedicine;
private String ymedicine;
private String zmedicine;
//getter and setter here....
}
Run Code Online (Sandbox Code Playgroud)
如果Medicine不为null,则用于收集药物的API下方。
public List<Map<String, Object>> gettMedicine(Medicine medicine) {
List<Map<String, Object>> detailsList = null;
List<Integer> list = new ArrayList<>();
if(null != medicine.getXmedicine()){
list.add(medicine.getXmedicine());
}
if(null != medicine.getYmedicine()){
list.add(medicine.getYmedicine());
}
if(null != medicine.getZmedicine()){
list.add(medicine.getZmedicine());
}
if(!CollectionUtils.isEmpty(list)) {
detailsList = //calling other API to get details from list;
}
return detailsList;
}
Run Code Online (Sandbox Code Playgroud)
最好在Optional这里使用Java 8 来消除,NullPointerException因为我有多个空检查使我的代码样本化。
如果是,那么您能建议我Optional在这里使用吗?
此方法比较两个字符串。其中之一来自物体。如果字符串匹配,则ID由对象返回。
private static Long getDefaultKag(Long mandandId) {
List<MandantKagAccountEntity> mandantKagAccountEntities = new MandantKagAccountManager().findAllKags(mandandId);
for (MandantKagAccountEntity mandantKagAccountEntity : mandantKagAccountEntities) {
if (mandantKagAccountEntity.getKagText().equals("Default_kag")) {
return mandantKagAccountEntity.getMandantKagId();
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以解决流?我的方法,但我无能为力了。
private static long getDefaultKag(Long mandandId) {
return new MandantKagAccountManager().findAllKags(mandandId).stream()
.filter(m -> m.getKagText().equals("Default_Kag"))
...
...
...
}
Run Code Online (Sandbox Code Playgroud)
你有解决的办法吗?我还想知道两个变体中的哪个变体对于大量数据更有效。
我有这样的数组
String arr[][] = {{"abc"}, {"bcd"}, {null}}
Run Code Online (Sandbox Code Playgroud)
这是多维数组(在数组中带有的单字符串数组)。我想删除这些空值,并希望最终结果为{{"abc"}, {"bcd"}}。此数组可以是任何大小,并且可以有任意数量的null
我尝试过这样的事情。我知道我可以使用传统的for循环,但是我想使用java8或更有效地做到这一点。
String arr1[][] = Arrays.stream(arr)
.filter(str -> (((str != null) && (str.length > 0))))
.toArray(String[][]::new);
Run Code Online (Sandbox Code Playgroud) 正如我List的主题
"Subjects":[
{"subject":"Math","grades":1},
{"subject":"Math","grades":2},
{"subject":"Math","grades":3},
{"subject":"Math","grades":3},
{"subject":"Lab","grades":10},
{"subject":"Lab","grades":12}
]
Run Code Online (Sandbox Code Playgroud)
我想这样分组并减少结果
//Expected Result
"Subjects":[
{"subject":"Math","grades":[1,2,3]},
{"subject":"Lab","grades":[10,12]}
]
Run Code Online (Sandbox Code Playgroud)
我很好奇如何映射和减少java8风格的对象。我在下面放置了过时的代码。
我的主班
public static void main(String[] args) {
List<Subject> list = new ArrayList<>();
list.add(new Subject("Math",1));
list.add(new Subject("Math",2));
list.add(new Subject("Math",3));
list.add(new Subject("Math",3));
list.add(new Subject("Lab",10));
list.add(new Subject("Lab",12));
Map<String, Set<Integer>> result = new HashMap<>();
list.stream().forEach(subjects-> {
if(result.get(subjects.getSubject())==null){
Set<Integer> set = new HashSet<>();
set.add(subjects.getGrades());
result.put(subjects.getSubject(),set );
}else{
Set<Integer> set =result.get(subjects.getSubject());
set.add(subjects.getGrades());
result.put(subjects.getSubject(), set);
}
});
result.forEach((key,val)->{
System.out.println("KEY:"+key + " RESULT :"+val);
});
}
public class Subject …Run Code Online (Sandbox Code Playgroud)