简体   繁体   English

(Java)如何将 CSV 文件的一列 map 转换为不同类型的另一列?

[英](Java) How to map one column of a CSV file to another column of a different type?

So for this problem, my function is supposed to return the top-10 list of avengers with the most appearances (in decreasing order).所以对于这个问题,我的 function 应该会返回出场次数最多的前 10 名复仇者名单(按降序排列)。
In the Avenger class it is much like a basic Person class however there are no getter functions, just fields that represent their name, gender, appearances, etc.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 + "]";
    }
}

My function is getting the top 10 APPEARANCES , and the numbers it is returning are correct.我的 function 获得了前10 APPEARANCES APPEARANCES ,并且它返回的数字是正确的。

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());

However, I don't need it to return the numbers, I need it to return the NAMES that correspond to those numbers.但是,我不需要它来返回数字,我需要它来返回与这些数字对应的NAMES So my question is How can I map what I have here to the names and then return the List<String> of those names?所以我的问题是我怎样才能 map 我在这里的名字然后返回这些名字的List<String>

I think it should be enough to only remove the first map operation and perform the comparing like so我认为只删除第一个map操作并像这样执行比较就足够了

sorted(Comparator.comparingInt((Avenger x) -> x.appearances).reversed())

Then, you sort of keep things how you've done in your code.然后,您可以在代码中保留您所做的事情。 You limit the top 10 results, map each avenger to their name and finally collect the results.你限制前 10 个结果,map 每个复仇者到他们的名字,最后收集结果。

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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java将基于列名而不是按列索引的特定列从一个CSV文件转换为另一个CSV - How To Transfer specific Columns based on column name not by column index From One CSV File Into Another CSV Using Java 如何在JAVA中知道我的CSV文件列的数据类型 - How to know the data type of my CSV file column in JAVA 如何在Java中将一个csv文件与另一个稍有不同的数据映射? CSV1具有Fname,Lname,重量,职业。 CSV2具有名称,重量,职业 - How to map one csv file with another of slightly different data in Java? CSV1 has Fname, Lname, weight, occupation. CSV2 has Name, weight, occupation 是否可以保存几个列表,每个列表都在CSV文件的另一列中? JAVA - Is it possible to save few list, each one is in another column in CSV file? JAVA 如何按 Java 中的特定列对 csv 文件进行排序 - How to sort a csv file by a specific column in Java 如何在Java中消除选项卡式列CSV文件 - How to eliminate tabbed column csv file in Java 在 Java 中创建 UDF 以将一个数据框列映射到另一个 - Creating a UDF in Java to map one dataframe column to another OpenCSV-在添加列时将一个csv文件复制到另一个 - OpenCSV - copying one csv file to another while adding a column 在csv文件中搜索列词,并用另一个值java替换它 - search a column word in csv file and replace it by another value java 在Java中以升序对csv文件中的一列数据进行排序 - Sort one column of data in a csv file in ascending order in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM