【问题标题】:Getting the highest column value from csv file and storing it into arraylist从 csv 文件中获取最高列值并将其存储到 arraylist
【发布时间】:2019-02-03 10:00:31
【问题描述】:

我正在尝试将 Name 中具有最高 Sub Name 存储到 arraylistONE >管理员值。

在这种情况下,

  1. 通用面粉1 3/4 cups all-purpose flour
  2. 泡打粉1/4 teaspoon baking soda and 1/2 cup of plain
  3. 小苏打8 teaspoons of baking powder

如果一个 Name 只有 0 个 admin 值,它应该是,

  1. Brewed Coffee 它应该始终将第一个值存储在文件中,即1 cup brewed coffee

对于其余具有较小 admin 值的数据,它们将存储在 arraylistTWO 中。

我一直在阅读 csv 文件,我不知道如何将 NameSub Name 存储到 arraylistONE具有最高 admin 值的 em> 和具有较小 admin 值的其余数据> value,我不知道如何存储到 arraylistTWO 中。

这是我迄今为止所做的工作:

try {
    br = new BufferedReader (new FileReader ("/sdcard/TABLE_BF.csv"));
    while ((sCurrentline = br.readLine ()) != null) {
           subIng.add(sCurrentline.split (","));
    }
    arrSubIng = new String[subIng.size ()][];
    subIng.toArray (arrSubIng);
} catch (IOException e) {
        e.printStackTrace ();
}

【问题讨论】:

  • 您在代码中读取两个不同的文件,这与您的问题有什么关系?
  • @JoakimDanielson 我忘了排除它
  • 这个作业有一些限制还是可以使用 Java 8 和流?
  • CSV 文件是否按照您发布的示例数据按 名称 排序?
  • 没有限制。我在我的项目中设置了 minSdkVersion 19 和 targetSdkVersion 27 @JoakimDanielson

标签: java csv arraylist


【解决方案1】:

首先,我认为创建一个简单的类来保存数据是有意义的,因为使用对象而不是值数组来过滤和排序会更容易。

public class Ingredient {
    String name;
    String subName;
    int status;
    int admin;

    public Ingredient(String name, String subName, String status, String admin) {
        this.name = name;
        this.subName = subName;
        this.status = Integer.valueOf(status);
        this.admin = Integer.valueOf(admin);
    }

    public String getName() {
        return name;
    }
    public int getAdmin() {
        return admin;
    }
    //more get and set methods here. I have only included what is needed for my answer
}

然后您读取并创建Ingredient 对象列表。

List<Ingredient> data = new ArrayList<>();
try {
    String sCurrentline = null;
    BufferedReader br = new BufferedReader(new FileReader("/sdcard/MAIN_BF.csv"));
    while ((sCurrentline = br.readLine()) != null) {
        String[] arr = sCurrentline.split(",");
        Ingredient ingredient = new Ingredient(arr[0], arr[1], arr[2], arr[3]);
        data.add(ingredient);
    }
    br.close();
} catch (IOException e) {
    e.printStackTrace();
}

然后我们按名称将列表分组为Map

Map<String, List<Ingredient>> ingredientsByName = data.stream().collect(Collectors.groupingBy(Ingredient::getName));

然后循环遍历该地图以找到每种成分的最大管理值并将它们添加到正确的列表中

List<Ingredient> main = new ArrayList<>();
List<Ingredient> other = new ArrayList<>();

//Sort on `admin` in descending order
Comparator<Ingredient> compartor = Comparator.comparing(Ingredient:: getAdmin, (i1, i2) -> {
    if (i2 > i1) {
        return -1;
    } else if (i2 < i1) {
        return 1;
    } 
    return 0;
});

//Go through each list (ingredient) and find the one with max `admin` value 
//and add it to the `main` list then add the rest to `other`
ingredientsByName.forEach( (k, group) -> {
    Ingredient max = group.stream().max(compartor).get();
    if (max.getAdmin() == 0) {
        max = group.get(0);
    }
    main.add(max);
    group.remove(max);
    other.addAll(group);
});

【讨论】:

  • 嘿,我认为它有效。但我仍在尝试打印出每个列表中存储的值是什么。谢谢!
【解决方案2】:

我会将文件的全部内容加载到内存中并将其存储在java.util.List&lt;String&gt; 中。然后您可以按 NameadminList 进行排序。然后只需遍历List。每当您点击不同的 Name 时,您就知道其关联的 admin 值是最大的。因此,您将其添加到您的第一个 ArrayList,并将所有其他添加到您的第二个 ArrayList

【讨论】:

  • 当列表只包含字符串,没有Nameadmin 属性时,如何按Nameadmin 对列表进行排序,是吗?请在您的答案中添加一些代码来解释您的意思。
猜你喜欢
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多