Java 从csv文件中获取最高列值并将其存储到arraylist中
我正在尝试将具有最高管理值的Name的子名称存储到arraylistONE中 在这种情况下, 多用途面粉是1 3/4杯多用途面粉 小苏打粉是1/4茶匙小苏打和1/2杯原味 小苏打是8茶匙的小苏打粉 如果名称只有0个管理值,则应为 煮咖啡应始终存储文件中的第一个值,即1杯煮咖啡 对于管理值较小的其余数据,它们存储在arraylistTWO中 我一直在阅读csv文件,我不知道如何将管理值最高的Name的子名称存储到ArrayListon中,对于管理值较小的其余数据,我不知道如何存储到arraylistTWO中 这是我迄今为止所做的工作:Java 从csv文件中获取最高列值并将其存储到arraylist中,java,csv,arraylist,Java,Csv,Arraylist,我正在尝试将具有最高管理值的Name的子名称存储到arraylistONE中 在这种情况下, 多用途面粉是1 3/4杯多用途面粉 小苏打粉是1/4茶匙小苏打和1/2杯原味 小苏打是8茶匙的小苏打粉 如果名称只有0个管理值,则应为 煮咖啡应始终存储文件中的第一个值,即1杯煮咖啡 对于管理值较小的其余数据,它们存储在arraylistTWO中 我一直在阅读csv文件,我不知道如何将管理值最高的Name的子名称存储到ArrayListon中,对于管理值较小的其余数据,我不知道如何存储到arraylis
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 ();
}
首先,我认为创建一个保存数据的简单类是有意义的,因为使用对象而不是值数组更容易过滤和排序
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
}
然后阅读并创建成分对象列表
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();
}
然后在地图上循环找到每个成分的最大管理值,并将它们添加到正确的列表中
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);
});
我会将文件的全部内容加载到内存中,并将其存储在java.util.List中。然后,您可以按名称和管理员对列表进行排序。然后只是在列表中迭代。每当你点击一个不同的名字,你就会知道它关联的管理员值是最大的。因此,您可以将其添加到第一个ArrayList,将所有其他文件添加到第二个ArrayList。您正在读取代码中的两个不同文件,这与你的问题有什么关系?@JoakimDanielson我忘了排除这个作业,但有一些限制,或者使用Java 8和streams可以吗?CSV文件是否按照你发布的示例数据按名称排序?没有限制。在我的项目@joakimdanielson中设置了minSdkVersion 19和targetSdkVersion 27。当列表只包含字符串时,没有名称和管理员属性,如何按名称和管理员对列表进行排序,是吗?请在你的答案中添加一些代码来解释你的意思。嘿,我想它起作用了。但我仍在试图打印出每个列表中存储的值。谢谢
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);
});