我正在编写一个java程序。该程序的主要议程是读取一个文本文件并将它们存储到一个列表中。我成功了但是现在我需要基于某个键对列表对象进行排序。我也有执行映射并有一个键,但我无法根据该键值重新排序列表对象 请帮帮我,看看我的整个代码
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
public class InvoiceFileReader extends InvoiceLineItem {
@SuppressWarnings("unused")
public static void main(String args[]) throws FileNotFoundException, IOException {
FileInputStream fs = null;
BufferedReader br = null;
List<InvoiceLineItem> invLineItems = new ArrayList<>();
String line = null;
String[] arrs = new String[100];
InvoiceLineItem lineitem = null;
int num = 0;
int sums[] = null;
int s = 0;
double t = 0;
String[] characters = null;
int z = 0;
String upns = null;
HashMap<String, List<InvoiceLineItem>> listMap;
try {
fs = new FileInputStream("K:/java/gstrates.txt");
br = new BufferedReader(new InputStreamReader(fs));
//StringTokenizer st=new StringTokenizer(line,"\t");
/*while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}*/
//For reading a line
br.readLine(); //Assuming that the first line is string
while ((line = br.readLine()) != null) {
arrs = line.split("\t");
lineitem = new InvoiceLineItem();
lineitem.setUpn(arrs[0]);
lineitem.setDescription(arrs[1]);
lineitem.setHsn(arrs[2]);
lineitem.setGstrate(arrs[3]);
lineitem.setDivision(arrs[4]);
lineitem.setQnty(Integer.parseInt(arrs[5]));
lineitem.setRate(Double.parseDouble(arrs[6]));
invLineItems.add(lineitem);
}
for (int i = 0; i < invLineItems.size(); i++) {
InvoiceLineItem lineItem = invLineItems.get(i);
System.out.println(lineItem.getUpn() + "\t\t\t" + lineItem.getDescription() + "\t\t" + lineItem.getHsn() + "\t\t" +
lineItem.getGstrate() + "\t\t" + lineItem.getDivision() + "\t\t" + lineItem.getQnty() + "\t\t" + lineItem.getRate() + "\t\t" + lineItem.getGrossamt());
z += (Integer.parseInt(arrs[5]));
t += lineItem.getGrossamt();
listMap = new HashMap<String,List<InvoiceLineItem>>();
ArrayList<InvoiceLineItem> arraylist = new ArrayList<InvoiceLineItem>();
if(listMap.get(lineItem.getUpn()) != null || (listMap.get(lineItem.getUpn()) == null && listMap.containsKey(lineItem.getUpn()))) {
System.out.println("The key is: " + listMap);
}
else {
String key = lineItem.getUpn();
arraylist = (ArrayList<InvoiceLineItem>) listMap.put("UPN ", listMap.get(key));
System.out.println("The key is: " + listMap.containsValue("UPN"));
//boolean val = listMap.isEmpty();
Set<String> val = listMap.keySet();
System.out.println("Key present:" + val);
Collections.sort(Arrayist);
}
}
System.out.println("The total quantity is: " + z);
System.out.println("The sum of Gross amount is: " + t);
} catch (Exception e) {
System.out.println(e);
} finally {
if (fs != null) {
fs.close();
}
if (br != null)
br.close();
}
}
}
答案 0 :(得分:1)
请使用以下方法进行排序:
Collections.sort(invLineItems /* Your array list which need to sort */, new Comparator<InvoiceLineItem >() {
@Override
public int compare(InvoiceLineItem o1, InvoiceLineItem o2) {
//This is the Comparable parameter.
return o1.getUpn() < o2.getUpn();
}
});