3

My problem is can't get an object "Item" (value) from my Treemap. I need send that info to my GUI class and display it in JList to get a select list, so can easily select and add songs to playlist, but only what I get as an output is "01, 02, 03, 04, 05" (key). Please help, because I'm beginner and have no idea what to do.

public class LibraryData {

private static class Item {


    Item(String n, String a, int r) {
        name = n;
        artist = a;
        rating = r;
    }

    // instance variables 
    private String name;
    private String artist;
    private int rating;
    private int playCount;

    public String toString() {
        return name + " - " + artist;
    }
}


private static Map<String, Item> library = new TreeMap<String, Item>();


static {
    library.put("01", new Item("How much is that doggy in the window", "Zee-J", 3));
    library.put("02", new Item("Exotic", "Maradonna", 5));
    library.put("03", new Item("I'm dreaming of a white Christmas", "Ludwig van Beethoven", 2));
    library.put("04", new Item("Pastoral Symphony", "Cayley Minnow", 1));
    library.put("05", new Item("Anarchy in the UK", "The Kings Singers", 0));
}


public static String[] getLibrary() {
String [] tempa = (String[]) library.keySet().toArray(new String[library.size()]);
return tempa;
}

SOLUTION:

Because I've to pass the values to another class:

JList tracks = new JList(LibraryData.getLibrary());

I made something like that and it's works

public static Object[] getLibrary() {
Collection c = library.values();
return c.toArray(new Item[0]);

Thank You guys, after 10 hours I finally done it! }

5
  • 1
    There is one answer here. stackoverflow.com/questions/16246821/… Commented Jan 18, 2014 at 2:13
  • Please, post the code where you are trying to get the Item. Commented Jan 18, 2014 at 2:14
  • Does library.get("01"); not work? Commented Jan 18, 2014 at 2:20
  • I believe an enum would be more appropriate. Commented Jan 18, 2014 at 2:23
  • @Christian I've got only that bit in my GUI class JList tracks = new JList(LibraryData.getLibrary()); Commented Jan 18, 2014 at 2:33

4 Answers 4

1

With this code that you have:

String [] tempa = (String[]) library.keySet().toArray(new String[library.size()]);

You are getting all keys from the map. If you want all values, then use:

library.values();

Finally, if you need to get a value by key use V get(Object key):

library.get("01");

Which will return you the first Item from the map.

It's not very clear which one of these you want, but basically these are the options.

** EDIT **

Since you want all values you can do this:

library.values().toArray()

JList expects an array or vector of Object so this should work.

Sign up to request clarification or add additional context in comments.

1 Comment

I want to get each of those songs, into the JList. I was trying to change library.keySet() to library.values(), but then I get exception. The bit of code in GUI is: JList tracks = new JList(LibraryData.getLibrary());
-1

If you want to get value and key by position, you can use:

key: library.keySet().toArray()[0]

value: library.get(key);

OR (if you just want value)

 library.values().toArray()[0]; 

Comments

-1

You can use the ArrayList:

1 - The best for flexible-array managing in Java is using ArrayLists

2 - ArrayLists are easy to add, get, remove and more from and to.

3 - Treemaps are a little... arbitrary. What I say is that if you use the get(Object o) method from a Treemap, the Object o must be a key, which is something not very flexible.

If you want them, use this code:

import java.util.ArrayList;
import com.example.Something; // It can be ANYTHING
//...
ArrayList<Something> somethingList = new ArrayList<Something>();
//...
somethingList.add(new Something("string", 1, 2.5, true));
//...
boolean isSomething = somethingList.get(somethingList.size() - 1); // Gets last item added
//...
int listSize = somethingList.size();
//...
somethingList.remove(somethingList.size() - 1); // Removes last item and decrements size
//...
Something[] nativeArray = somethingList.toArray(new Something[somethingList.size()]); // The parameter is needed or everthing will point to null
// Other things...

Or the classic Treemap:

Object keyAtIndex0 = library.keySet.toArray(new Object[library.size()])[0];
Object value = library.get(keyAtIndex0);

Good Luck!

Comments

-1

I was returning a list of string values as treemap value. The used approach is

 private Map<String, TreeSet<String>> result;

    TreeSet<String> names=  result.get(key);
     for(String contactName: names){
      print contactName;
     }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.