English 中文(简体)
在文件中对数据进行排序
原标题:sorting data in a file
  • 时间:2012-05-25 07:37:14
  •  标签:
  • java
  • sorting

我在分类方面面临一个问题。数据的格式是:

b4 S0_c5 t 0.426544 
b6 S1_c5 t 1.51049 
b13 S0_c5 t 0.594502 
b13 S1_c5 t 0.537496 
b15 S1_c5 t 0.884126   
b18 S0_c5 t 0.500933 
b19 S1_c5 t 0.628472 
b22 S0_c5 t 0.437718 

所需结果如下:

S0_c5 b13 0.594502 b18 0.500933 b22 0.437718 b4 0.426544 
S1_c5 b6 1.51049 b15 0.884126 b19 0.628472 b13 0.537496 

数值也以递减顺序排列。 提前感谢 。

最佳回答

将数据放入 TreeList< String, List< String; String< String> & gt; (因为它已经分类) 中, 一个序列的第二个单词是密钥, 以及字符串列表的价值, 然后将您获取的每个列表排序 :

    Map<String, List<String[]>> map = new TreeMap<String, List<String[]>>();
    for (String s : strings) {
        String[] tokens = s.split(" ");
        List<String[]> values = map.get(tokens[1]);
        if (values == null) {
            values = new ArrayList<String[]>();
            map.put(tokens[1], values);
        }
        values.add(new String[]{tokens[0],  tokens[3]});
    }

    for (String key : map.keySet()) {
        List<String[]> list = map.get(key);
        Collections.sort(list, new Comparator<String[]>() {

            @Override
            public int compare(String[] o1, String[] o2) {
                return o1[1].compareTo(o2[1]) * -1;
            }

        });

        System.out.print(key + " ");
        for (String[] s : list) {
            System.out.print(s[0] + " " + s[1]);
        }
        System.out.println();
    }

Update: E.g. to read from a file:

    BufferedReader br;
    try {
        br = new BufferedReader(new FileReader("d:/temp/r.res"));

        Map<String, List<String[]>> map = new TreeMap<String, List<String[]>>();
        while (br.ready()) {
            String s = br.readLine();
            if (!s.trim().isEmpty()) {
                String[] tokens = s.split(" ");
                List<String[]> values = map.get(tokens[1]);
                if (values == null) {
                    values = new ArrayList<String[]>();
                    map.put(tokens[1], values);
                }
                values.add(new String[]{tokens[0],  tokens[3]});
            }
        }
    } finally {
        br.close();
    }
问题回答
  • Split your data by .
  • Create a HashMap<String, List<String[]>>
  • For Each Row:
    • Look if the Map Contains the Key (split[1])
      • If there is no List at that key, create one
      • add split[1], split to the correct list
  • Iterate through your map and order each List
  • Output the data

将数据放入 List 并使用 Colections.sort () 进行排序。

JDK 中有一个类, 仅用于排序列表。 它被命名为“ java. util. 优先 Quue ” ( 与其它分类* 接口有些不合序 ) 。 它可以排序可比较的或使用比较器 。

与使用收藏.sort(...) 排序的列表的区别在于, 这将在任何时候都保持秩序, 并且通过使用堆积数据结构插入性能良好, 其中在排序的矩阵列表中插入为 O(n) (即使用二进制搜索和移动) 。

然而,除列表外,优先队列不支持索引访问(get(5)),在堆积中访问项目的唯一办法是一次取出(因此名为优先队列)。

试试这个,会成功的

private void ReadTextFile(String filename) throws IOException
    {
        BufferedReader br = null;
        FileInputStream fin = null;
        fin = new FileInputStream(filename);
        br =new BufferedReader(new InputStreamReader(fin));
        Map<String,String> stringStringMap = new TreeMap<String, String>(Collections.reverseOrder());
        while ((line = br.readLine()) != null) {
            stringStringMap.put(line.split(" ")[3],line);
        }
        Collection<String> collection = stringStringMap.values();
        Map<String, List<String>> map = new TreeMap<String, List<String>>();
        Iterator<String> iterator = collection.iterator();
        while(iterator.hasNext()){
            String[] tokens = iterator.next().split(" ");
            List<String> values = map.get(tokens[1]);
            if (values == null) {
                values = new ArrayList<String>();
                map.put(tokens[1], values);
            }
            values.add(tokens[0] + " " + tokens[3]);
        }
        for (List<String> mapList : map.values()) {
            Collections.sort(mapList);
        }
        for (String key : map.keySet()) {
            System.out.println(key + " " + map.get(key));
        }
    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签