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.
List List = new ArrayList (); Employee Employee1 = new Employee (), Employee1.setname ("Wangqiang"), Employee1.setage (List.add); Employee Employee2 = new Employee (), Employee2.setname ("Lisi"), List.add (employee2); Employee2.setage (25);
Way One:
Public List Search (String name,list list) { list results = new ArrayList (); Pattern pattern = pattern.compile (name); for (int i=0; i < list.size (); i++) { Matcher Matcher = 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) { list results = new ArrayList (); Pattern pattern = pattern.compile (name); for (int i=0; i < list.size (); i++) { Matcher Matcher = Pattern.matcher ((Employee) list.get (i)). GetName ()); if (Matcher.find ()) { Results.add (List.get (i)); } } return results;}
Way two:
public class Listlike {//Define Employee class public class Employee {private String name;private int age;public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}} Public list List=new ArrayList ()//Add Employee public list addlist (String Name,int age) {Employee employee1 = new Employee (); Emplo Yee1.setname (name); Employee1.setage (age); List.add (employee1); return list;} Show all employees public void Showlist () {for (int i=0;i<list.size (); i++) {System.out.println ((Employee) (List.get (i))). GetName () + "" + ((Employee) (List.get (i))). Getage ());}} The fuzzy query public lists likestring (String likename) {for (int i=0;i<list.size (); i++) {if ((Employee) (List.get (i))). GetName (). IndexOf (likename) <=-1) list.remove (i);} return list;} public static void Main (String arg[]) {listlike ll=new listlike (); Ll.addlist ("Wuxiao", Ll.addlist ("Wangwang"); Ll.addlist ("Wanghua"), Ll.addlist ("Xiaowang"), Ll.addlist ("Xiaoxiao"); Ll.likestring ("Wang")); LL. Showlist ();}}
How to make a fuzzy query in the Java list