?
Solved

Split a string into map

Posted on 2004-11-05
18
Medium Priority
?
516 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:komlaaa
  • 14
  • 4
18 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12511631
String s[]=s.split("Your expression");

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

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12511639
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
 

Author Comment

by:komlaaa
ID: 12511643
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12511697
==================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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12511701
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12511705
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12511723
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12511731
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
 

Author Comment

by:komlaaa
ID: 12514135
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12514168
>>I want map the first item of songAndArtist[] to the second.

I didn't get u. Can u elaborate??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12514181
Oh, I think  I understand

But a small question

for an artist will there be multiple songs are only one??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12514197
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12514256
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12514270
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
 

Author Comment

by:komlaaa
ID: 12517438
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
 

Author Comment

by:komlaaa
ID: 12517444
and there are different type of  artists.
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 900 total points
ID: 12517734
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12517739
>> and there are different type of  artists.

I wanted to see your dataset. I mean your artists and songs file
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 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