Partially match strings in case of List.contains(String)

后端 未结 9 861
夕颜
夕颜 2020-12-10 11:23

I have a List

List list = new ArrayList();
list.add(\"ABCD\");
list.add(\"EFGH\");
list.add(\"IJ KL\")         


        
9条回答
  •  春和景丽
    2020-12-10 11:59

    As a second answer, upon rereading your question, you could also inherit from the interface List, specialize it for Strings only, and override the contains() method.

    public class PartialStringList extends ArrayList
    {
        public boolean contains(Object o)
        {
            if(!(o instanceof String))
            {
                return false;
            }
            String s = (String)o;
            Iterator iter = iterator();
            while(iter.hasNext())
            {
                String iStr = iter.next();
                if (iStr.contain(s))
                {
                    return true;
                }
            }
            return false;
        }
    }
    

    Judging by your earlier comments, this is maybe not the speed you're looking for, but is this more similar to what you were asking for?

提交回复
热议问题