For example, I have the following list, which holds multiple employee objects. Then I want to make a fuzzy query about the list by the name of the Employee object. Do you have any good solutions?
For example, if the query I entered is "Wang", then you should return a list containing only employee1.
List List = new ArrayList ();
Employee Employee1 = new Employee ();
Employee1.setname ("Wangqiang");
Employee1.setage (a);
List.add (employee1);
Employee Employee2 = new Employee ();
Employee2.setname ("Lisi");
List.add (employee2);
Employee2.setage (25);
Mode 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, if the request is not sensitive to case, change to:
Pattern pattern = pattern.compile (name,pattern.case_insensitive);
And the above is the exact query, if you want to blur the match, 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;
}
Mode two:
public class Listlike {//Define Employee class public class Employee {private String name; private int-age; public int getage () {Retu
RN age; public void Setage (int age) {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 (); Employee1.setname (name); employee1.s
Etage (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 ());} Fuzzy query public List 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", 11);
Ll.addlist ("Wanghua", 12); Ll.addlist ("XiaowaNg ", 13);
Ll.addlist ("Xiaoxiao", 13);
Ll.likestring ("Wang"); ll.
Showlist (); }
}
The above is a small series for everyone to bring in the Java list in the implementation of Fuzzy Query method all content, I hope that we support cloud-Habitat Community ~