开发者

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

开发者 https://www.devze.com 2023-04-02 01:20 出处:网络
I\'m writing a contact book application in Java. The Contacts are displayed on a JList which uses a Sorted TreeSet list model.

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.


If you don't need concurrency don't use Vector use ArrayList it's not synchronized thus faster.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号