Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: i need some advice ....

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default i need some advice ....

    hello i am pretty new to java i am making my first program that will get info from mp3 files and find all duplicates now i get a very very big array (arraylist to be exact) with some objects that havs a couple of strings artist title ...
    what is the best way to organize this array let say according to artist so it will be most efficient (not using a linked list) i thought alphabetically so i can compare songs only with artist in the same letters

    i thought maybe using an arraylist and do what you do with an linked list check where to put your object and push everything forward so your array is always organized

    the best way i found is Collections methods but i don't really know what it is ill be very happy if some one explain to me what it is and how to use it (good likes to guide are good to )

    i well be happy to read what you think i should do ....


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: i need some advice ....

    Hello and welcome,

    How big array are we talking about?

    You might want to write your self a comparator and sort the array on whatever it is you want to sort it on

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    mdstrauss (July 24th, 2009)

  4. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: i need some advice ....

    i don't know what is a big array but i hove at least 7000 objects on it how do i create comparator and i don't really understand what the term Collections mean what is it ?

    did i understand this write?
    sort methods put things in order and comparator are the way they put them in order
    if you can give me some explanation that well be great

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: i need some advice ....

    Ok, here we go, lets say we have a separate file/class for the mp3 info.

    public class Mp3Info implements Comparable<Mp3Info> {
     
        private String artist;
     
        private String song;
     
        public Mp3Info() {
     
        }
     
        public Mp3Info(final String artist, final String song) {
            this.artist = artist;
            this.song = song;
        }
     
        @Override
        public int compareTo(Mp3Info mp3Info) {
            return this.artist.compareTo(mp3Info.getArtist());
        }
     
        public String getArtist() {
            return artist;
        }
     
        public void setArtist(String artist) {
            this.artist = artist;
        }
     
        public String getSong() {
            return song;
        }
     
        public void setSong(String song) {
            this.song = song;
        }
    }

    As you can see there it implements Comparable so we have to implement the compareTo method. This could be done using a comparator instead and if you wish to know what that will look like, let me know and I will create one.

    Here is the code for my main test class.

        public static void main(String... args) {
            final List<Mp3Info> mp3s = new ArrayList<Mp3Info>();
     
            mp3s.add(new Mp3Info("La Roux", "Bulletproof"));
            mp3s.add(new Mp3Info("Cascada", "Evacuate the Dancefloor"));
            mp3s.add(new Mp3Info("Black Eyed Peas", "I got a feeling"));
            mp3s.add(new Mp3Info("Lady Gaga", "Just dance"));
     
            Collections.sort(mp3s);
     
            for (final Mp3Info mp3Info : mp3s) {
                System.out.println("Artist: " + mp3Info.getArtist());
            }
        }

    And thats it really.

    // Json

  6. The Following User Says Thank You to Json For This Useful Post:

    mdstrauss (July 24th, 2009)

  7. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: i need some advice ....

    Ok, lets say we wanted to sort by songs instead, lets create a comparator.

        public static class SongComparator implements Comparator<Mp3Info> {
     
            @Override
            public int compare(Mp3Info mp3Info1, Mp3Info mp3Info2) {
                return mp3Info1.getSong().compareTo(mp3Info2.getSong());
            }
        }

    And the main app.

        public static void main(String... args) {
            final List<Mp3Info> mp3s = new ArrayList<Mp3Info>();
     
            mp3s.add(new Mp3Info("La Roux", "Bulletproof"));
            mp3s.add(new Mp3Info("Cascada", "Evacuate the Dancefloor"));
            mp3s.add(new Mp3Info("Black Eyed Peas", "I got a feeling"));
            mp3s.add(new Mp3Info("Lady Gaga", "Just dance"));
     
            Collections.sort(mp3s, new SongComparator());
     
            for (final Mp3Info mp3Info : mp3s) {
                System.out.println("Artist: " + mp3Info.getArtist() + " (" + mp3Info.getSong() + ")");
            }
        }

    Note how we pass in a new SongComparator into the Collections.sort method.

    // Json

  8. The Following User Says Thank You to Json For This Useful Post:

    mdstrauss (July 24th, 2009)

  9. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: i need some advice ....

    thank you very very much
    let me see if i understand the sort method forces the arraylist (that is a abstract of collection) to use the comparator or the Comparable interface to organize the objects ....
    ...?

  10. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: i need some advice ....

    Well, to sort an arraylist you have to pass it to the Collections static method called sort, for this to work the object in the arraylist must implement comparable or you can pass in another comparator you've created which will take that object as you can see by the generics declaration.

    However if you would like to have a list of unique items you can make your self a TreeList which will always sort it self when passing in a new object.

    // Json

  11. The Following User Says Thank You to Json For This Useful Post:

    mdstrauss (July 24th, 2009)

  12. #8
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: i need some advice ....

    both methods have a compareTo method in them that return 0,1,-1 can i build some other comparing system that returns the same ints ???

  13. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: i need some advice ....

    For creating a quick set, i'd recommend using a Binary Search Tree or a Hash Table instead. You can define the compareTo method any way you want, that's the point of having the Comparable interface. It allows you to compare certain things that for humans may be trivial, but are quite strange for the computer to compare, for example which picture has more blue, or which next move in chess is a good/optimal choice.