动态搜索 TreeSet 中的记录 [英] Searching for a record in a TreeSet on the fly

查看:44
本文介绍了动态搜索 TreeSet 中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swing 和 awt 库用 Java 编写通讯录应用程序.应用程序由一个 JList 组成,它使用一个 TreeSet 作为抽象列表模型.

I'm writing a contact book application in Java using the swing and awt libraries. The Application consists of a JList which uses a TreeSet as an abstractListModel.

TreeSet 用于一个名为 Contact 的类,该类具有私有比较器类,可根据联系人的名字对联系人进行排序.private boolean equals(Object o) 如果 Contact 的 mobileNumber 与 O 的相同(当然是在转换之后),则该方法返回 true.

The TreeSet is for a class called Contact, which has private comparator class that sorts contacts based on their first name. the private boolean equals(Object o) method returns true if the Contact has the same mobileNumber as that of O (after casting, of course).

我想在此应用程序中添加搜索功能.我进行了搜索 JTextField 并添加了一个 keyListener,我想要做的是在按下每个键后,列表会显示一组包含搜索词的缩小结果集.TreeSet 或任何其他集合中是否有此方法?我希望它类似于您在 iPod 中的音乐应用程序中所拥有的内容,例如,当您键入字母f"时,它会列出所有包含字母 F 的歌曲,但仅当您键入50 cent"时才会列出您想要的歌手的歌曲出现.

I want to add a search functionality into this application. I've made a search JTextField and added a keyListener and what I want to do is that after each key is pressed, the list displays a narrow down set of results which contains the search terms. Is there a method for this in TreeSet or any other Collection? I want it to be similar to what you have in the Music Application in iPods, where when you type the letter 'f', for example, it lists all the songs that contain the letter F but it's only when you type 'fifty cent' that the songs by the singer you want appear.

感谢您的帮助.

推荐答案

如果要查找所有以文本开头的条目(例如f"),可以使用 subSet(from, to) 方法,像这样:

If you want to find all entries that start with the text (e.g. "f"), you can use the subSet(from, to) method, like this:

SortedSet<String> s = new TreeSet<String>(new Comparator<String>() {
  public int compare( String s1, String s2 ) {
    return s1.compareToIgnoreCase( s2 );
  }

});


s.add( "Erich" );
s.add( "Erica" );
s.add( "Erin" );
s.add( "Dave" );
s.add( "Thomas" );

SortedSet<String> result = s.subSet( "e", "e" + Character.MAX_VALUE ); //"e" represents the user input
System.out.println(result);//prints [Erica, Erich, Erin]

result = s.subSet( "Eric", "Eric" + Character.MAX_VALUE );
System.out.println(result); //prints [Erica, Erich]

result = s.subSet( "Erich", "Erich" + Character.MAX_VALUE );
System.out.println(result); //prints [Erich]

因为 to 参数 subset(from, to) 是独占的,所以你需要一些明显更大的参数.在我的示例中,我只是添加了 Character.MAX_VALUE 但您可能希望获得更好的上限.请注意,这取决于您的比较器,例如它如何处理大小写差异等

Since the toparameter to subset(from, to) is exclusive, you need something that will clearly be greater. In my example I simply added Character.MAX_VALUE but you might want to get some better upper bound. Note that this depends on your comparator, e.g. how it handles case differences etc.

如果您想使用通配符进行过滤,例如所有包含文本的文本(例如 f 将转换为 *f*),您无论如何都必须迭代并检查所有条目.在这种情况下,使用排序集不会有任何优势.

If you want to filter using wildcards, like all texts containing the text (e.g. f would translate to *f*), you'd have to iterate over and check all the entries anyways. In that case you don't get any advantage using a sorted set.

将示例更新为您的数据(也添加我:)).

updated the example to your data (adding me as well :) ).

这篇关于动态搜索 TreeSet 中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆