Core Java Assignment 10

1. Create a list with elements – "d2", "a2", "b1", "b3", "c" and filter the elements that start with "a" and print the element in uppercase.

 

2. Create a Person class with fields name and age.

    Create a list which contains Person objects.

    List<Person> persons =

                Arrays.asList(

                        new Person("Max", 18),

                        new Person("Peter", 23),

                        new Person("Pamela", 23),

                        new Person("David", 12));

 

    1. Print the list of person names (print as a list) that start with "P".

        Answer – [Peter, Pamela]

        Hint – Use .collect(Collectors.toList()) to print as a list

    2.  Sort the list of persons by age and print the person names as a list.

        Answer – [David, Max, Peter, Pamela]

 

3. Print the numbers greater than 5 from the below list.

    List<Integer> numbers = Arrays.asList(1, 5, 7, 3, 8, 10, 48, 67, 2, 23, 56, 11, 4, 100);

 

    Answer – 7, 8, 10, 48, 67, 23, 5, 11, 100

    Hint – Use Streams and Lambdas expression to filer and sort.

Leave a Reply

Your email address will not be published. Required fields are marked *