首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从存储在文件中的行数据中获取不同值

从存储在文件中的行数据中获取不同值
EN

Stack Overflow用户
提问于 2017-07-18 17:57:27
回答 2查看 39关注 0票数 0

我有一个包含如下表格数据的.data文件

Id邮政编码年龄疾病

1 12377 39心脏病

2 12302 32心脏病

3 12378 37心脏病

4 45605 53胃炎

5 45609 62心脏病

6 45606 57癌症

7 78905 40心脏病

8 78973 46癌症

9 78907 42癌症

我想要获取疾病属性的唯一/不同值,并将它们存储在一个数组或列表中。任何人都能帮上忙!

EN

回答 2

Stack Overflow用户

发布于 2017-07-18 19:11:24

PseudoCode:

代码语言:javascript
运行
复制
1.Class Row (Id ZIP-code Age Disease)
2.HashMap <String,ArrayList<Row>>
3.for each line split by comma
3.a Check if column[5] exists in hashmap
    i) if Yes add Row to corresponding arraylist
    ii)if No  Create new arraylist add Row and then add to the HashMap
票数 0
EN

Stack Overflow用户

发布于 2017-07-18 19:44:12

假设您可以读取数据,我可能会在以后使用流api来减少数据集。参见下面“function”中的逻辑。

代码语言:javascript
运行
复制
public class playground {

private static class Row {
    String id;
    String  zip;
    String age;
    String disease;

    public Row(String id, String zip, String age, String disease) {
        this.id = id;
        this.zip = zip;
        this.age = age;
        this.disease = disease;
    }
}


private static Set<String> function(List<Row> rows) {
    return rows.stream().map(row -> {
        return row.disease;
    }).collect(Collectors.toSet());
}

private static List<Row> readDate(String filePath) {

    ArrayList<Row> rows = new ArrayList<Row>();
    try (Stream<String> stream = Files.lines(Paths.get(filePath))) {

        stream.forEach(
                line -> {
                    String[] split = line.split(" ");
                    rows.add(new Row(split[0], split[1], split[2], split[3]));
                }
        );

    } catch (IOException e) {
        e.printStackTrace();
    }
    return rows;
}

public static void main(String[] args) {

    List<Row> rows = readDate("...filepath.data");
    Set<String> unique = function(rows);
    assert(unique.size() == 2);
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45163378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档