sort a list of characters using lambdas in java

為{幸葍}努か 提交于 2019-12-11 12:18:40

问题


ok this is homework. I have tried a couple things but just cant get it to work. the instructions are--Write a program that inserts 30 random letters into a List. Perform the following operations and display your results: a) Sort the List in ascending order. b) Sort the List in descending order. c) Display the List in ascending order with duplicates removed. I have the simplest part done but when it comes to the sorting im not sure where to start. public class RandomCharacters {

// private static List randomCharacters;

public static void main(String[] args) {


    //Create List of Char type
    List<Character> randomCharacters = new ArrayList<>();
    //Character[] randChars = new Character[30];

    //generate random letters and add to the list
    for(int i = 0; i<30; i++){
        int num = (int) (26* Math.random());
        //randChars[i] = (char) (num + 'a');
        char letter = (char) (num + 'a');
        randomCharacters.add(letter);
        //System.out.println(letter);
    }
    //System.out.println( "Unsorted list= " + Arrays.asList(randChars));
    System.out.println( "Unsorted list= " + randomCharacters);


    System.out.println("Ascending sort: ");
    Arrays  .stream(randomCharacters)
            .sorted()
            .collect(Collectors.toList());
}

I have tried doing it two ways. one with the Character Array and converting that using tolist and by just using the ArrayList but the sort is not working. any hints as to why this is not working.


回答1:


You can call Stream.sorted() and Stream.sorted(Comparator<? super T>) and Stream.distinct(). Something like,

System.out.println("Unsorted list= " + randomCharacters);
System.out.println("Sorted Ascending: ");
randomCharacters.stream().sorted().forEach(System.out::println);
System.out.println("Sorted Descending: ");
randomCharacters.stream().sorted(Comparator.reverseOrder())
        .forEach(System.out::println);
System.out.println("Ascending unique sort: ");
randomCharacters.stream().sorted().distinct().forEach(System.out::println);



回答2:


Because you call ".collect" on the stream but don't store it anywhere. And you print the unsorted list (you are sorting the list after printing it). Do this instead

List<Character> sortedList = randomCharacters.stream()
                                             .sorted()
                                             .collect(Collectors.toList());
System.out.println("Ascending sort: " + sortedList);



回答3:


alright thanks guys i used a combination of both of your soulutions and here is my complete code. I think its good for what i need but if anyone thinks it can be more efficent let me know public class RandomCharacters {

private static List<Character> randomCharacters;

public static void main(String[] args) {


    //Create List of Char type
    List<Character> randomCharacters = new ArrayList<>();


    //generate random letters and add to the list
    for(int i = 0; i<30; i++){
        int num = (int) (26* Math.random());
        char letter = (char) (num + 'a');
        randomCharacters.add(letter);
    }

    System.out.println( "Unsorted list= " + randomCharacters);

    List<Character> sortedList = randomCharacters.stream()
                                                 .sorted()
                                                 .collect(Collectors.toList());
    System.out.println("Ascending sort: " + sortedList);


    List<Character> reversedList = randomCharacters.stream()
                                                    .sorted(Comparator.reverseOrder())
                                                    .collect(Collectors.toList());
    System.out.println("Sorted Descending: " + reversedList);

    List<Character> uniqueList = randomCharacters.stream()
                                                 .sorted()
                                                 .distinct()
                                                 .collect(Collectors.toList());
    System.out.println("Unique Sort: "+ uniqueList);
}

}

i will add more comments before turning in as well



来源:https://stackoverflow.com/questions/36254950/sort-a-list-of-characters-using-lambdas-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!