Split a string into map

Hi Experts!!
I have a string of two fields: s = "a b"
How can i split s into a map so that a is the key and b the value?
Something like: Map m = new HashMap(); m.put(s.split("\\|"); =====> NOT CORRECT SYNTAX BUT THE IDEA
komlaaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:
String s[]=s.split("Your expression");

map.put(s[0],s[1]);

0
sudhakar_koundinyaCommented:
provided that string must have only one key value pair

i.e

String s1="a|b";
 String s[]=s1.split("|");

map.put(s[0],s[1]);


if u have multiple key value pairs like
String s1="a|b|c|d|e|f"; //for an example

then this will work

 String s[]=s1.split("|");

for(int i=0;i<s.length;i+=2)
{
map.put(s[i],s[i+1]);
}


0
komlaaaAuthor Commented:
That answer is correct but in the code below, notice that it is in a while loop and i don't
want to use 2d array but a map to do this.
I don't want to hardcode it. i would like to use variable in case the size of string vary

==================sample code ===============
 for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");
            tracks = new String[numOfTracks][songAndArtist.length];
           
           for(int y = 0; y < songAndArtist.length; y++){
                tracks[x][y] = songAndArtist[y];
               
           }
       }
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

sudhakar_koundinyaCommented:
==================sample code ===============
 for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");
            tracks = new String[numOfTracks][songAndArtist.length];
            Map map=new Hashmap();          
           for(int y = 0; y < songAndArtist.length; y++){
       
                map.put(new Integer(y),songAndArtist[y]);
               
           }
       }
0
sudhakar_koundinyaCommented:
rather it should be

Map mainmap=new Hashmap();

for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");
            tracks = new String[numOfTracks][songAndArtist.length];
            Map map=new Hashmap();          
           for(int y = 0; y < songAndArtist.length; y++){
       
                map.put(new Integer(y),songAndArtist[y]);
               
           }
              mainmap.put(new Integer(x),map);
       }
0
sudhakar_koundinyaCommented:
or if you wnat to use single map



Map mainmap=new Hashmap();

for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");
            tracks = new String[numOfTracks][songAndArtist.length];
       
           for(int y = 0; y < songAndArtist.length; y++){
       
                mainmap.put(x+" "+y,songAndArtist[y]);
               
           }

       }
0
sudhakar_koundinyaCommented:
The other model what you can have is

Map map=new Hashmap();
for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");            
           map.put(new Integer(x),songAndArtist);
       }
0
sudhakar_koundinyaCommented:
So finally you have 3 models

Model 1
__________________________________________

uses 2 maps to handle the data
__________________________________________

Map mainmap=new Hashmap();

for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");
            Map map=new Hashmap();          
           for(int y = 0; y < songAndArtist.length; y++){
       
                map.put(new Integer(y),songAndArtist[y]);
               
           }
              mainmap.put(new Integer(x),map);
       }



Model 2
__________________________________________

uses 1 map to handle the data
__________________________________________

Map mainmap=new Hashmap();

for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");
           for(int y = 0; y < songAndArtist.length; y++){
       
                mainmap.put(x+" "+y,songAndArtist[y]);
               
           }              
       }



Model 3
__________________________________________

uses 1 map to handle the data
__________________________________________


Map map=new Hashmap();
for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");            
           map.put(new Integer(x),songAndArtist);
       }
0
komlaaaAuthor Commented:
Thanks for all your models but, that is not what i a trying to achieve.

I want map the first item of songAndArtist[] to the second.
0
sudhakar_koundinyaCommented:
>>I want map the first item of songAndArtist[] to the second.

I didn't get u. Can u elaborate??
0
sudhakar_koundinyaCommented:
Oh, I think  I understand

But a small question

for an artist will there be multiple songs are only one??
0
sudhakar_koundinyaCommented:
If it is one to one relation

Map map=new Hashmap();
for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");            
           for(int i=0;i<songAndArtist.length;i+=2)  
           {
                       map.put(songAndArtist[i],songAndArtist[i+1]);
           }      
       }
0
sudhakar_koundinyaCommented:
If it is one artist to one song relation

Map map=new Hashmap();
for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");            
           for(int i=0;i<songAndArtist.length;i+=2)  
           {
                       map.put(songAndArtist[i],songAndArtist[i+1]);
           }      
       }




 If it is one artist  to many songs relation

Map artists=new Hashmap();
for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");            

           for(int i=0;i<songAndArtist.length;i+=2)  
           {
                      String song=songAndArtist[i];
                      String artist =songAndArtist[i+1];  
                     Vector songs=(Vector)artists.get(artist);
                     if(songs==null)
                       songs=new Vector();
                     songs.add(song);
                     artists.put(artist,songs);
           }      
       }
0
sudhakar_koundinyaCommented:
similary

If it is one song  to many artistss relation

Map songs=new Hashmap();
for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");            

           for(int i=0;i<songAndArtist.length;i+=2)  
           {
                      String song=songAndArtist[i];
                      String artist =songAndArtist[i+1];  
                     Vector artists=(Vector)artists.get(song);
                     if(artists==null)
                       artists=new Vector();
                     artists.add(artist );
                     artists.put(song,artists);
           }      
       }
0
komlaaaAuthor Commented:
Thats someting like your code below i am looking for, except that in my example,
artist (type String ) is the key and Songs is a list of few possible type of Songs (type Object)
Exemple:

  -------                 ------------------------                       ------------------
| artist |---------> |  SongTypeOne         |-------------->| SongTypeTwo |------------------>...
  -------                 -------------------------                      -------------------
.......
.......
I shouldn't allow duplicate in my  list of Songs for an artist key
0
komlaaaAuthor Commented:
and there are different type of  artists.
0
sudhakar_koundinyaCommented:
In that case  the code below "If it is one artist  to many songs relation" will work for you.

Ummm, But you are saying Duplicate songs are not allowed to an artist
then this code must work to you

Map artists=new Hashmap();
for(int x = 0; x < numOfTracks; x++)
       {
           String compTracks = in.readLine();
           System.out.println(compTracks);
           String songAndArtist[] = compTracks .split("\\|");            

           for(int i=0;i<songAndArtist.length;i+=2)  
           {
                      String song=songAndArtist[i];
                      String artist =songAndArtist[i+1];  
                     Set songs=(Set)artists.get(artist);
                     if(songs==null)
                       songs=new HashSet();
                     songs.add(song);
                     artists.put(artist,songs);
           }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudhakar_koundinyaCommented:
>> and there are different type of  artists.

I wanted to see your dataset. I mean your artists and songs file
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.