Solved

Mapping - List

Posted on 2011-09-12
10
273 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jkteater
Comment Utility
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
Comment Utility
leaving for the day - be back tomorrow
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

728 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

9 Experts available now in Live!

Get 1:1 Help Now