autocomplete - how to implement Custom Auto suggestion in java like google -


how implement java based auto suggestion. suppose have different types of data firstname, rollnumber, address.

my first requirement if user enter first character on text box, result should sorted on natural order based on firstname , 10 results should display.

after space if use enter second character , if numbere rollnumber else lastname should sorted on natural order ascending.

or if user type third character address should display on ascending order. there should no database, don't have implement solr or other api. how implement on pure java.

here did not implement text-box,but took example demonstrate

    import java.util.*;     import java.lang.*;     import java.io.*;      // class represent student.     class student {         int rollno;         string name;         string address;          // constructor         public student(int rollno, string name, string address) {             this.rollno = rollno;             this.name = name;             this.address = address;         }          // used print student details in main()         public string tostring(){             return this.rollno + " " + this.name +                                " " + this.address;         }     }      class sortbyroll implements comparator<student> {         // used sorting in ascending order of rollno         public int compare(student a, student b) {             return a.rollno - b.rollno;         }     }      class sortbyname implements comparator<student> {         // used sorting in ascending order of name         public int compare(student a, student b) {             return a.name.compareto(b.name);         }     }      // driver class     class main {         public static void main (string[] args) {             arraylist<student> ar = new arraylist<student>();            //here have thousand student inserted             //simple collection.              ar.add(new student(111, "bbbb", "london"));             ar.add(new student(131, "aaaa", "nyc"));             ar.add(new student(121, "cccc", "jaipur"));              system.out.println("unsorted");             (int i=0; i<ar.size(); i++) {                 system.out.println(ar.get(i));             }              //collection sorted rollno                 collections.sort(ar, new sortbyroll());              system.out.println("\nsorted rollno");             (int i=0; i<ar.size(); i++) {                 system.out.println(ar.get(i));             }              //sort name             collections.sort(ar, new sortbyname());              system.out.println("\nsorted name");             (int i=0; i<ar.size(); i++) {                 system.out.println(ar.get(i));             }         }     } 

first of question incomplete , misleading. not describes requirement properly. overall assume

you want google (?) suggester in text box

it not tell specific things. front end ? how data ?

any way think wanted have console application give partial string input , method guess rest of string assumption dummy data. right ?

if thing looking sketched demo code below

static list<string> query(string querystr, list<student> list) {         list<string> suggestion = new arraylist<>();         list.foreach(std -> {             if (ismatched(querystr, string.valueof(std.getroll()))) {                 suggestion.add(string.valueof(std.getroll()));             }              if (ismatched(querystr, std.getname())) {                 suggestion.add(std.getname());             }              if (ismatched(querystr, std.getaddress())) {                 suggestion.add(std.getaddress());             }         });          return suggestion;     }      private static boolean ismatched(string query, string text) {         return text.tolowercase().contains(query.tolowercase());     } 

and code ? takes partial string user input far , list<student> parameters. iterates on list , matches field partial match. if field matches query add value in suggestion list. in main can :

public static void main(string[] args) {          list<student> list = new arraylist<>();         list.add(new student(101, "abc ghi", "usa"));         list.add(new student(102, "def", "ust"));         list.add(new student(103, "ghi ab", "dsjkd"));         list.add(new student(104, "jkl ut", "usn"));         list.add(new student(105, "mnp", "tsa101"));         list.add(new student(106, "utc abc", "esa"));          list<string> sugg = query("01", list);         sugg.foreach(system.out::println);     } 

and find console printed :

101 tsa101 

does make sense ? might not whole confusing requirements. think got idea. can exploit address own requirements. further imply sorting logic or kind of filters it. should not tough thing.

but should concerned large number of collection or complex associated objects not suffice. real world application not work straight forward. might need lot of other things consider memory, i/o , execution time.

good luck!


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -