Java 8 commonly asked interview questions

// count the occurrence of each character in a string
        String input = "ilovejavatechie";
        var split = input.split("");
        var collect = 
                Arrays.stream(split)
                .collect(Collectors
                .groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(collect);
 // find all duplicate element from a given string
        String input = "ilovejavatechie";
        var split = input.split("");
        var collect = Arrays
                .stream(split)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        var collect1 = collect
                .entrySet()
                .stream()
                .filter(x -> x.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
        System.out.println(collect1);
    // find first non repeat element from a given string
        String input = "ilovejavatechie";
        var split = input.split("");
        var collect = Arrays
                .stream(split)
                .collect(Collectors.groupingBy(Function.identity(),       LinkedHashMap::new, Collectors.counting()))
                .entrySet()
                .stream().filter(x -> x.getValue() == 1)
                .findFirst();

        System.out.println(collect);
 // to find second highest number from given array
        int[] ints = {5, 9, 11, 2, 8, 21, 1};
        var sorted = Arrays.stream(ints).boxed()
                .sorted(Comparator.reverseOrder()).skip(1).findFirst().get();
        System.out.println(sorted);
 // find longest string from given array
        String[] array = {"java", "techie", "springboot", "microservices"};
        var s = Arrays.stream(array)
                .reduce((w1, w2) -> w1.length() > w2.length() ? w1 : w2)
                .get();
        System.out.println(s);
     // to find all elements from array who start with 1
        int[] ints = {5, 9, 11, 2, 8, 21, 1};
        var collect = Arrays.stream(ints)
                .boxed()
                .map(s -> s + "")
                .filter(s -> s.startsWith("1"))
                .collect(Collectors.toList());
        System.out.println(collect);
       // String.join example
        var list = Arrays.asList("1", "2", "3", "4");
        var join = String.join("-", list);
        System.out.println(join);
 // skip and limit example
        IntStream.rangeClosed(1, 10)
                .skip(1)
                .limit(8)
                .forEach(System.out::println);