Write a program to group the string objects by length. Strings less than 10 characters should be added to the first group, Strings with exactly 10 characters should be added to the second group and Strings greater than 10 characters should be added to the third group.
Input (ArrayList containing String s) |
Outputs - Less Than Group, Equals Group, Greater Than Group (All of them are ArrayList s containing String s) |
---|---|
["Andhra Pradesh", "Tamil Nadu", "Maharashtra", "Madhya Pradesh", "Gujarat", "Karnataka"] |
Less than group = ["Gujarat", "Karnataka"]
|
["Sachin Tendulkar", "Yuvraj", "Harbhajan", "Virat Kohli", "Virendar Shewag", "MS Dhoni", "Suresh Raina", "Rohit"] |
Less than group = ["Yuvraj", "Harbhajan", "MS Dhoni", "Rohit"]
|
import java.util.*;
class GroupStringsByLength
{
public static void main(String s[])
{
ArrayList<String> input = new ArrayList<String>();
input.add("Andhra Pradesh");
input.add("Tamil Nadu");
input.add("Maharashtra");
input.add("Madhya Pradesh");
input.add("Gujarat");
input.add("Karnataka");
ArrayList<String> lessThanGroup = new ArrayList<String>();
ArrayList<String> equalsGroup = new ArrayList<String>();
ArrayList<String> greaterThanGroup = new ArrayList<String>();
groupStringsByLength(input, lessThanGroup, equalsGroup, greaterThanGroup);
System.out.println("Less Than Group = " + lessThanGroup);
System.out.println("Equals Group = " + equalsGroup);
System.out.println("Greater Than Group = " + greaterThanGroup);
}
public static void groupStringsByLength(List<String> input,
List<String> lessThanGroup,
List<String> equalsGroup,
List<String> greaterThanGroup)
{
}
}
If you need explanation Read this topic
If you need Answer Take test on this topic