Given a list of strings, find the list of strings which start with letter I in the given strings using Predicate Functional Interface
This exercise contains a class named PredicateFunctionalInterface with the following method:
+findPattern(List) : List
-Should accept list of strings as input
-Should find the list of strings which start with letter I in the given strings
-Should return list of strings which start with letter I in the given strings as output
-Should return empty list if no such string is found which start with letter I
-Should avoid the redundancy among the strings in output
Example
Sample Input:
[Icecream,Water,Ice,Gas,Ice]
Expected Output:
[Icecream,Ice]
Sample Input:
[Java,C,C++,Java,C]
Expected Output:
[]
Sample Input:
[]
Expected Output:
[]
SOLUTION TO ABOVE QUESTION
SOLUTION CODE
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of strings you want to add in your list: ");
int number_of_strings = sc.nextInt();
System.out.println("The number is " +number_of_strings);
ArrayList<String> my_string_array = new ArrayList<String>();
//now the elements starting with I we add them in set to avoid redundancy
Set<String> strings_with_I_set = new HashSet<String>();
for(int i = 0; i<number_of_strings; i++)
{
System.out.print("Enter string "+(i+1)+": ");
String my_string = sc.next();
//ad the string to the ist
my_string_array.add(my_string);
char c = my_string.charAt(0);
//add the string to our list
if(c=='I')
{
strings_with_I_set.add(my_string);
}
}
//print the input list
System.out.println("The input list is: "+my_string_array);
//print the output set
System.out.println("The output list is: "+strings_with_I_set);
}
}
SAMPLE OUTPUT PROGRAM
Comments
Leave a comment