简体   繁体   English

JAVA:如何按字母顺序对从文件读取并输出到控制台的字符串进行排序?

[英]JAVA: How to sort strings read from file and output to console in alphabetical order?

I'm looking to sort the contacts read from a file in alphabetical order by last name to the console? 我正在寻找按姓氏顺序按字母顺序对从文件中读取的联系人进行排序? How would I go about doing so? 我将如何去做? The contacts are already written to file starting with the last name, I just want to read them back into the application in alphabetical order when a user wants to view the contacts in the console. 联系人已经以姓氏开头写入文件,当用户想要在控制台中查看联系人时,我只想按字母顺序将它们读回到应用程序中。

// Read from file, print to console. by XXXXX
            // ----------------------------------------------------------
            int counter = 0;
            String line = null;

            // Location of file to read
            File file = new File("contactlist.csv");

            try {

                Scanner scanner = new Scanner(file);

                while (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                    System.out.println(line);
                    counter++;
                }
                scanner.close();
            } catch (FileNotFoundException e) {

            }
            System.out.println("\n" + counter + " contacts in records.");

        }
        break;
        // ----------------------------------------------------------
        // End read file to console. by XXXX

Before printing, add each line to a sorted set, as a TreeSet : 在打印之前,将每一行作为TreeSet添加到排序集中:

Set<String> lines = new TreeSet<>();
while (scanner.hasNextLine()) {
    line = scanner.nextLine();
    lines.add(line);
    counter++;
}

for (String fileLine : lines) {
    System.out.println(fileLine);
}
package main.java.com.example;

import java.io.*;
import java.net.URL;
import java.util.Set;
import java.util.TreeSet;

public class ReadFromCSV {
    public static void main(String[] args) {
        try {
            final ClassLoader loader = ReadFromCSV.class.getClassLoader();
            URL url = loader.getResource("csv/contacts.csv");
            if (null != url) {
                File f = new File(url.getPath());
                BufferedReader br = new BufferedReader(new FileReader(f));
                Set<String> set = new TreeSet<String>();
                String str;

                while ((str = br.readLine()) != null) {
                    set.add(str);
                }

                for (String key : set) {
                    System.out.println(key);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

从文件中读取名称,并将其放入SortedSet类的对象中。

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

相关问题 按字母顺序对 java 中的字符串列表进行排序 - Sort a list of strings in java in alphabetical order 如何按字母顺序对文件进行排序? - How to sort a file in alphabetical order? 如何使用Java在文件之间按字母顺序对姓氏进行排序? - How do I use Java to sort surnames in alphabetical order from file to file? 按字母顺序对文本文件中的数据进行排序-Java - Sort data in a text file in alphabetical order - Java 如何仅使用我的代码中的 if 语句将字符串按正确的字母顺序排序? - How can I sort strings into the correct alphabetical order using only if statements from my code? 使用方法(Java)按字母顺序将文件内容排序到新文件中 - Sort contents of file in alphabetical order into a new file using methods (Java) 如何使用 Arrays.sort() 按字母顺序输出名称? - How to use Arrays.sort() to output names in an alphabetical order? 按匈牙利语字母顺序对匈牙利字符串列表进行排序 - Sort a list of hungarian strings in the hungarian alphabetical order 如何按字母顺序对列表视图进行排序 - How to sort a listview in alphabetical order 如何使用Java在Selenium中验证字符串列表是否按字母顺序排列 - how to validate a list of strings are in alphabetical order or not in Selenium using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM