开发者

Java search-Arraylist

开发者 https://www.devze.com 2023-03-21 14:09 出处:网络
ArrayList searchList = new ArrayList(); ArrayList words=(ArrayList) request.getSession().getAttribute(\"words\");
ArrayList searchList = new ArrayList();
ArrayList words=(ArrayList) request.getSession().getAttribute("words");
words.add("one");
words.add("twenty one");
words.add("thirty one");
words.add("two");
words.add("twenty two");
words.add("thirty two");
words.add("three");
words.add("twenty three");
words.add("thirty three");'

If I have this arraylist and I want to search all the strings containing one(i.e. one开发者_Go百科,twenty one and thirty one), what logic should I use? Means how should I do that?


for (String item : searchList) {
    if (item.contains("one") {
        // Do something. Like adding the result to a different list.
        // If you need the index from the original list, you a for instead of a for each
    }
 }


//iterate through words
for(String str : list){
  //check if word contains the key
  if(str.contains(key)){ 
     //add its reference to another resultant list
     result.add(str);
  }
}


for (String word : words) {
   if (word.contains("one")) {
       //we have a match
   }
}


Of course you have to loop thru the elements. Look for ways to loop thru an ArrayList: that can be indexed or with the

for (x : collect) 

notation.

In the loop you have to do some pattern matching. Read String Java API doc for a method.

(Give'em some think food ...)


You could solve this using iterators if the condition will be more complex

    public interface IPredicate<T> {

        boolean check(T t);

    }


public class PredicatIterable<T> implements Iterable<T> {

        private final Iterator<T> iterator;
        private final IPredicate<T> predicate;

        public PredicatIterable(Iterable<T> iterable, IPredicate<T> predicate) {
            this.iterator = iterable.iterator();
            this.predicate = predicate;
        }

        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {

                T current;

                @Override
                public boolean hasNext() {

                    if(iterator.hasNext()) {
                        T next = iterator.next();

                        if(predicate.check(next)) {
                            current = next;
                            return true;
                        } 
                        current = null;
                    }

                    return false;
                }

                @Override
                public T next() {
                    return current;
                }

                @Override
                public void remove() {
                    throw new RuntimeException("Invalid useage of method");
                }



            };
        }

    }

To validate more the single predicate you can create also method that is responsible for conuntion or alternative of two IPredicate argument.


In general, when searching an item in a List, the best solution is to sort your List first using Collections.sort() method. Then using the Collections.binarySearch() method, find your element. In this case your elements are String type that are Comparable and can be sorted alphabetically otherwise you needed to implement Comparable interface for your element class type.

0

精彩评论

暂无评论...
验证码 换一张
取 消