How to filter a list of strings by first character in Java

2 Answers

0 votes
import java.util.List;
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        List<String> list = 
        
        Arrays.asList("java", "c", "c++", "php", "c#");

        list.stream()
            .filter(s -> s.startsWith("c"))
            .sorted()
            .forEach(System.out::println);
    }
}



/*
run:

c
c#
c++

*/

 



answered Apr 8, 2021 by avibootz
0 votes
import java.util.List;
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        List<String> list = 
        
        Arrays.asList("java", "c", "c++", "php", "c#");

        list.stream()
            .filter(s -> s.startsWith("c"))
            .map(String::toUpperCase)
            .sorted()
            .forEach(System.out::println);
    }
}



/*
run:

C
C#
C++

*/

 



answered Apr 8, 2021 by avibootz

Related questions

2 answers 201 views
2 answers 250 views
1 answer 177 views
1 answer 136 views
...