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 5 of 5

Thread: Getting mp3 files from folder and sorting them using hashmap.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting mp3 files from folder and sorting them using hashmap.

    Hi,

    I am trying to get solution for getting mp3 files from a specific folder and sort them ascending order. I want to use hash map reason - it can also store the location of the mp3 file. Is this possible??

    I tried out the below code but over here we have to enter file names manually.

    Can somebody help me out on this?

    Awaiting your response!

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Set;
     
    public class Main {
     
    	public static void main(String[] args) {
    		HashMap<String, String> mp3List= new HashMap<String, String>();
    		mp3List.put("rbc.mp3", "rebba camma");
    		mp3List.put("abc.mp3", "lamma romma");
    		mp3List.put("ter.mp3", "Teriya mama");
    		mp3List.put("rat.mp3", "ratatriyla");
     
    		Set<String> set=mp3List.keySet();
    		String[] keys=new String[set.size()];
    		set.toArray(keys);
    		List<String> tmpkeyList=Arrays.asList(keys);
     
    		Collections.sort(tmpkeyList);
    		for(String key:tmpkeyList){
    		System.out.println(key+":"+mp3List.get(key));
    		}
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting mp3 files from folder and sorting them using hashmap.

    Instead of using a HashMap, I would recommend creating an Object that stores pertinent information about an mp3, including name and file location. Then you can implement Comparable and use that to sort a List of that Object however you want.

    But I'm not really sure what your actual question is. What do you mean you have to enter the names manually?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting mp3 files from folder and sorting them using hashmap.

    Names manually was for the code that tried...

    It would be really great - if you can please give me an example on creating the object.. ?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting mp3 files from folder and sorting them using hashmap.

    Quote Originally Posted by raamkum View Post
    Names manually was for the code that tried...

    It would be really great - if you can please give me an example on creating the object.. ?
    What have you tried? Where are you stuck?

    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Getting mp3 files from folder and sorting them using hashmap.

    Another approach is along the lines you tried:

    * Have the filenames and names in a map (key/value). * Take the key set and from it construct a sorted set * Go through the sorted set with a for loop and pick out the associated name.

    Note that there is no array used at any point - that is making things more complicated.

    Either way (this or KevinWorkman's) you will have to try things out and post some code so we can see exactly where you are stuck. And probably do a bit of reading. The various collections available are described well in Oracle's Tutorial.

Similar Threads

  1. Replies: 1
    Last Post: March 18th, 2012, 12:16 PM
  2. Replies: 15
    Last Post: September 2nd, 2011, 05:05 PM
  3. How to test all files in a folder
    By GodspeedPR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 28th, 2011, 06:15 AM
  4. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  5. Listing files in a folder in a .jar
    By kulan8 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 22nd, 2010, 08:18 AM