排序在Java中,字符串比较 [英] Sorting in Java, String comparison

查看:152
本文介绍了排序在Java中,字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此数据被保存在data.txt中我试图写一个程序,可以安排

This data is saved in data.txt I am trying to write a program that can arrange

18b0885  // this is the registration number, bullet points to show indentation 
   SS844 Parallel Algorithms        //  These are course taken by student
   SS555 Calculus for Distributed Computing
   SS501 Quantum Communication

17b0585
   SS828 Problem Based Programming
   SS844 Parallel Algorithms 
   SS567 Hacking Quantum Network

17b2582
   SS567 Hacking Quantum Network
   SS844 Parallel Algorithms 
   SS501 Quantum Communication

这样的数据的大名单​​,而需要编写一个程序,总之这个数据升序由登记号和课程将遵循注册号。所以进出料口放就是这个样子。

A big list of data like this, And need to write a program to short this data is ascending order by registration number and course will follow the registration number. so the expected out put is like this.

OUT输出,当然会题目,学生注册号正在服用的过程中样品谁:

Out put will, course Title and the students registration number who are taking that course sample:

SS501 Quantum Communication
     18b0885
     17b2582

SS567 Hacking Quantum Network
     17b2582
     17b0585

SS844 Parallel Algorithms 
     17b2582
     17b0585
     18b0885

等等,哪一种方法会更容易做到这一点。

and so on, Which method will be easier to do this

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;
import java.util.TreeSet;

public class Sort3 {
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new FileReader("data2018.txt"));
    //   Map<String, List<String>> map = new TreeMap<String, List<String>>();
    Map<String, Set<String>> map = new TreeMap<String, Set<String>>();
    String line = reader.readLine();//read header
    while ((line = reader.readLine()) != null) {
      String key = getField(line); // 
      //  List<String> l = map.get(key);
      Set<String> l = map.get(key);
      if (l == null) {
        // l = new LinkedList<String>();
        l = new TreeSet<String>();
        map.put(key, l);
      }
      l.add(line);

    }
    reader.close();
    FileWriter writer = new FileWriter("sorted_numbers4.txt");
    writer.write("");
//     for (List<String> list : map.values()) {
//      for (String val : list) {
//       writer.write(val);
//       writer.write("\n");
//      }
//     }
    for (Set<string> key : map.keySet()){
//print key (subject) here
      writer.write("Subject="+key+"\n");
      for (Set<String> list : map.get(key)) {
        for (String val : list) {
          writer.write(val);
          writer.write("\n");
        }
      }
    }

    writer.close();
  }

  private static String getField(String line) {
    return line.split(",")[0];// 
  }
}


以上程序了付诸这样的另一个文本文件


The above program out puts into another text file like this

    SS501 Quantum Communication
    SS555 Calculus for Distributed Computing
    SS567 Hacking Quantum Network
    SS567 Hacking Quantum Network
    SS660 Genetic Computation
    SS828 Problem Based Programming
    SS844 Parallel Algorithms
    SS876 Positronics
    SS880 Quark-based Logic

    17b2582
    17b0585
    18b0885

任何建议进行修改,以获得想要的答案吗?

Any recommendation to modify to get desired answer?

推荐答案

而不是的地图&LT;字符串列表与LT;字符串&GT;&GT;地图=新TreeMap的&LT;字符串列表与LT;字符串&GT;&GT;(); 使用地图&LT;字符串,设置&LT;字符串&GT;&GT;地图=新TreeMap的&LT;字符串,设置&LT;字符串&GT;&GT;(); ,然后使用而不是 TreeSet的 A LinkedList的如: L =新TreeSet的&LT;字符串&GT;(); 这可以确保列表(组)作品是独一无二的,分选

Instead of Map<String, List<String>> map = new TreeMap<String, List<String>>(); use Map<String, Set<String>> map = new TreeMap<String, Set<String>>(); and then use a TreeSet instead of a LinkedList e.g. l = new TreeSet<String>(); This ensures that list (set) entries are unique and sorted.

现在,而写出来,第一个迭代的 KeySet的地图,然后遍历相关联的值(集)每个按键,类似下面伪code:

Now while writing out, iterate first on KeySet of the Map and then iterate the values (set) associated with each keys, something like below pseudo-code:

for (String key : map.keySet()){
//print key (subject) here
writer.write("Subject="+key+"\n");
for (Set<String> list : map.get(key)) {
      for (String val : list) {
       writer.write(val);
       writer.write("\n");
      }
}
}

编辑:从样本数据分析逻辑电,我可以看到,当然,名称以S字,而注册号为十六进制/数字。可能是你可以使用这个信息,而分析数据。

parsing logic- from your sample data, I can see that course names begin with "S" character while registration numbers are hex/numeric. may be you could use this information while parsing the data.

这篇关于排序在Java中,字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆