> Java Milestone Challenge Solution - TECH UPDATE

Java Milestone Challenge Solution

 

Java Milestone Challenge Solution




import java.util.Arrays;

import java.util.List;

import java.util.Scanner;


import java.util.function.Function;

import java.util.function.Predicate;

/*

 * Create the Filter and Mapper classes here.

 */

 class Filter {

    public static Predicate<String> nameStartingWithPrefix(String prefix) {

        return n -> n.startsWith(prefix);

    }

}

class Mapper {

    public static Function<String, CharactersCount> getDistinctCharactersCount() {

        return s -> new CharactersCount(s, (int)s.chars().distinct().count());

    }

}


class CharactersCount {

    private final String name;

    private final Integer distinctCharacterCount;

    

    public CharactersCount(String name, Integer distinctCharacterCount) {

        this.name = name;

        this.distinctCharacterCount = distinctCharacterCount;

    }

    

    @Override

    public String toString() {

        return "\"" + this.name + "\" has " + this.distinctCharacterCount + " distinct characters.";

    }

}


public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    

    public static void main(String[] args) {

        List<String> names = Arrays.asList(

                "aaryanna",

                "aayanna",

                "airianna",

                "alassandra",

                "allanna",

                "allannah",

                "allessandra",

                "allianna",

                "allyanna",

                "anastaisa",

                "anastashia",

                "anastasia",

                "annabella",

                "annabelle",

                "annebelle"

        );

        

        names.stream()

                .filter(Filter.nameStartingWithPrefix(scanner.nextLine()))

                .map(Mapper.getDistinctCharactersCount())

                .forEachOrdered(System.out::println);

    }

}

Java Milestone Challenge Solution Java Milestone Challenge Solution Reviewed by TECH UPDATE on December 15, 2021 Rating: 5

6 comments:

  1. Replies
    1. import java.util.function.Function;

      import java.util.function.Predicate;

      Include these lines.

      Delete
    2. import java.util.function.Function;

      import java.util.function.Predicate;
      Include these lines.

      Delete
    3. import java.util.function.Function;

      import java.util.function.Predicate;

      Include these lines.

      Delete
    4. import java.util.function.Function;

      import java.util.function.Predicate;

      Include these statements.

      Delete
  2. Please share the Agular Milestone Challenge Solution

    ReplyDelete

Powered by Blogger.