简体   繁体   中英

sort a list of characters using lambdas in java

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.

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);

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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