谁能告诉我如何CONTAINS
在 java 集合中实现类型功能。我的问题是我有一个名字列表。例如:-List<String> nameList = new ArrayList<String>();
这个列表有 100 个元素。假设我Paras Anand
在列表中有一个名字。现在,如果我搜索ara ana
or ana ara
,我应该得到结果。它类似于CONTAINS
我想要的 SQL 中使用的子句。
问问题
1269 次
2 回答
2
这是另一种解决方案:更优雅的方式和简单的方式:):
覆盖and的自定义ArrayList
实现:contains
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;
}
}
主要Controller
用于测试:
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));
}
}
输出:
Matched for: ose ose | true
Matched for: ame osa | false
Matched for: ama OSE | true
Matched for: ose AMA | true
如果您有任何疑问,请随时询问。
于 2013-03-07T18:12:01.380 回答
0
编辑
长时间后编辑此代码。我今天刚刚看到CONTAINS
Oracle 中的功能,并觉得有必要更新此代码以使其行为与SQL
. 你可以这样尝试:
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"));
}
}
于 2013-03-07T11:38:41.240 回答