简体   繁体   English

排序列表的分配

[英]assignment for sorting lists

My chapter 16 assignment is this.我的第 16 章作业是这样的。

Create an application which创建一个应用程序

  • accepts user input of a string or words接受用户输入的字符串或单词
  • prints each word back in alphabetical order按字母顺序打印每个单词
  • prints the total number of elements typed打印输入的元素总数

I have this so far到目前为止我有这个

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;



public class Chapter16Assignment {
        public static void main(String[] args){

            Scanner input = new Scanner(System.in);


            String userInput = "";
            System.out.print(" Enter a line of words: ");
            userInput = input.nextLine();
            List<String> userString = Arrays.asList(userInput);
            String[]userInput
            //userString.add(userInput);
            //.split
            Collections.sort(userString);
            System.out.println(userString);
            //System.out.println("Frequency of words: " + Collection.frequency(userString));

            }



    }

I need to use split or something.我需要使用拆分或其他东西。 i thought i could use sort method and frequency for part 3.我想我可以在第 3 部分使用排序方法和频率。

I expect the output to be enter a line of words then for it to print those words in alphabetical order, then the amount of words.我希望 output 输入一行单词,然后按字母顺序打印这些单词,然后是单词的数量。 at the moment its not doing any of that.目前它没有做任何事情。 its getting the users input though.它虽然得到了用户的输入。

This is how you can use split method to convert string to string array.这就是您可以使用 split 方法将字符串转换为字符串数组的方法。 I am not using list since I want to show you a slightly different approach to do this problem.我没有使用列表,因为我想向您展示一种稍微不同的方法来解决这个问题。

I am removing white spaces and converting everything to lower case.我正在删除空格并将所有内容转换为小写。 It is up to you if you want to do it.如果你想这样做,这取决于你。

Scanner input = new Scanner(System.in);
String userInput;
System.out.print("Enter a line of words: ");
userInput = input.nextLine().replace(" ", "").toLowerCase();
String[] userInputSplit = userInput.split(""); // Splits array
Arrays.sort(userInputSplit); // Sorts array
System.out.println(Arrays.toString(userInputSplit)); // Prints sorted array
// Checks for frequency of each letter using maps
Map<String, Integer> countMap = Arrays.stream(userInputSplit)
        .collect(Collectors.toMap(Function.identity(), v -> 1, Integer::sum));
// Prints map
System.out.println("Frequency of words: " + countMap.toString());

Output: Output:

Enter a line of words: The quick brown fox jumps over the lazy dog
[a, b, c, d, e, e, e, f, g, h, h, i, j, k, l, m, n, o, o, o, o, p, q, r, r, s, t, t, u, u, v, w, x, y, z]
Frequency of words: {a=1, b=1, c=1, d=1, e=3, f=1, g=1, h=2, i=1, j=1, k=1, l=1, m=1, n=1, o=4, p=1, q=1, r=2, s=1, t=2, u=2, v=1, w=1, x=1, y=1, z=1}

Edit:编辑:

Like @AndyTurner Suggested You can use Collectors.counting() as well which makes the syntax a bit easier to understand but it will return Long instead of Integer喜欢@AndyTurner 建议您也可以使用Collectors.counting()这使得语法更容易理解,但它会返回 Long 而不是 Integer

Map<String, Long> countMap = Arrays.stream(userInputSplit)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Collectors.counting is essentially Collectors.counting 本质上是

Map<String, Integer> countMap = Arrays.stream(userInputSplit)
        .collect(Collectors.toMap(Function.identity(), v -> 1L, Long::sum));

Using HashSet and Collections.frequency:使用 HashSet 和 Collections.frequency:

HashSet<String> uniqueValues = new HashSet<String>(userInputList);
for (String value : uniqueValues) {
    System.out.println("Frequency of " + value + " is: " + Collections.frequency(userInputList, value));
}

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

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