10 Replies Latest reply: Jun 17, 2010 1:39 PM by 843793 RSS

    how to get element from hashset

    843793
      I have class Song
      public class Song {
           private String title;
           private String artist;
      
           public Song(){
                
           }
           
           public Dosen(String title,String artist){
                this.title= title;
                this.artist=artist;
           }     
           
           public String getTitle(){
                return title;
           }
           
           public  String getArtist(){
                return artist;
           }
           
             //override
           public boolean equals(Object songData){
                Song d = (Song) songData;
                return getTitle().equals(d.getTitle());
           }
           //override
           public int hashCode(){
                return getTitle().hashCode();
           }
      and then
      import java.util.HashSet;
      public class test {
           
           public static void main(String args[]){
                HashSet<Song> songHashSet = new HashSet<Song>();
                
                      Song song1 = new Song("dance", "lady gaga");
                Song song2 = new Song("candy shop", "50 cent");
                   Song song3 = new Song("dance", "lady gaga");
                
                songHashSet.add(song1);
                songHashSet.add(song2);
                songHashSet.add(song3);
                    
                      //
      i just wonder how to print all of element from songHashSet...
      so I want
      output
      dance , lady gaga
      candy shop, 50 cent
      anyone can help me??
      sorry about my english
        • 1. Re: how to get element from hashset
          843793
          You can use a [for loop|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html]. Note that there are 2 different forms of for loops. Both work, but one is much easier to use in this case.
          • 2. Re: how to get element from hashset
            843793
            sorry, can you give me an example please..
            • 3. Re: how to get element from hashset
              843793
              ilmuinside wrote:
              sorry, can you give me an example please..
              There is a link in my answer. Follow it. Read the page I linked to. It contains plenty of examples.
              • 4. Re: how to get element from hashset
                843793
                i've tried...but i did not found any method to get element from songHashSet...

                songHashset.????
                • 5. Re: how to get element from hashset
                  843793
                  ilmuinside wrote:
                  i've tried...
                  What have you tried? Reading the tutorial I linked to?
                  but i did not found any method to get element from songHashSet...
                  You mean the example on that page that shows you how to print all elements of an array doesn't look like it could be useful in your case?
                  • 6. Re: how to get element from hashset
                    843793
                    import java.util.HashSet;
                    public class test {
                    
                         public static void main(String args[]){
                              HashSet<Song> songHashSet = new HashSet<Song>();
                    
                                    Song song1 = new Song("dance", "lady gaga");
                              Song song2 = new Song("candy shop", "50 cent");
                                 Song song3 = new Song("dance", "lady gaga");
                    
                              songHashSet.add(song1);
                              songHashSet.add(song2);
                              songHashSet.add(song3);
                    
                                    for(int i=0,i<=songHashSet.size();i++){
                                           System.out.println(songHashSet.????????);/*this is really my problem..I didn't find what method should i use*/
                                    }
                    
                    
                    }
                    • 7. Re: how to get element from hashset
                      843793
                      You're using the wrong for-loop.

                      You could use that loop type together with an explicit Iterator, but it's more complicated.

                      The problem is that Sets don't have an indexed getter (i.e. you can't do "get(1)") because the elements in a Set don't have a defined order and thus "first element" is undefined.

                      The easier solution is to use the second for-loop form (as I've already told you!).
                      • 8. Re: how to get element from hashset
                        843793
                        thank you..I will try again..
                        • 9. Re: how to get element from hashset
                          843793
                          You problem is that you have chosen a Set when you should be using a Map. This is simpler and more efficient because it more naturall fits your solution.
                          public class Song {
                              public final String title;
                              public final String artist;
                           
                              public Song (String title,String artist){
                               this.title= title;
                               this.artist=artist;
                              }
                              public String toString() {
                               return title + " by " + artist;
                              }
                          }     
                          and then
                          import java.util.HashMap;
                          public class test {
                              public static void main(String args[]){
                                  Map<String, Song> songMap = new HashMap<String, Song>();
                                  songMap.put("dance", new Song("dance", "lady gaga"));
                                  songMap.put("candy shop", new Song("candy shop", "50 cent"));
                                  songMap.put("dance", new Song("dance", "lady gaga"));
                          
                                  System.out.println("all songs: "+songMap);
                                  System.out.println("get(dance): "+songMap.get("dance"));
                              }
                          }
                          • 10. Re: how to get element from hashset
                            843793
                            thank you very much.....it works...