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!
Code :
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));
}
}
}
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?
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.. ?
Re: Getting mp3 files from folder and sorting them using hashmap.
Quote:
Originally Posted by
raamkum
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)
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.