Search for entries in TreeSet on the fly

I am writing a contact application in Java using swing and awt libraries. The application consists of a JList that uses a TreeSet as an abstractListModel.

TreeSet is for the Contact class, which has its own comparator class, which sorts contacts based on their name. private boolean equals(Object o) method returns true if Contact has the same mobile device number as O (after casting, of course).

I want to add a search function to this application. I did a JTextField search and added keyListener, and what I want to do is that after pressing each key the list displays a narrow set of results containing the search terms. Is there a way to do this in a TreeSet or any other collection? I want it to look like what you have in the Music application on your iPod, where, for example, when you enter the letter "f", it lists all the songs that contain the letter F, but only when you enter "fifty cents "that song of the singer you want to appear.

Thank you for your help.

+4
source share
2 answers

If you want to find all entries starting with text (for example, "f"), you can use the subSet(from, to) method, for example:

 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] 

Since the to parameter on subSet(from, to) is exceptional, you need something that will be clearly larger. In my example, I just added Character.MAX_VALUE , but you might want to get a better upper bound. Note that this depends on your comparator, for example. how it handles differences in cases, etc.

If you want to filter with wildcards, like all texts containing text (for example, f will be translated to *f* ), you will have to iterate over and check all the records anyway. In this case, you will not get any advantage using sorted set.

Edit: updated the example to your data (also adding me :)).

+11
source

You can use the boolean startsWith(String prefix) method of the java.lang.String class to check if any values ​​in the set begin with an input line.

Example:

 public void getName(Set<String> t, String s) { for(String str : t) { if(str.toLowerCase().startsWith(s.toLowerCase())) System.out.println(str); } } 

:

 Set<String> test = new TreeSet<String>(); test.add( "Erich" ); test.add( "Erica" ); test.add( "Erin" ); test.add( "Dave" ); test.add( "Thomas" ); 

if you call the method:

 getName(test, "eri"); 

the output will be:

 Erica Erich Erin 
+1
source

Source: https://habr.com/ru/post/1369459/


All Articles