?
Solved

Mapping - List

Posted on 2011-09-12
10
Medium Priority
?
280 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 500 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
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…
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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