Reputation: 541
Can anyone tell me how to implement CONTAINS
type feature in java collections. My problem is that I've a list of names.
For e.g:- List<String> nameList = new ArrayList<String>();
This list is having, say 100 elements. Lets say I've a name Paras Anand
in the list. Now if I search for ara ana
or ana ara
, I should get the result. Its something like CONTAINS
clause used in SQL that I want.
Upvotes: 2
Views: 1779
Reputation: 13066
EDIT
Editing this code after long time . I just saw the CONTAINS
functionality in Oracle today and felt the need to update this code to behave exactly the way it behaves in SQL
.
You can try like this:
import java.util.*;
public class Reg
{
public int contains(String val)
{
List<String> records = new ArrayList<String>();
String arr[] = val.split("\\s");
boolean found = false;
for (String value : list) //Let list be the object of that Collection , say ArrayList
{
found = false;
String[] valArr = value.split("\\s");
int counter = 0;
int i = 0;
for (i = 0 ; i < valArr.length ; i++ )
{
if (counter == arr.length)
{
if (found)
{
break;
}
}
if (valArr[i].contains(arr[counter]))
{
counter++;
found = true;
}
else
found = false;
}
if ( i < arr.length )
{
found = false;
}
if (found)
{
records.add(value);
}
}
return records.size();//Now returning the total number of record matching the input String..
}
ArrayList<String> list = new ArrayList<String>()
{
{
add("John fine here");
add("oh ere");
add("Fox channel scam");
add("Michael sam");
add("Michael");
}
};
public static void main(String st[])
{
Reg re = new Reg();
System.out.println(re.contains("el am"));
}
}
Upvotes: 0
Reputation: 29816
Here is another solution: more elegant way and little complected one :) :
The custom ArrayList
implementation for overriding contains
and containsAll
:
import java.util.ArrayList;
import java.util.Collection;
public class CustomStringList extends ArrayList<String> {
private static final long serialVersionUID = -3513906584235908802L;
public CustomStringList(Collection<String> aist) {
super(aist);
}
@Override
public boolean contains(Object obj) {
String paramString = (String) obj;
for (String string : this) {
if (string.toLowerCase().indexOf(paramString.toLowerCase()) != -1) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection<?> collection) {
for (Object obj : collection) {
if (!contains(obj)) {
return false;
}
}
return true;
}
}
The main Controller
for testing:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Controller {
private static List<String> names = new ArrayList<String>();;
static {
names.add("shyamali bose");
names.add("SHYAMALI BOSE");
names.add("aShoK BoSE");
}
private static List<String> contains(String search) {
CustomStringList searchParts = new CustomStringList(Arrays.asList(search.split("\\s")));
List<String> matcheResults = new ArrayList<String>();
for(String name : names) {
CustomStringList nameParts = new CustomStringList(Arrays.asList(name.split("\\s")));
if(nameParts.containsAll(searchParts)) {
matcheResults.add(name);
}
}
return matcheResults;
}
public static void main(String... args) {
String searchString = "ose ose";
System.out.println("Matched for: " + searchString + " | " + (contains(searchString).size() > 0));
searchString = "ame osa";
System.out.println("Matched for: " + searchString + " | " + (contains(searchString).size() > 0));
searchString = "ama OSE";
System.out.println("Matched for: " + searchString + " | " + (contains(searchString).size() > 0));
searchString = "ose AMA";
System.out.println("Matched for: " + searchString + " | " + (contains(searchString).size() > 0));
}
}
Output:
Matched for: ose ose | true
Matched for: ame osa | false
Matched for: ama OSE | true
Matched for: ose AMA | true
Feel free to ask if you have any doubts.
Upvotes: 2