如何将 LinkedHashMap 的键添加到 JList 中?

发布于 2024-07-24 09:50:16 字数 152 浏览 1 评论 0 原文

我有一个 LinkedHashMap,想知道将 LinkedHashMap 中每个元素的 String 部分放入其中的最有效方法是什么一个JList

I have a LinkedHashMap<String,Object> and was wondering what the most efficient way to put the String portion of each element in the LinkedHashMap into a JList.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

貪欢 2024-07-31 09:50:16

您还可以使用 setListData(Vector) 方法:

jList1.setListData(new Vector<String>(map.keySet())); 

使用每个 setListData 方法,您都会复制实际数据,因此对地图的更改不会反映在列表中。 您可以改为创建自定义 ListModel 并将其传递给 setModel 方法,但由于无法通过索引访问 LinkedHashMap 的任意元素,这可能是不可行的。

You could also use the setListData(Vector) method:

jList1.setListData(new Vector<String>(map.keySet())); 

With each of the setListData methods, you're making a copy of the actual data, so changes to the map will not be reflected in the list. You could instead create a custom ListModel and pass it to the setModel method instead, but because there is no way to access an arbitrary element of a LinkedHashMap by index, this is probably infeasible.

以歌曲疗慰 2024-07-31 09:50:16

如果jList对原始Map的变化不敏感,那么您使用的原始方法就可以(比使用Vector更好,Vector有额外的同步层)。

如果您希望 jList 在地图更改时也更改,那么您必须编写自己的 ListModel (这并不难)。 您还必须弄清楚如何知道地图何时发生变化。

If the jList is not sensitive to changes of the original Map, the original method you used is fine (better than using Vector, which has the extra layer of sychronization).

If you want jList to change when the map changes, then you will have to write your own ListModel (which is not that hard). You will also have to figure out how to know when the map changes.

俯瞰星空 2024-07-31 09:50:16

在我回答问题后就发现了这种方法,我想我会把它放在这里供社区使用。 我认为这是完成任务的最有效方法,但我当然愿意接受更多建议。

jList1.setListData(LinkedHashMap.keySet().toArray());

Found this way to do it just after I answered the question, thought I would put it up here for the community. I think that this is the most efficient way to get it done, but am certainly open to more suggestions.

jList1.setListData(LinkedHashMap.keySet().toArray());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文