How to make a fuzzy query in the Java list

Source: Internet
Author: User

For example, I have the following list, which contains multiple employee objects. Then I want to do a fuzzy query on this list according to the name of the Employee object. Do you have any good solutions?
For example, if I enter a query condition of "Wang", you should return a list containing only employee1.

Newnew  Employee (); Employee1.setname ("Wangqiang"); Employee1.setage (30  New  Employee (); Employee2.setname ("Lisi"); List.add (employee2); Employee2.setage ( 25);

Way One:

 Public List Search (String name,list List) {   new  ArrayList ();    = Pattern.compile (name);     for (int i=0; i < list.size (); i++) {      = pattern.matcher ((Employee) list.get (i)). GetName ( ));       if (Matcher.matches ()) {         Results.add (List.get (i));      }   }    return results;}

The upper one is case sensitive, and if the case is not sensitive, change to:
Pattern pattern = pattern.compile (name,pattern.case_insensitive);

And the above is the exact query, if you want to fuzzy matching, matcher.find () can be fuzzy matching

 Public List Search (String name,list List) {   new  ArrayList ();    = Pattern.compile (name);     for (int i=0; i < list.size (); i++) {      = pattern.matcher ((Employee) list.get (i)). GetName ( ));       if (Matcher.find ()) {         Results.add (List.get (i));      }   }    return results;}

Way two:

 Public classListlike {//Define Employee Classes Public classEmployee {PrivateString name;Private intAge ; Public intGetage () {returnAge ;} Public voidSetage (intAge ) { This. Age =Age ;} PublicString GetName () {returnname;} Public voidsetName (String name) { This. Name =name;}} PublicList list=NewArrayList ();//Increase Staff PublicList addlist (String name,intAge ) {Employee Employee1=NewEmployee (); Employee1.setname (name); Employee1.setage (age); List.add (employee1) ;returnlist;}//Show All Employees Public voidshowlist () { for(intI=0;i<list.size (); i++) {System.out.println ((Employee) (List.get (i)). GetName ()+" "+( Employee) (List.get (i))). Getage ());}}//Fuzzy Query PublicList likestring (String likename) { for(intI=0;i<list.size (); i++){if((Employee) (List.get (i)). GetName (). IndexOf (Likename) <=-1) List.remove (i);}returnlist;} Public Static voidMain (String arg[]) {listlike ll=Newlistlike (); Ll.addlist ("Wuxiao", 13); Ll.addlist ("Wangwang", 11); Ll.addlist ("Wanghua", 12); Ll.addlist ("Xiaowang", 13); Ll.addlist ("Xiaoxiao", 13); Ll.likestring ("Wang"); LL. Showlist ();}}




How to make a fuzzy query in the Java list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.