Solved

Arraylist Grouping

Posted on 2009-06-30
8
508 Views
Last Modified: 2013-11-23
ArrayList<String> superList = new ArrayList<String>();

ArrayList<String> subList = new ArrayList<String>();

superList.add("sa1");superList.add("sa1");
superList.add("sa2");superList.add("sa4");superList.add("sa3");

subList.add("A");subList.add("A");subList.add("A");subList.add("B");
subList.add("B");

the output sud contain a hashmap where key is A and value is a arraylist
like
A = {sa1,sa2},
B = {sa4,sa3}

Here key is from sublist and value is from corresponding superlist.

ArrayList<String> superList = new ArrayList<String>();
 

ArrayList<String> subList = new ArrayList<String>();
 

superList.add("sa1");superList.add("sa1");

superList.add("sa2");superList.add("sa4");superList.add("sa3");
 

subList.add("A");subList.add("A");subList.add("A");subList.add("B");

subList.add("B");
 

the output sud contain a hashmap where key is A and value is a arraylist

like

A = {sa1,sa2}

B = {sa4,sa3}

Open in new window

0
Comment
Question by:satyabrata25
  • 3
  • 3
8 Comments
 
LVL 20

Expert Comment

by:a_b
Comment Utility
will the size of the sublist and superlist be equal?
0
 
LVL 20

Accepted Solution

by:
a_b earned 500 total points
Comment Utility
       ArrayList<String> superList = new ArrayList<String>();
        ArrayList<String> subList = new ArrayList<String>();

        superList.add("sa1");superList.add("sa1");
        superList.add("sa2");superList.add("sa4");superList.add("sa3");

        subList.add("A");subList.add("A");subList.add("A");subList.add("B");
        subList.add("B");
       
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for(int i=0;i<superList.size();i++)
        {
             List<String> list = map.get(subList.get(i));
             if(list == null)   list = new ArrayList<String>();
             if(!list.contains(superList.get(i)))
             list.add(superList.get(i));
             map.put(subList.get(i), list);
        }
        System.out.println(map.toString());
0
 

Author Comment

by:satyabrata25
Comment Utility
hi a b how r u doing...
It has to ..null will be there if no value...the value will come from database..
so i think the size is equal.but key is A(which is from subList) Value will be corresponding
values from superList{sa1,sa2,sa3}....we ll not put null if its there in wen forming the output
A={sa1,sa2}

Ex:
public getData(List1,List2)
{
some opearion
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 20

Expert Comment

by:a_b
Comment Utility
Did you check the implementation that I gave?
0
 

Author Comment

by:satyabrata25
Comment Utility
ya its working fine....i ll test with real data which i have...and let u know...thanks champ....
0
 

Author Comment

by:satyabrata25
Comment Utility
When a member try to post some question in that case...
the user may have struggled with the logic.anyway programming is
putting the logic in correct way.
i tried to put my question in such a way thet me and others get benifitted.
I ll appreciate if EE will provide a guideline to there users how to use
the forum before they join here.That will make clear some confusion..
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now