我有一个这样的班...
class Data { int id, int parent}
Run Code Online (Sandbox Code Playgroud)
还有像这样的对象列表......
List<Data> dataList = new ArrayList();
dataList.add(new Data(1,0));
dataList.add(new Data(2,1));
dataList.add(new Data(3,1));
dataList.add(new Data(4,2));
dataList.add(new Data(5,2));
Run Code Online (Sandbox Code Playgroud)
我想使用流媒体 API 将我的列表收集到一个
Map<Data, List<Data>>
Run Code Online (Sandbox Code Playgroud)
其中地图的键是父级,值是子级。
任何帮助将不胜感激。谢谢
有什么方法可以使用 Lambda Stream 基于 Set 过滤流或列表中的 Set 字段?
例子:
List<Person> persons = new ArrayList<Person>();
Set<String> hobbySet1 = new HashSet<String>();
hobbySet1.add("H1");
hobbySet1.add("H2");
hobbySet1.add("H3");
Set<String> hobbySet2 = new HashSet<String>();
hobbySet2.add("H2");
hobbySet2.add("H4");
Set<String> hobbySet3 = new HashSet<String>();
hobbySet3.add("H3");
Set<String> hobbySet4 = new HashSet<String>();
hobbySet4.add("H4");
persons.add(new Person("P1", hobbySet1));
persons.add(new Person("P2", hobbySet2));
persons.add(new Person("P3", hobbySet3));
persons.add(new Person("P4", hobbySet4));
Set<String> searchHobby = new HashSet<String>();
searchHobby.add("H1");
searchHobby.add("H3");
Run Code Online (Sandbox Code Playgroud)
我想从基于 searchHobby 的 List 人员中过滤爱好,以便只保留那些在 searchHobby 中指定了他们的爱好的人。
我知道如何使用简单的 for 循环来实现这一点。
List<Person> result = new ArrayList<Person>();
for (String sHobby : searchHobby) …Run Code Online (Sandbox Code Playgroud) 我想将任务列表映射到List<Callable<Tasks>>:
List<Callable<Tasks>> callableTasks = tasks.stream().map(t ->
{
// how to return callable directly and omitting curly braces?
Callable<Task> task = () -> run(t);
return task;
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如何缩短上述表达式以直接返回可调用对象?
我正在学习一些教程,Java Streams看起来所有这些教程都已经过时了(或者显然我不明白)
var list = Arrays.stream(new int[] {1,2,3,4,5,6})
.filter(x -> x > 3)
.collect(toList());
Run Code Online (Sandbox Code Playgroud)
它告诉我这个错误:
Expected 3 arguments but found 1
Run Code Online (Sandbox Code Playgroud)
但是每个教程都使用这样的收集代码。
出了什么问题?
该void accept(T t)方法的目的是什么?它似乎与Stream.Builder<T> add(T t)方法除了返回 void 并且因此不能链接。不返回 Builder 对象是否更有效(似乎次要)?是否存在一些您不希望返回 Builder 的用例?
我在 JDK 文档中没有看到对这个决定的任何解释,也没有在 stackoverflow 上找到这个问题。如果我错过了一些明显的东西,请道歉。
我有一些城市的房屋清单。我正在尝试使用过滤器来生成每个城市最昂贵的房子的列表。我无法使用传统循环。
//This returns unique City Names
List unique = obList.stream()
.map(x -> x.getCity())
.distinct()
.sorted()
.collect(Collectors.toList());
//This returns the house with the highest price in that city
RAddress obReturn = obList.stream()
.filter(x -> x.getCity().equalsIgnoreCase(sName))
.sorted((x,y) -> (int)(y.getPrice() - x.getPrice()))
.findFirst()
.get();
Run Code Online (Sandbox Code Playgroud)
我知道以某种方式将这些结合起来对于解决这个问题是必要的,但我一生都无法弄清楚如何......
任何和所有的帮助表示赞赏。
我正在尝试将 Map> 转换为单行的列表/字符串集,但除非我对地图使用传统的 forEach,否则无法做到这一点。是否有人知道使用流在单行中执行此操作。
import java.util.*;
public class StreamDemo {
public static void main(String[] args) {
HashMap<String, List<String>> map = new HashMap<>();
ArrayList<String> s1 = new ArrayList<>();
map.put("A", Arrays.asList("A1, A2"));
map.put("B", Arrays.asList("B1, B2"));
map.put("C", Arrays.asList("C1, C2"));
Set<String> stringValues = new HashSet<>();
// LOGIC TO GET ALL VALUES A1, A2, B1, B2, C1, C2 INTO stringValues
System.out.println(stringValues);
// Expected output
// [A1, A2, B1, B2, C1, C2]
}
}
Run Code Online (Sandbox Code Playgroud) 如何在java 8中编写以下代码
List<String> names = service.serviceCall();
if(names != null) { // **how to do this null check with java 8**
names.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud) 是否可以在下面的地图中使用 if 语句条件?
public Map<LocalDate, Double> avarageTemperatures(LocalDate dateFrom, LocalDate dateTo) {
Map<LocalDate, Double> tempAverage = Data.stream()
.collect(Collectors.groupingBy(Weather::getDateTime,
Collectors.averagingDouble(Weather::getTemp)));
System.out.println("average: " + tempAverage);
return tempAverage;
}
Run Code Online (Sandbox Code Playgroud)
我想只返回日期和tempAverage对之间的日期datefrom和dateTo,这是用户输入。所以我想实现一个 if 语句
for (Weather weather : Data){
if(!weather.getDateTime().isAfter(dateTo) && !weather.getDateTime().isBefore(dateFrom)){}
}
Run Code Online (Sandbox Code Playgroud)
其中weatherData是包含日期和温度的 ArrayList。我的天气课:
public class Weather {
private LocalDate dateTime;
private LocalTime time;
private double temperature;
private String tag;
//setter, getter, constructor
}
Run Code Online (Sandbox Code Playgroud)
日期格式为 1946-01-15,温度格式为 -0.3。
Javadocs说 distinct() - 返回一个由该流的不同元素(根据 Object.equals(Object))组成的流。
我有一个包含一些重复项的自定义对象列表。当我distinct()在流列表上运行方法时,我仍然得到原始列表。为什么即使我在自定义对象中定义了一个 equals 方法,也没有删除重复项?
代码 :
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class CustomType {
private String data1;
public CustomType(String data1) { this.data1 = data1; }
public String getData1() { return data1; }
@Override
public boolean equals(Object other){
CustomType otherC = (CustomType) other;
return this.getData1().equals(otherC.getData1());
}
@Override
public String toString(){
return "[" + data1 + "]";
}
}
public class StreamDistinctTest {
public static void main(String [] args){
List<CustomType> data = Arrays.asList(
new …Run Code Online (Sandbox Code Playgroud) java ×10
java-stream ×10
collections ×2
filter ×2
foreach ×1
grouping ×1
java-8 ×1
lambda ×1
list ×1
set ×1