My initial way: Using space as a delimiter.
We can do the following.
Actions
Create a list. Properly
1) Use the Java split function. Creation of an array.
List<String> list = new ArrayList<String>(Arrays.asList(string.split(" ")))`;
2) Create a hash map.
Map<String, String> hash = new HashMap<String, String>(); for(i = 0 ; i < list.length(); i++) { hash.put(list[i], list[i]); }
Where list[i] is your key.
3) Get matches.
Now when the user enters the word you are interested in, you can use containsKey
command. for instance
if (hash.containsKey("flour") && hash.containsKey("water") && hash.containsKey("beans"); println("Whatever you want");
It should be noted that creating a HashTable is useful for large datasets. Here is the link you must see in order to see the benefits. Retrieving data from a hash table is O (1), therefore, almost instantly.
Hope this was helpful.
source share