Solved

Mapping - List

Posted on 2011-09-12
10
274 Views
Last Modified: 2012-05-12
As you may know - I have asked a question about how to turn 2 arrayList in to 1.  I have received a great suggestion and plan on using it.  But during the process, I was looking at Maps.  I really have never used Maps in code before.  I was doing something like this.

Map <List<TCComponent>, List<TCComponent>> map = new HashMap<List<TCComponent>, List<TCComponent>>();

Iterator<?> it = revisionList.iterator();
      while (it.hasNext()){
      map.put(revisionList, datasetList);
      }

Open in new window


But that can not work because both things I am putting in to MAP each are list and may be more than one entry.

So my question is how do you put values in a map from listArrays? and when you have them in your map how would you print out your map?
0
Comment
Question by:jkteater
  • 5
  • 3
  • 2
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36526062
It is not commopn to have a List as a key.
It uis quirte OK rto have a List a s a wvalue - but suggest hwne would you want to have a list as akey ?
difficult to uinvent such case



0
 
LVL 47

Expert Comment

by:for_yan
ID: 36526070
Why would you want to have a list as a key?
If this list has some uniqe element then use this as a key.
You don;'t want to have the whole list as a key - jkey uis usually integer or unique String, buty not a list
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36526081
Very normal situation is to have a key - like Integer or String and ArrayList as a value

Then if you need to print in not very beautiful way,
you can just sya

HashMap<Strung, ArrayList<String,STRING>>  m = new HashMap<Strung, ArrayList<String,STRING>>  () ;

// populate

System.out.println(m);

because hashmap and ArrayList bith have reasonavble toString()
you;ll see reasonable printout
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:jkteater
ID: 36526087
OK what if my key is a string and the value is a list - how would I create the map then?
0
 

Author Comment

by:jkteater
ID: 36526104
leaving for the day - be back tomorrow
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 36526424

Well, you create it as in any other case
Let's say you have the keys in one array
String [] keys;
and then you have
values  in the array
String [] values;

And now let's assume that you ca have several
different values corresponding to the same key

say
keys [1] = "myKey";
value[1] = "value1";

Open in new window

and
keys[3] = "myKey";
value[3] = "value3

Open in new window


of you juist make hashmap key-value
then when you store third element it will overwrite first element

That's why is this case you want to have
MashMap of the type key vs list of Vlaues


HashMap<String, ArrayList<String>>  m = new HashMap<String, ArrayList<String>>  () ;

for(int j=0; j<keys.length; j++){
if(m.get(keys[j]) == null){
ArrayList <String> ar = new ArrayList<String>();
ar.add(values[j]);
m.put(keys[j],ar);

} else
{
ArrayList <String> ar = m.get(keys[j]);
ar.add(values[j]);
m.put(keys[j],ar);
}
}

Open in new window



So after you run thorough all your jeys-values pairs
you'll get you HashMap m,
which will store lists of values against the
keys which are strings
(there is a way even to make that code inside the loop a few lines shorter,
but I like this way - because it is very illustrative)


as i said when you just

System.out.println(m);

it will print the HashMap in a reasonable format
thanks to good toString() methods of both HashMap
and ArrayList




0
 
LVL 47

Expert Comment

by:for_yan
ID: 36526440
if you still want to format it differently when printing
you could do it like that:

Set set = m.keySet();
for(String s: set){

System.out.print("key " + s + " :  Values: ");
ArrayList<String> ar = m.get(s);
for(String s1: ar){

System.out.print(s1 + " ");
}

System.out.println("");


}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36528157
As i mentioned in your other question - a Map is much more appropriate for what you want to do ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36528380
>>But that can not work because both things I am putting in to MAP each are list and may be more than one entry.

That is not the case afaicr. There is one 'dataset' with n 'revision'. Only the latter is a List, and even that maybe not, since you said that you wished to prevent > 1 revision.
This is not really the place to discuss this though, since it belongs in the original. I can't remember now though whether that original question is still open
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36531073
For future viewers of this question, please see http:Q_27293047.html, of which this question is essentially a duplicate in two senses:

a. it duplicates my original suggestion
b. it duplicates the original question
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

813 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

8 Experts available now in Live!

Get 1:1 Help Now