Menu
Question Index
...

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 Strings) Outputs - Less Than Group, Equals Group, Greater Than Group (All of them are ArrayLists containing Strings)
["Andhra Pradesh", "Tamil Nadu", "Maharashtra", "Madhya Pradesh", "Gujarat", "Karnataka"] Less than group = ["Gujarat", "Karnataka"]
Equals group = ["Tamil Nadu"]
Greater than group = ["Andhra Pradesh", "Maharashtra", "Madhya Pradesh"]
["Sachin Tendulkar", "Yuvraj", "Harbhajan", "Virat Kohli", "Virendar Shewag", "MS Dhoni", "Suresh Raina", "Rohit"] Less than group = ["Yuvraj", "Harbhajan", "MS Dhoni", "Rohit"]
Equals group = [EMPTY]
Greater than group = ["Sachin Tendulkar", "Virat Kohli", "Virendar Shewag", "Suresh Raina"]


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)
    {
    }

}

Doubts

Problems

Topic: Java ArrayList

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App