我有以下课程:
class Data {
String systemId;
String fileName;
int x;
int y;
Data(String systemId, String fileName, int x, int y) {
this.systemId = systemId;
this.fileName = fileName;
this.x = x;
this.y = y;
}
public String getSystemId() {
return systemId;
}
public void setSystemId(String systemId) {
this.systemId = systemId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = …Run Code Online (Sandbox Code Playgroud) 我有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)
非常感谢您的帮助。
有时,我们倾向于“过度使用”事物。这意味着我们拥有诸如Optional之类的东西,并且到处都可以看到用例。在Optional的情况下,一种常见的情况是将其方法链接起来以达到获得价值的单一目的。避免这种做法,而是依靠简单明了的代码。
这是真的Stream.ofNullable吗?我应该避免这种情况:
Stream.ofNullable(getHandlers(...))
.flatMap(Collections::stream)
.forEach(handler -> handler.handle(event));
Run Code Online (Sandbox Code Playgroud)
赞成吗?
List<Handler> handlers = getHandlers(...)
if (handlers == null) {
return; // do nothing
}
handlers.forEach(handler -> handler.handle(event));
Run Code Online (Sandbox Code Playgroud) 此方法比较两个字符串。其中之一来自物体。如果字符串匹配,则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) java ×10
java-stream ×10
java-8 ×9
list ×3
arrays ×1
intersection ×1
lambda ×1
optional ×1
stream ×1
string ×1