Solved

Arraylist Grouping

Posted on 2009-06-30
8
512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 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