[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Arraylist Grouping

Posted on 2009-06-30
8
Medium Priority
?
519 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 1500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …
Suggested Courses

649 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