我List的MapS作为
[{"a"="10","b"="20"},{"a"="12","b"="22"},{"a"="14","b"="24"}]
Run Code Online (Sandbox Code Playgroud)
我想Map作为
{"a"=["10","12","14"],"b"=["20","22","24"]}
Run Code Online (Sandbox Code Playgroud) 我在ArrayListas 中有值Value1, Value2, Value3。
现在我需要String像Value1_Value2_Value3使用 Java一样以 Single 的方式返回它们Streams。
我试过这个,但没有用
myList.stream().map((s)->s+"_").toString();
Run Code Online (Sandbox Code Playgroud)
我也尝试过,collect(toList());但我不确定如何进一步将其转换为String.
我该怎么做?
题:
举一个ArrayList的Player秒- List<Player>。以一个Player与POJO String Name,int age和int runs。使用list.add.
找到第一个年龄小于 30 且跑数大于 5000 的玩家并打印姓名。否则打印“没有选择参加世界杯的球员”
POJO类:
public class Player {
String name;
int age;
int runs;
public Player(String name, int age, int runs) {
super();
this.name = name;
this.age = age;
this.runs = runs;
}
@Override
public String toString() {
return "Player [name=" + name + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
逻辑:
List<Player> players = Arrays.asList(
new Player("Sachin",45,10252),
new Player("Virat",29,7000),
new …Run Code Online (Sandbox Code Playgroud) 我有以下 Dto 类:
@AllArgsConstructor
@ToString
@Getter
public class Dto {
private String cityName;
private String personName;
}
Run Code Online (Sandbox Code Playgroud)
我想从上面的 Dtos 列表创建一个 Map<String, List>。逻辑是这样的,所有具有相同城市的Dto都应该以该城市名称作为关键字一起列出。可以使用普通的 foreach 轻松完成,但我想知道这是否可以使用 Java 8 完成。
可以使用computeIfAbsent、computeIfPresent、stream.collect(toMap())等来做些什么吗?
public class Runner {
public static void main(String[] args) {
Map<String, List<Dto>> map = new HashMap<>();
List<Dto> tempList = null;
List<Dto> dtos = Arrays.asList(
new Dto("Indore", "A"),
new Dto("Indore", "B"),
new Dto("Pune", "C"),
new Dto("Pune", "D")
);
for (Dto dto : dtos) {
if(map.containsKey(dto.getCityName())) {
map.get(dto.getCityName()).add(dto);
}
else {
tempList = new …Run Code Online (Sandbox Code Playgroud) 我计划编写一个类,extends java.time.YearMonth目的是YearMonth使用一种方法来扩展LocalDates ,该方法使我能够流式传输其中的s YearMonth:
public class ExtendedYearMonth extends YearMonth {
public Stream<LocalDate> days() {
LocalDate firstOfMonth = this.atDay(1);
LocalDate lastOfMonth = firstOfMonth.with(TemporalAdjusters.lastDayOfMonth());
return firstOfMonth.datesUntil(lastOfMonth);
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,当我发现YearMonth是一个final class? final classes 不可扩展。
我当然可以写一个这样的类
public class ExtendedYearMonth {
private YearMonth yearMonth;
// parameterized constructor, getters and setters omitted for brevity
public Stream<LocalDate> days() {
LocalDate firstOfMonth = this.yearMonth.atDay(1);
LocalDate lastOfMonth = firstOfMonth.with(TemporalAdjusters.lastDayOfMonth());
return firstOfMonth.datesUntil(lastOfMonth);
}
}
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的,因为它需要我将 aYearMonth …
我正在尝试使用 Streams.zip 创建地图。我不关心流返回的结果,我只需要为每个元素执行 lambda。这是我的代码
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var data = new HashMap<String, String>();
var myStream = Streams.zip(Arrays.stream(x), Arrays.stream(y),
(arg1, arg2) -> { data.put(arg1, arg2); return 0; };
var result = myStream.collect(Collectors.toList()); // ideally don't want this line
Run Code Online (Sandbox Code Playgroud)
如果没有 collect() 行,我不会填充地图,那么最简单的方法是什么,我尝试了 count() 而不是 collect ,但这完全是 zip 的快捷方式。或者是否有没有 Streams.zip 的更简单的单行解决方案来从两个列表填充地图?
我有一个简单的类:
public class Thing {
Long id;
String color;
String size;
}
Run Code Online (Sandbox Code Playgroud)
我有两个List<Thing>看起来像这样的对象:
List<Thing> colors = [{2, red}, {3, blue}];
List<Thing> sizes = [{2, small}, {3, large}];
Run Code Online (Sandbox Code Playgroud)
使用 Java 流 / forEach,我如何将一个“流”到另一个中以达到:
List<Thing> things = [{2, red, small}, {3, blue, large}]
Run Code Online (Sandbox Code Playgroud) 例如,当n = 5,我需要生成一个字符串"012345"。
我可以通过for从0to运行循环n并将数字附加到字符串来实现。
for(int i = 0; i <= n; i++) {
s += i;
}
Run Code Online (Sandbox Code Playgroud)
有没有没有for循环的简单方法?也许使用流?
我正在尝试将给定产品的所有价格添加为:
BigDecimal getTotalPrice(List<Product> products) {
return products.stream()
.map(Product::getPrice)
.peek(bigDecimal -> System.out.println(bigDecimal))
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
Run Code Online (Sandbox Code Playgroud)
我试着打印所有的值bigDecimal,每当我们得到的价值null,我们正在java.lang.NullPointerException,有没有另外的什么更好的办法java 8,防止任何类型的异常?
鉴于:
List<String> str = Arrays.asList ("my", "pen", "is", "your", "pen");
Predicate<String> test = s -> {
int i = 0;
boolean result = s.contains ("pen");
System.out.print((i++) + ":");
return result;
};
Run Code Online (Sandbox Code Playgroud)
这打印: 0:0:
str.stream().filter(test).findFirst();
Run Code Online (Sandbox Code Playgroud)
这将打印 0:0:0:0:0:
str.stream().filter(test).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,无论是findFirst还是collect,无论终端操作是否短路,它们都应该遍历列表中的每个项目,对吗?
那么为什么在第一个示例 findFirst 中,“0:”被打印两次,而不是一次,而不是5 次?