我有基于要重新排序列表对象的钥匙值



我正在编写一个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() + "ttt" + lineItem.getDescription() + "tt" + lineItem.getHsn() + "tt" +  
                    lineItem.getGstrate() + "tt" + lineItem.getDivision() + "tt" + lineItem.getQnty() + "tt" + lineItem.getRate() + "tt" + 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();
        }
    }
}
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();
       }
 });

最新更新