我有一個名為 Person 的類,其結構如下:
public class Person {
public final int num;
public final String name;
public final String gender;
public final int age;
public Person (int aNum, String aName, String aGender, int anAge){
this.num = aNum;
this.name = aName;
this.gender = aGender;
this.age = anAge;
}
public static Person lineValues(String line) {
String array = line.split(",");
int numA = array[0];
String nameA = array[1];
String genderA array[2];
int ageA = array[3];
return new Person(numA, nameA, genderA, ageA);
}
}
單個 Person 的資料來自名為 people.csv 的 csv 檔案中的一行:
| Num | Name | Gender |Age |
| --- | ---- | ------ | --- |
| 1 | Fred | Male | 41 |
| 2 | Wilma | Female | 36 |
| 3 | Barney | Male | 38 |
| 4 | Betty | Female | 35 |
這是我的實際問題。我有一個名為 People 的介面,它有一個名為 getGenderCount 的函式。這個函式應該通過一個 Person 物件并檢索每個性別的計數圖。
static Function<Stream<Person>,Map<String,Long>> getGenderCount = null;
我的問題是我無法理解如何流式傳輸整個類物件的正確語法。我事先使用過原始流。執行諸如此類的拆分是.map(x -> x.split(","))行不通的,因為那將是 String 型別而不是 Person。
我提出的解決方案如下所示:
e -> e.map(x -> x.split(","))
.skip(1) // skips the title line
.filter(x -> x.length == 4)
.collect(Collectors.groupingBy(x -> x[2], Collectors.counting()));
但它應該映射一個 Person 物件的資訊。除了流操作和語法之外,我不想更改我目前擁有的任何東西。我想了解如何從 Person 中提取變數并在 Stream 中找到它們。
uj5u.com熱心網友回復:
這會做你想要的嗎?
Files.lines(Paths.get("people.csv"))
.skip(1)
.map(Person::lineValues) // this method must handle "|" divider chars
.collect(Collectors.groupingBy(Person::getGender, Collectors.counting()));
uj5u.com熱心網友回復:
您需要使用通常的方法對流進行操作map,filter或者foreach然后可以使用標準訪問操作。
Stream<Person> people;
people = ...; // initialize stream with some data
// iterate over stream and access each element
people.forEach(person -> { System.out.println(person.name); });
該函式getGenderCount獲取 Person 流作為輸入,并回傳包含性別及其頻率的地圖。
此外,在 Java 中,字串不能直接分配給 int。它需要轉換。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Person {
public final int num;
public final String name;
public final String gender;
public final int age;
public Person(int aNum, String aName, String aGender, int anAge) {
this.num = aNum;
this.name = aName;
this.gender = aGender;
this.age = anAge;
}
public static Person lineValues(String line) {
String array[] = line.split(",");
int numA = Integer.parseInt(array[0]);
String nameA = array[1];
String genderA = array[2];
int ageA = Integer.parseInt(array[3]);
return new Person(numA, nameA, genderA, ageA);
}
static Function<Stream<Person>, Map<String, Long>> getGenderCount = people -> {
return people.collect(Collectors.groupingBy((person -> person.gender), Collectors.counting()));
};
public static void main(String args[]) {
try {
Stream<Person> people = Files.lines(Paths.get("people.csv"))
.map(line -> { System.out.println(line); return line;}) // just to show input
.skip(1) // remove first line
.map(line -> line.substring(line.indexOf("|") 1)) // remove first '|' and in front
.map(line -> line.substring(0, line.lastIndexOf("|"))) // remove last '|' and behind
.map(line -> line.replace(" ", "")) // remove spaces, can cause trouble with spaces within names
.map(line -> line.replace("|", ",")) // change pipe symbols into commas
.map(line -> Person.lineValues(line)); // transform lines into persons
Map<String, Long> genderCount = Person.getGenderCount.apply(people);
System.out.println(genderCount);
} catch (IOException e) {
e.printStackTrace();
}
}
}
生產
$ javac Person.java
$ java Person
| Num | Name | Gender | Age|
| 1 | Fred | Male | 41 |
| 2 | Wilma | Female | 36 |
| 3 | Barney | Male | 38 |
| 4 | Betty | Female | 35 |
{Male=2, Female=2}
$
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/467275.html
