Solved

Mapping - List

Posted on 2011-09-12
10
275 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

828 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