gpt4 book ai didi

java - 如何以特定定义的排序顺序有效读取 csv 列表?

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:24 27 4
gpt4 key购买 nike

我正在阅读 CSV并将其映射到 List<SomeBean>使用ICsvBeanReader

csv:

someName,someNumber,report,env,dir
TEST1,111111111111,DES,T,LAS
TEST1,111111111111,INV,T,LAS
TEST1,111111111111,DES,T,FIRS
TEST1,111111111111,INV,T,FIRS
TEST1,111111111111,ORD,P,FIRS
TEST1,111111111111,ORD,P,LAS

一旦我有了List<SomeBean> dataList,我正在运行嵌套 for 循环来获得我想要实现的目标。

定义的顺序:

{1-FIRS, 2-LAS}
{1-P, 2-T}
{1-ORD, 2-DES, 3-INV}

预期:“someNumber”的映射和 report、env、dir 的组合(按定义的订单)。

111111111111,ORD-P-FIRS
111111111111,DES-T-FIRS
111111111111,INV-T-FIRS
111111111111,ORD-P-LAS
111111111111,DES-T-LAS
111111111111,INV-T-LAS

编码:

获取订单:

Map<Integer, String> dirOrder = {1-FIRS, 2-LAS}; // getting from defined config
Map<Integer, String> envOrder = {1-P, 2-T}; // getting from defined config
Map<Integer, String> reportOrder = {1-ORD, 2-DES, 3-INV};// getting from defined config

运行循环来准备 map :

MultiValuedMap < String, String > mapProperOrder = new ArrayListValuedHashMap < > ();

for (Map.Entry < Integer, String > directionEntry: dirOrder.entrySet()) {
String directionValue = directionEntry.getValue();

for (Map.Entry < Integer, String > envirionmentEntry: envOrder.entrySet()) {
String envirionmentValue = envirionmentEntry.getValue();

for (Map.Entry < Integer, String > reportTypeEntry: reportOrder.entrySet()) {
String reportTypeValue = reportTypeEntry.getValue();

for (SomeBean someBean: (List < SomeBean > ) someList) {

String num = someBean.getNum();
String dir = someBean.getDir();
String env = someBean.getEnv();
String report = someBean.getReport);

boolean directionCheck = dir.equalsIgnoreCase(directionValue) ? true : false;
boolean envirionmentCheck = env.equalsIgnoreCase(envirionmentValue) ? true : false;
boolean reportTypeCheck = report.equalsIgnoreCase(reportTypeValue) ? true : false;

if (directionCheck && envirionmentCheck && reportTypeCheck) {
mapProperOrder.put(num, report + "-" + env + "-" + dir);
}
}
}
}
}

这段代码完成了它的工作,但是如果我在 CSV 中有多个“someNumber”怎么办,为所有记录运行嵌套 for 循环将效率不高。

请帮我写一个简单而有效的方法来处理这个问题。

最佳答案

如果此订单是应用程序范围内的默认订单,我个人会在 SomeBean 中实现 Comparable 接口(interface),否则是自定义的 Comparator 接口(interface)。

实现可能如下所示:

public int compare(SomeBean o1, SomeBean o2) {
Map<String, Integer> firstLevel = new HashMap<>();
firstLevel.put("FIRS", 1);
firstLevel.put("LAS", 2);


int cmp = firstLevel.get(o1.getDir()).compareTo(firstLevel.get(o2.getDir()));

if (cmp != 0) {
return cmp;
}

Map<String, Integer> secondLevel = new HashMap<>();
secondLevel.put("P", 1);
secondLevel.put("T", 2);

cmp = secondLevel.get(o1.getEnv()).compareTo(secondLevel.get(o2.getEnv()));

if (cmp != 0) {
return cmp;
}

Map<String, Integer> thirdLevel = new HashMap<>();
thirdLevel.put("ORD", 1);
thirdLevel.put("DES", 2);
thirdLevel.put("INV", 3);

return thirdLevel.get(o1.getReport()).compareTo(thirdLevel.get(o2.getReport()));

}

关于java - 如何以特定定义的排序顺序有效读取 csv 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58519536/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com