Solved

Arraylist Grouping

Posted on 2009-06-30
8
509 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
ID: 24743628
will the size of the sublist and superlist be equal?
0
 
LVL 20

Accepted Solution

by:
a_b earned 500 total points
ID: 24743655
       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
ID: 24743664
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Expert Comment

by:a_b
ID: 24743740
Did you check the implementation that I gave?
0
 

Author Comment

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

Author Comment

by:satyabrata25
ID: 24753270
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

911 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

22 Experts available now in Live!

Get 1:1 Help Now