简体   繁体   English

如何从Java中获取TreeMap的价值?

[英]How to get value from TreeMap in Java?

My problem is can't get an object "Item" (value) from my Treemap. 我的问题是无法从我的Treemap获取对象“Item”(值)。 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). 我需要将该信息发送到我的GUI类并在JList中显示以获得选择列表,因此可以轻松选择并添加歌曲到播放列表,但只有我得到的输出是“01,02,03,04,05” (键)。 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! 谢谢你们,10个小时后我终于完成了! } }

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) : 最后,如果需要通过键获取值,请使用V get(Object key)

library.get("01");

Which will return you the first Item from the map. 这将返回地图中的第一个Item

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. JList需要一个Object的数组或向量,所以这应该工作。

If you want to get value and key by position, you can use: 如果您想获得价值并按位置关键,您可以使用:

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

value: library.get(key); value:library.get(key);

OR (if you just want value) 或者(如果你只想要价值)

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

You can use the ArrayList : 您可以使用ArrayList

1 - The best for flexible-array managing in Java is using ArrayList s 1 - Java中灵活阵列管理的最佳方法是使用ArrayList

2 - ArrayList s are easy to add, get, remove and more from and to. 2 - ArrayList很容易添加,获取,删除和更多。

3 - Treemap s are a little... arbitrary. 3 - Treemap有点......任意。 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. 我所说的是,如果你使用Treemap中的get(Object o)方法, Object o必须是一个键,这是一个不太灵活的东西。

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 : 或者经典的Treemap

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

Good Luck! 祝好运!

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;
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM