Day 14 - Java

Problem Statement: Mahabharata List Manipulation

In the great epic Mahabharata, the Pandavas were a legendary group of brothers who played a crucial role in the epic's events. Let's say you have a list of strings representing the names of certain characters. You're given another list, which should ideally contain the names of the Pandavas: Yudhisthira, Bhima, Arjuna, Nakula, and Sahadeva. However, this list might be incomplete.

Your task is to create a Java program that takes an input list of strings, representing some characters from the Mahabharata, and identifies which Pandavas are present and which are missing. The program should use Java streams for processing.

Your program should:

Read a comma-separated list of strings from the standard input.

Identify which of the Pandavas are present in the input list and which are missing.

Output the names of the Pandavas in the same order as the original list, printing the name in lowercase if present and in uppercase if missing.

Implement the solution using Java streams.

Input Format:

A single line of comma-separated strings representing characters from the Mahabharata.

Output Format:

The names of the Pandavas, each on a separate line. Names present in the input should be printed in lowercase, while missing names should be printed in uppercase.

Sample Input 1:

Krishna,Bhima,Arjuna,Nakula,Sahadeva

Sample Output 1:

YUDHISTHIRA

Bhima

Arjuna

Nakula

Sahadeva


Sample Input 2:

Krishna,Arjuna,Sahadeva,Karna,Duryodhana

Sample Output 2:

YUDHISTHIRA

BHIMA

Arjuna

NAKULA

Sahadeva


Sample Input 3:

Bhima,Sahadeva,Karna,Yudhisthira,Arjuna,Nakula,Arjuna

Sample Output 3:

Yudhisthira

Bhima

Arjuna

Nakula

Sahadeva


SOLUTION:

import java.util.*;

import java.util.stream.Collectors;

public class MahabharataListManipulation {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);Bhima,Sahadeva,Karna,Yudhisthira,Arjuna,Nakula,Arjuna

        String input = sc.nextLine();

        List<String> arr = Arrays.asList(input.split(","));

        List<String> pandavas = Arrays.asList("Yudhisthira", "Bhima", "Arjuna", "Nakula", "Sahadeva");

        

        List<String> missedPandavas = pandavas.stream()

                         .filter(pandava -> !arr.contains(pandava))

                         .collect(Collectors.toList());

        pandavas.forEach(i -> {

            if (arr.contains(i)) {

                System.out.println(i);

            } else {

                System.out.println(i.toUpperCase());

            }

        });

    }

}

Insights:

  • This program splits the input string using the split() method based on commas , and converts it into a list of strings.
  • The program initializes a list containing the names of the Pandavas: Yudhisthira, Bhima, Arjuna, Nakula, and Sahadeva.
  • It uses Java streams to filter out the names of Pandavas that are missing from the input list. This is done using the filter() operation.
  • The filtered Pandavas names (those missing in the input list) are collected into a separate list using the collect() method.
  • It iterates through each Pandava name in the original Pandavas list. If the name is present in the input list, it's printed in lowercase. Otherwise, it's printed in uppercase.
  • The program demonstrates the usage of streams and lambda expressions in Java for concise and expressive code.
  • The order of the Pandava names in the output is preserved, maintaining the same sequence as the original Pandavas list.
May your coding concentration be as sharp and unwavering as Arjuna's aim at the fish's eye.
Focus and Aim the best! :)

Comments