猿问

从 csv 文件中获取最高列值并将其存储到 arraylist

我正在尝试将具有最高管理值的名称的子名称存储到 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

如果一个名称只有 0个管理值,它应该是,

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

对于其他管理价值较小的数据,它们被存储到 arraylistTWO 中。

我一直在阅读 csv 文件,我不知道如何将具有最高管理值的名称的子名称存储到 arraylistONE 中,而对于具有较小管理值的其余数据,我不知道如何存储到 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 ();

}


慕仙森
浏览 98回答 2
2回答

翻过高山走不出你

首先,我认为创建一个简单的类来保存数据是有意义的,因为使用对象而不是值数组来过滤和排序会更容易。public class Ingredient {&nbsp; &nbsp; String name;&nbsp; &nbsp; String subName;&nbsp; &nbsp; int status;&nbsp; &nbsp; int admin;&nbsp; &nbsp; public Ingredient(String name, String subName, String status, String admin) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.subName = subName;&nbsp; &nbsp; &nbsp; &nbsp; this.status = Integer.valueOf(status);&nbsp; &nbsp; &nbsp; &nbsp; this.admin = Integer.valueOf(admin);&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public int getAdmin() {&nbsp; &nbsp; &nbsp; &nbsp; return admin;&nbsp; &nbsp; }&nbsp; &nbsp; //more get and set methods here. I have only included what is needed for my answer}然后你阅读并创建一个Ingredient对象列表。List<Ingredient> data = new ArrayList<>();try {&nbsp; &nbsp; String sCurrentline = null;&nbsp; &nbsp; BufferedReader br = new BufferedReader(new FileReader("/sdcard/MAIN_BF.csv"));&nbsp; &nbsp; while ((sCurrentline = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; String[] arr = sCurrentline.split(",");&nbsp; &nbsp; &nbsp; &nbsp; Ingredient ingredient = new Ingredient(arr[0], arr[1], arr[2], arr[3]);&nbsp; &nbsp; &nbsp; &nbsp; data.add(ingredient);&nbsp; &nbsp; }&nbsp; &nbsp; br.close();} catch (IOException e) {&nbsp; &nbsp; e.printStackTrace();}然后我们按名称将列表分组为MapMap<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 orderComparator<Ingredient> compartor = Comparator.comparing(Ingredient:: getAdmin, (i1, i2) -> {&nbsp; &nbsp; if (i2 > i1) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; } else if (i2 < i1) {&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return 0;});//Go through each list (ingredient) and find the one with max `admin` value&nbsp;//and add it to the `main` list then add the rest to `other`ingredientsByName.forEach( (k, group) -> {&nbsp; &nbsp; Ingredient max = group.stream().max(compartor).get();&nbsp; &nbsp; if (max.getAdmin() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; max = group.get(0);&nbsp; &nbsp; }&nbsp; &nbsp; main.add(max);&nbsp; &nbsp; group.remove(max);&nbsp; &nbsp; other.addAll(group);});

翻翻过去那场雪

我会将文件的全部内容加载到内存中并将其存储在java.util.List<String>.&nbsp;List然后你可以按Name和admin排序。然后只需遍历List.&nbsp;每当您点击不同的Name时,您就知道其关联的admin值是最大的。因此,您将其添加到您的 first 中ArrayList,并将所有其他人添加到您的 secondArrayList中。
随时随地看视频慕课网APP

相关分类

Java
我要回答