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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21199193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:15:32  来源:igfitidea点击:

How to get value from TreeMap in Java?

javaarraysjlisttreemap

提问by rewen

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.

我的问题是无法从我的树状图中获取对象“项目”(值)。我需要将该信息发送到我的 GUI 类并将其显示在 JList 中以获取选择列表,因此可以轻松地选择歌曲并将其添加到播放列表中,但只有我作为输出获得的是“01、02、03、04、05” (钥匙)。请帮忙,因为我是初学者,不知道该怎么做。

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个小时后我终于完成了!}

回答by y?s??la

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 Itemfrom 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 向量,所以这应该可以工作。

回答by Hao Luong

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

如果要按位置获取值和键,可以使用:

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

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

value: library.get(key);

值:library.get(key);

OR (if you just want value)

或(如果您只想要价值)

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

回答by alesc3

You can use the ArrayList:

您可以使用ArrayList

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

1 - Java 中灵活数组管理的最佳方式是使用ArrayLists

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

2 - ArrayLists 很容易添加、获取、删除以及更多。

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

3 - Treemaps 有点……随意。我要说的是,如果您使用get(Object o)Treemap 中的方法,则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!

祝你好运!

回答by rajeesh

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