简体   繁体   English

按字母顺序对字符串数组进行排序

[英]Sorting an array of strings alphabetically

I have written the following working code. 我已经编写了以下工作代码。 I believe it could be made better or more efficient but I'm not too sure how to go on about it. 我相信它可以做得更好或更有效,但我不太确定如何进行。

The main thing that I'm unhappy with is the three replaces. 我不满意的主要内容是三个替换项。

ArrayList<String> test = new ArrayList<String>(); 
for (int i = 0; i < user.getTasks().size(); i++) {
    test.add(user.getTasks.get(i).getTask().toString());
}
Collections.sort(test);
System.out.println(test.toString().replace(",", " |").replace("[", "").replace("]", ""));

The output is something like follows: Tast1 | 输出如下所示:Tast1 | Task2 | Task2 | Task3 and it is good. Task3,它很好。

Please don't hesitate to ask any questions, I'm super responsive. 请不要问任何问题,我反应超级好。

Since you are using Java 7 and streams are not available, you can: 由于您使用的是Java 7,并且流不可用,因此您可以:

  1. use the simpler for syntax (assuming user.getTasks() returns a list of Task objects - replace Task with your class if not) 使用更简单for语法(假设user.getTasks()返回Task对象的列表-如果没有,请用您的类替换Task
  2. loop through the strings, concatenating them with the separator 遍历字符串,用分隔符将它们连接起来

ArrayList<String> test = new ArrayList<String>(); 
for (Task task : user.getTasks()) {
    test.add(task.getTask().toString());
}
Collections.sort(test);

// make a string of the values with a pipe separator
StringBuilder valuesToPrint = new StringBuilder();
for (int index = 0; index < test.size(); index++) {
    if (index > 0) {
        valuesToPrint.append(" | ");
    }
    valuesToPrint.append(test);
}

System.out.println(valuesToPrint.toString());

Java 8+ you could simplify it as: 您可以将Java 8+简化为:

List<String> test = new ArrayList<String>(); 
String result = user.getTasks().stream()
                               .map(t -> t.getTask().toString())
                               .sorted()
                               .collect(Collectors.joining("|"));
System.out.println(result);

Which will create a Stream of the String 's in the list, sort them, and then join them into a String with | 这将在列表中创建StringStream ,对它们进行排序,然后使用|将它们加入到String| in between each item 在每个项目之间

Or if you need to do other operations on the sorted list, you could sort it and then do the stream part after: 或者,如果您需要对已排序列表进行其他操作,则可以对其进行排序,然后在执行以下操作之后执行流部分:

Collections.sort(test);
String result = test.stream().collect(Collectors.joining("|"));

As pointed out, Java 8 Stream API provides a lot of utilities to perform those kind of tasks, but if you want to keep your solution using loops and you want to avoid the three replaces, instead of calling toString() on the list and then cleaning it up, you should loop the items on the list while building a String 如前所述,Java 8 Stream API提供了许多实用程序来执行这些任务,但是如果您想使用循环来保持您的解决方案,并且希望避免这三个替换,而不是在列表上调用toString() ,然后清理它,您应该在构建String时循环列表中的项目

String result = "";
for (String item : test) {
    result += item + " | ";
}
System.out.println(result.substring(0, result.length() - 3);

Note that you print the result cutting out the last three characters to avoid printing Tast1 | 请注意,打印结果时要切出最后三个字符,以避免打印Tast1 | Task2 | Task2 | Task3 | Task3 |

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

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