我想阅读以下csv文件:
1,2,3
1,2,3
1,2,3
Run Code Online (Sandbox Code Playgroud)
我使用的是Java 8,因此无法实现从List<String[]>到的转换
List<List<Integer>>:
Files.lines(filePath)
.map(line -> line.split(","))
//Here
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我正在尝试将二维int数组转换为单个int,其中每个数字和每个使用流的单个数组之后的换行符之间都有一个空格。
我已经使用mapToObj轻松地对一维数组完成了此操作,但是当流是由数组而不是int组成时,这不是很容易。
int[] a = {1, 2, 3, 4};
String[] strArray = Arrays.stream(a).mapToObj(String::valueOf).toArray(String[]::new);
String joined = String.join(" ", strArray);
Run Code Online (Sandbox Code Playgroud)
a会映射到1 2 3 4这里。我想要的是像
int[][] a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
映射到
1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
如何获得与相似的东西joined,但具有二维数组并且仍在使用流?
我试图以ArrayList<Person>相反的顺序对此进行排序,但这无法编译
List<Person> newList = arrayList.stream()
.sorted(Comparator.reverseOrder(Person::getAge)) //Error
.limit(3)
.collect(Collectors.toList());
newList.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以对流进行反向排序吗?
说我有一张地图清单
List<Map<String, Double>> maps = new ArrayList<>();
Map<String, Double> map1 = new HashMap();
map1.put("height",60D);
map1.put("weight",144D);
maps.add(map1);
Map<String, Double> map2 = new HashMap();
map2.put("height",63D);
map2.put("weight",192D);
maps.add(map2);
Run Code Online (Sandbox Code Playgroud)
等等...
创建身高或体重列表的最快方法是什么?像列表高度之类的东西。
经典的方法是浏览地图,使用if条件并搜索高度键,然后如果找到高度键,则将其添加到返回列表中。
使用流执行此操作的等效方法是什么?
我想同时执行两个流并生成第三个流作为前两个的组合,假设我有以下两个流
Stream<String> a = Stream.of("f","b","z");
Stream<String> b = Stream.of("foo","bar","baz");
Run Code Online (Sandbox Code Playgroud)
我想从这两个中生成一个新的,这相当于这个
Stream<String> c = Stream.of("f:foo","b:bar","z:baz");
Run Code Online (Sandbox Code Playgroud)
到目前为止,我发现的是Stream.concat或Stream.of
Stream<String> result = Stream.concat(a, b); or
Stream<String> result = Stream.of(a, b).flatMap(s -> s);
Run Code Online (Sandbox Code Playgroud)
两者相互依存并与此相对应
Stream.of("f","b","z","foo","bar","baz");
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的解决方法是将两个流的元素收集到一个列表中,并遍历两个列表以连接单个元素并将结果列表转换为流。strem api中是否有较短的方法或现有功能可以同时在两个流上工作?
List<String> aList = a.collect(Collectors.toList());
List<String> bList = b.collect(Collectors.toList());
List<String> result = new ArrayList<>();
for(int i = 0; i< aList.size(); i++){
result.add(aList.get(i)+":"+bList.get(i));
}
Stream<String> cc = result.stream();
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用流从ResultSet中的int列中获取平均值,但是,我要做此工作的唯一方法是首先将数字放入列表中,然后在该列表上使用.stream()。
有没有更直接的方法可以做到这一点?
public void avgSalaryAbove150k(Connection con) {
String sqlQuery = "SELECT * FROM " + schemaName + "."+tableName+" WHERE EMPLOYEE_SALARY>150000";
List<Integer>salaries = new ArrayList<>();
try(
PreparedStatement preparedStatement = con.prepareStatement(sqlQuery);
ResultSet rs = preparedStatement.executeQuery();
){
while(rs.next()) {
int salary = rs.getInt("EMPLOYEE_SALARY");
salaries.add(salary);
}
System.out.println("Now showing average salary of employees with a salary of over 150,000...");
salaries.stream()
.mapToInt((x)->x)
.average()
.ifPresent(System.out::println);
System.out.println("");
}catch(SQLException ex) {
System.out.println("Failed to create the database connection.");
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
该方法目前可以从中使用,但是我有一种明显的感觉,这不是执行此操作的最佳方法。
I want to have a stream where I can put elements after creation:
Stream<Task> all2 = Stream.of();
Run Code Online (Sandbox Code Playgroud)
Because the return type of the method is also a Stream and I want to avoid the extra cost by using a list and the need for return list.stream():
Stream<Task> getAll(){
Stream<Task> all2 = Stream.of();
all2.add(item1);
..
return all2
}
Run Code Online (Sandbox Code Playgroud)
But how can I add elements to all2 for further processing?
Is this the only way to do this:
Stream<Task> all2 = …Run Code Online (Sandbox Code Playgroud) 是否可以通过Stream API从HashMap中创建String [] []?
我试图得到这样的东西:
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3"); =>
=> String[][] 2dArray => [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]
Run Code Online (Sandbox Code Playgroud)
我已经用简单的java做了同样的事情:
Set<Map.Entry<String, String>> entrySet = map.entrySet();
String[][] result = new String[3][2];
int i = 0;
for (Map.Entry<String,String> entry : entrySet){
result[i][0] = entry.getKey();
result[i][1] = entry.getValue();
++i;
}
Run Code Online (Sandbox Code Playgroud) How can I use Java 8 streams to calculate an average of the stream elements? Because I cannot use a stream twice I have to do it in one iteration. The stream contains custom beans holding a float value. The number of elements in the stream may vary. Hence, I want to sum up the float value of all elements in the stream and divide it by the number of elements in the stream.
Update: Finally I need the sum …
我试图找到如何在一个衬板中收集对象列表中每个字段的平均值。
这是我要执行的操作:
public class Value {
int a;
int b;
int c;
// rest of the class
}
Run Code Online (Sandbox Code Playgroud)
现在假设我有 List<Value> values = getMillionValues();
我知道要获得一个字段的平均值,我可以执行以下操作:
int averageOfA = values.stream().mapToInt(Value::getA).average()
Run Code Online (Sandbox Code Playgroud)
为了获得所有值的平均值而又没有上面每个变量的重复行,我该怎么办?
也许还有其他一些库(例如Guava)可以帮助执行此类操作?
java ×10
java-stream ×10
java-8 ×3
list ×2
arrays ×1
average ×1
dictionary ×1
sorting ×1
string ×1