简体   繁体   中英

What's a more efficient alternative than converting a SortedSet to a Vector in Java?

I'm writing a contact book application in Java. The Contacts are displayed on a JList which uses a Sorted TreeSet list model.

I've added a search field and I've added a key listener to it. With each key entered, the subset function of the list model is used to display a narrowed down set of contacts. I want the JList to display this narrowed down subset.

I'm thinking of converting the SortedSet to a Vector and then using the JList's setListData method to display the results but I know this would be slow and inefficient, and is an even worse idea when you're using a key listener.

So I would like to ask, what's the most efficient way to solve this problem.

Thanks for your help.

I would extend AbstractListModel and use the Set/subSet directly as the source of the data displayed by the JList. Each time you replace the current subset by another one in the model, call fireContentsChanged to make the view (the JList) aware of the change.

This way, there's no need to convert the Set to a Vector.

如果您不需要并发,请不要使用Vector,而是使用ArrayList,它不会同步,因此速度更快。

You will need to bind your data (in whatever collection) to your list via its ListModel.

See section titled Lists with Dynamic Contents in Advanced JList Programming Tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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