Solved

Arraylist Grouping

Posted on 2009-06-30
8
513 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

688 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