Solved

Mapping - List

Posted on 2011-09-12
10
279 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
[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
  • 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
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

 

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

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

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…
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 about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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