(Java) 如何将 CSV 文件的一列映射到不同类型的另一列?
(Java) How to map one column of a CSV file to another column of a different type?
所以对于这个问题,我的函数应该是 return 出场次数最多的前 10 名复仇者名单(在
递减顺序)。
在 Avenger
class 中,它很像基本的 Person
class 但是没有 getter 函数,只有代表他们的姓名、性别、外貌的字段,等等
public class Avenger {
public final String name;
public final int appearances;
public final String gender;
public final int year;
public final String honorary;
public final String[] deaths;
public final String[] returns;
public final String notes;
public Avenger(String name,
int appearances,
String gender,
int year,
String honorary,
String[] deaths,
String[] returns,
String notes) {
this.name = name;
this.appearances = appearances;
this.gender = gender;
this.year = year;
this.honorary = honorary;
this.deaths = deaths;
this.returns = returns;
this.notes = notes;
}
public static Avenger valueOf(String line) {
String[] array = line.split(",");
String name = array[0];
int appearances = Integer.valueOf(array[1]);
String gender = array[2];
int year = Integer.valueOf(array[3]);
String honorary = array[4];
String[] deaths = new String[5];
String[] returns = new String[5];
String notes = array[15];
int index = 5;
int i = 0;
while (index < 15) {
deaths[i] = array[index++];
returns[i] = array[index++];
i++;
}
return new Avenger(name, appearances, gender, year, honorary, deaths, returns, notes);
}
@Override
public String toString() {
return "Avenger [name=" + name + ", appearances=" + appearances + ", gender=" + gender + ", year=" + year
+ ", honorary=" + honorary + ", deaths=" + Arrays.toString(deaths) + ", returns="
+ Arrays.toString(returns) + ", notes=" + notes + "]";
}
}
我的函数是获取顶部 10 APPEARANCES
,它 returning 的数字是正确的。
static Function<Stream<Avenger>, List<String>> getTop10ByAppearances = a ->
a.map(s -> s.appearances).sorted((x, y) -> y - x).limit(10)
.map(p -> p.toString()).collect(Collectors.toList());
但是,我不需要它 return 数字,我需要它 return 与那些数字相对应的 NAMES
。所以我的问题是如何将我这里的内容映射到名称,然后 return 这些名称的 List<String>
?
我认为只删除第一个 map
操作并像这样执行比较就足够了
sorted(Comparator.comparingInt((Avenger x) -> x.appearances).reversed())
然后,您可以在代码中保留您所做的事情。您限制前 10 个结果,将每个复仇者映射到他们的名字,最后收集结果。
static Function<Stream<Avenger>, List<String>> getTop10ByAppearances = a ->
a.sorted(Comparator.comparingInt((Avenger x) -> x.appearances).reversed())
.limit(10)
.map(p -> p.name)
.collect(Collectors.toList());
所以对于这个问题,我的函数应该是 return 出场次数最多的前 10 名复仇者名单(在
递减顺序)。
在 Avenger
class 中,它很像基本的 Person
class 但是没有 getter 函数,只有代表他们的姓名、性别、外貌的字段,等等
public class Avenger {
public final String name;
public final int appearances;
public final String gender;
public final int year;
public final String honorary;
public final String[] deaths;
public final String[] returns;
public final String notes;
public Avenger(String name,
int appearances,
String gender,
int year,
String honorary,
String[] deaths,
String[] returns,
String notes) {
this.name = name;
this.appearances = appearances;
this.gender = gender;
this.year = year;
this.honorary = honorary;
this.deaths = deaths;
this.returns = returns;
this.notes = notes;
}
public static Avenger valueOf(String line) {
String[] array = line.split(",");
String name = array[0];
int appearances = Integer.valueOf(array[1]);
String gender = array[2];
int year = Integer.valueOf(array[3]);
String honorary = array[4];
String[] deaths = new String[5];
String[] returns = new String[5];
String notes = array[15];
int index = 5;
int i = 0;
while (index < 15) {
deaths[i] = array[index++];
returns[i] = array[index++];
i++;
}
return new Avenger(name, appearances, gender, year, honorary, deaths, returns, notes);
}
@Override
public String toString() {
return "Avenger [name=" + name + ", appearances=" + appearances + ", gender=" + gender + ", year=" + year
+ ", honorary=" + honorary + ", deaths=" + Arrays.toString(deaths) + ", returns="
+ Arrays.toString(returns) + ", notes=" + notes + "]";
}
}
我的函数是获取顶部 10 APPEARANCES
,它 returning 的数字是正确的。
static Function<Stream<Avenger>, List<String>> getTop10ByAppearances = a ->
a.map(s -> s.appearances).sorted((x, y) -> y - x).limit(10)
.map(p -> p.toString()).collect(Collectors.toList());
但是,我不需要它 return 数字,我需要它 return 与那些数字相对应的 NAMES
。所以我的问题是如何将我这里的内容映射到名称,然后 return 这些名称的 List<String>
?
我认为只删除第一个 map
操作并像这样执行比较就足够了
sorted(Comparator.comparingInt((Avenger x) -> x.appearances).reversed())
然后,您可以在代码中保留您所做的事情。您限制前 10 个结果,将每个复仇者映射到他们的名字,最后收集结果。
static Function<Stream<Avenger>, List<String>> getTop10ByAppearances = a ->
a.sorted(Comparator.comparingInt((Avenger x) -> x.appearances).reversed())
.limit(10)
.map(p -> p.name)
.collect(Collectors.toList());