2013-03-07 81 views
2

任何人都可以告訴我如何在Java集合中實現CONTAINS類型的功能。我的問題是我有一個名稱列表。 例如: - List<String> nameList = new ArrayList<String>(); 這個列表有100個元素。可以說我在名單中有一個名字Paras Anand。現在,如果我搜索ara anaana ara,我應該得到結果。它就像我想要的SQL中使用的CONTAINS子句。在Java集合中實現CONTAINS子句

+5

[你試過什麼?](http://whathaveyoutried.com) – jlordo 2013-03-07 11:27:40

+0

用空格分割搜索字符串,對列表中每個元素的每個部分逐個做一個indexof,返回所有匹配的 – Patashu 2013-03-07 11:29:42

+2

創建一個名爲'contains'的自己的方法,並在'contains'方法中傳遞想要查找的字符串作爲參數。遍歷該列表中存在的所有元素,並使用'String.contains()'方法匹配字符串。如果它返回true,則將獲得的值放在List中,並且在循環完成後將該列表返回給程序。 – 2013-03-07 11:30:39

回答

0

編輯
長時間編輯此代碼。我剛剛在Oracle中看到了CONTAINS的功能,並且覺得需要更新此代碼以表現其在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")); 
    } 
} 
+0

@ paras2682:請參閱更新的答案.. – 2013-03-07 12:22:24

1

這裏是另一種解決方案:更優雅的方式和小膚色的一個:):

定製ArrayList實施覆蓋containscontainsAll

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 

隨意問,如果您有任何疑問。

+1

爲什麼它有0個upvotes?這似乎很棒。 – anon58192932 2014-11-26 01:10:03