简体   繁体   English

对馆藏进行排序

[英]Sorting Collections of Collection

I am trying to sort file content based on the first two columns value: 我正在尝试根据前两列的值对文件内容进行排序:

My file content is: 我的文件内容是:

File name : ComplexMapFileReader.txt 档案名称:ComplexMapFileReader.txt

And the conetent is: 内容是:

B 12 hgjhgjgjgjgjgjhgjgjgjgjgjgj
B 3 jgjdSAGdjgsdKJADJgjfgAJFL
B 6 jygtjgjgjgjgjgjhgjhbj
C 7 uiyuiyikykk
C 2 wrteyytyuiiiiyjhg
A 1 hikhkhkhkjhkhkhjkhkh
A 4 khkhkkhkhkhkhkh
A 2 khkhkhkhkhkhkhkhkhkhkhkhh
A 11 jhgjsGJDGjAGFjgfjgjfgj
A 5 gjgjgsadgajgjDGkkhkhdDKjhkjd
B 5 hdskdhkljdhKAJHDKjahsd
A 12 kjhkdjhfknkjflkjfsdlkjf
C 3 kjhfekJEHFKHefkh
B 34 khkhkjhkjhkqlwwjljlllljj
A 21 iuhEWHRIekhrlkHRKErhwkrhk;h;kfnk.nfas
C 11 jlklkqwwlklklklllklkkkkjjj         

My code for the same is : 我的代码是:

package CollectionExamples;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class MapFileReader {

    private static final String FILENAME = "D:\\ComplexMapFileReader.txt";

    public static void main(String[] args) {

        BufferedReader br = null;
        FileReader fr = null;

        Map<Integer, String> innerMap = null;
        Map<String, TreeMap> outerMap = new TreeMap();
        try {
            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);
            String sCurrentLine;
            br = new BufferedReader(new FileReader(FILENAME));

            try {
                while ((sCurrentLine = br.readLine()) != null) {

                    String[] s = sCurrentLine.split(" ");

                    if (!outerMap.containsKey(s[0])) {
                        innerMap = new TreeMap();
                        innerMap.put(Integer.valueOf(s[1]), s[2]);
                        outerMap.put(s[0], (TreeMap) innerMap);
                    } else {
                        innerMap.put(Integer.valueOf(s[1]), s[2]);
                        outerMap.put(s[0], (TreeMap) innerMap);
                    }

                }

                String s0 = null;
                String s1 = null;
                String s2 = null;

                for (Entry<String, TreeMap> map1 : outerMap.entrySet()) {

                    s0 = map1.getKey();

                    for (Entry<Integer, String> map2 : innerMap.entrySet()) {

                        s1 = " "+map2.getKey() + "";
                        s2 =" "+ map2.getValue();
                        System.out.println("Each line equals:" + s0 + s1 + s2);
                        System.out.println("#############");

                    }

                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

The above code is not giving me the desired result. 上面的代码没有给我想要的结果。 But when the above file is in some sort of ordered like (Alphabets all B's together or A's together ) then file content as shown below gives correct sorting: 但是当上面的文件以某种有序的方式排列时(字母将所有B和A在一起),则文件内容如下所示:

B 12 hgjhgjgjgjgjgjhgjgjgjgjgjgj
B 3 jgjdSAGdjgsdKJADJgjfgAJFL
B 6 jygtjgjgjgjgjgjhgjhbj
C 7 uiyuiyikykk
C 2 wrteyytyuiiiiyjhg
A 1 hikhkhkhkjhkhkhjkhkh
A 4 khkhkkhkhkhkhkh
A 2 khkhkhkhkhkhkhkhkhkhkhkhh
A 11 jhgjsGJDGjAGFjgfjgjfgj
A 5 gjgjgsadgajgjDGkkhkhdDKjhkjd

And the result is the desired one (Sort by Alphabet and then by Number): 结果是所需的结果(按字母顺序,然后按数字顺序):

OutPut: 输出:

Each line equals:A 1 hikhkhkhkjhkhkhjkhkh
#############
Each line equals:A 2 khkhkhkhkhkhkhkhkhkhkhkhh
#############
Each line equals:A 4 khkhkkhkhkhkhkh
#############
Each line equals:A 5 gjgjgsadgajgjDGkkhkhdDKjhkjd
#############
Each line equals:A 11 jhgjsGJDGjAGFjgfjgjfgj
#############
Each line equals:B 1 hikhkhkhkjhkhkhjkhkh
#############
Each line equals:B 2 khkhkhkhkhkhkhkhkhkhkhkhh
#############
Each line equals:B 4 khkhkkhkhkhkhkh
#############
Each line equals:B 5 gjgjgsadgajgjDGkkhkhdDKjhkjd
#############
Each line equals:B 11 jhgjsGJDGjAGFjgfjgjfgj
#############
Each line equals:C 1 hikhkhkhkjhkhkhjkhkh
#############
Each line equals:C 2 khkhkhkhkhkhkhkhkhkhkhkhh
#############
Each line equals:C 4 khkhkkhkhkhkhkh
#############
Each line equals:C 5 gjgjgsadgajgjDGkkhkhdDKjhkjd
#############
Each line equals:C 11 jhgjsGJDGjAGFjgfjgjfgj
#############

But when the file has the content as the below one, I am not getting the desired result. 但是当文件的内容如下时,我没有得到想要的结果。 I know as it is map it will override the previous value in the below case. 我知道是地图,在以下情况下它将覆盖先前的值。 But is there any way better way to solve this problem. 但是有什么更好的方法可以解决这个问题。

B 12 hgjhgjgjgjgjgjhgjgjgjgjgjgj
    B 3 jgjdSAGdjgsdKJADJgjfgAJFL
    B 6 jygtjgjgjgjgjgjhgjhbj
    C 7 uiyuiyikykk
    C 2 wrteyytyuiiiiyjhg
    A 1 hikhkhkhkjhkhkhjkhkh
    A 4 khkhkkhkhkhkhkh
    A 2 khkhkhkhkhkhkhkhkhkhkhkhh
    A 11 jhgjsGJDGjAGFjgfjgjfgj
    A 5 gjgjgsadgajgjDGkkhkhdDKjhkjd
    B 5 hdskdhkljdhKAJHDKjahsd
    A 12 kjhkdjhfknkjflkjfsdlkjf
    C 3 kjhfekJEHFKHefkh
    B 34 khkhkjhkjhkqlwwjljlllljj
    A 21 iuhEWHRIekhrlkHRKErhwkrhk;h;kfnk.nfas
    C 11 jlklkqwwlklklklllklkkkkjjj         

I think it's a better way to represent each row with a Record object. 我认为这是用Record对象表示每一行的更好方法。 Actually we don't need the TreeMap for sorting. 实际上,我们不需要TreeMap进行排序。 And it's will be much more convenient if you want to add more fields or change the compare logic. 如果您想添加更多字段或更改比较逻辑,它将更加方便。 For the record list, we only need to implement the Comparable interface to implement your compare logic. 对于记录列表,我们只需要实现Comparable接口即可实现您的比较逻辑。

For example: 例如:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MapFileReader {

    private static final String FILENAME = "D:\\ComplexMapFileReader.txt";


    private static class Record {
        private char ch;
        private int num;
        private String string;

        public void setCh(char ch) {
            this.ch = ch;
        }

        public char getCh() {
            return ch;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public int getNum() {
            return num;
        }

        public String getString() {
            return string;
        }

        public void setString(String string) {
            this.string = string;
        }

    }

    public static void main(String[] args) {

        BufferedReader br;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader(FILENAME));

            List<Record> records = new ArrayList<>();

            try {
                while ((sCurrentLine = br.readLine()) != null) {

                    String[] s = sCurrentLine.split(" ");

                    if (s.length != 3) {
                        // Filter invalid records
                        continue;
                    }

                    Record r = new Record();
                    r.ch = s[0].charAt(0);
                    r.num = Integer.parseInt(s[1]);
                    r.string = s[2];

                    records.add(r);
                }

                Collections.sort(records, new Comparator<Record>() {
                    @Override
                    public int compare(Record o1, Record o2) {
                        if (o1.ch == o2.ch) {
                            return o1.num < o2.num ? -1 : (o1.num == o2.num ? 0 : 1);
                        }
                        return o1.ch < o2.ch ? -1 : 1;
                    }
                });

                for (Record record : records) {
                    System.out.println(String.format("Each line equals: %s %s %s", record.ch, record.num, record.string));
                    System.out.println("#############");
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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