简体   繁体   中英

Sort a 2D Array with primary key

I'm trying to split each line of a text file and insert into an array list.

aa 04 cc ff gg rrr
aa 01 bb dd ee fff
aa 03 ff hh ee ttt
aa 05 dd ss ww ccc

1, How to store number as an integer and rest are strings in array list. (throwing Number Format Error with current code).

2, Is it possible to sort based on the integer while I am inserting to array with Java collections?

So far, I have,

List <Object> records = new ArrayList<Object>();
        bf = new BufferedReader(new FileReader(getFile()));
        String readLine;        

        while ((readLine = bf.readLine()) != null) {
            try {
                    List <Object> record = new ArrayList<Object>();

                    record.add(readLine.substring(flg_start, num_start).trim());
                    record.add(Integer.parseInt(readLine.substring(num_start-1, fld_start-1).trim())); // converting string to int and saving to record list, throwing Number Format Error.

let me rephrase my questions again, How can I set record [1] as integer and rest as string? What collection framework I can use to sort when I am adding into array? I am expecting the end result in my array as a sorted list of around 1M records

aa 01 bb dd ee fff
aa 03 ff hh ee ttt
aa 04 cc ff gg rrr
aa 05 dd ss ww ccc

As for the data structure looks like you need TreeMap with Integer keys and List values. You should read the file and parse each line as @HaridwarJha described and for each line put new value with the parsed Integer key and List value formed of the parsed values. With the TreeMap your data will be sorted by keys, ie by Integer values from you file.

Map<Integer, List<Object>> map = new TreeMap<Integer, List<Object>>();
while((line=reader.readLine())!=null){
    List<Object> row = new ArrayList<>();
    String[] strArray=line.split(" ");
    row.add(strArray[0]);
    row.add(strArray[1]);
    row.add(strArray[2]);
    row.add(strArray[3]);
    row.add(strArray[4]);
    row.add(strArray[5]);
    map.put(new Integer(row.get(1)), row);
}

You can do like this and to access strArray you can use for loop or can be use regex[0...1] for parsing.... Here is simple one...try it public static void main(String[] args) { List records = new ArrayList();

    File file=new File("File Location");
    try {
        BufferedReader reader=new BufferedReader(new FileReader(file));
        String line;
        while((line=reader.readLine())!=null){
            String[] strArray=line.split(" ");
            records.add(new Integer(strArray[1]));
            records.add(new String(strArray[0]));
            records.add(new String(strArray[2]));
            records.add(new String(strArray[3]));
            records.add(new String(strArray[4]));
            records.add(new String(strArray[5]));
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for(Object tmp:records){
        System.out.println(tmp);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM