开发者

Need a clarification on Implementing interface methods inside a class

开发者 https://www.devze.com 2023-02-02 23:48 出处:网络
I would like to know the what type of concept is used in the below code for implementing interface method in the class \"PredTest\".

I would like to know the what type of concept is used in the below code for implementing interface method in the class "PredTest".

 static Predicate pred = new Predicate() {
      public boolean predicate(Object o) {
       return o.toString().startsWith("Hi");
      }
    开发者_C百科 };

Full Code

import java.util.*;

public class PredTest  {
 static Predicate pred = new Predicate() {
  public boolean predicate(Object o) {
   return o.toString().startsWith("Hi");
  }
 };

 public static void main(String args[]) {
  String [] names = {"java", "ProdTest", "One", "Two", "Hi", "Three", "Four", "High", "Six", "Seven", "Higher"};
  List<String> list = Arrays.asList(names);
  Iterator<String> i1 = list.iterator();
  Iterator<String> i = new PredicateIterator(i1, pred);
  while (i.hasNext()) {
   System.out.println(i.next());
  }
 }
}

class PredicateIterator implements Iterator<String> {
 Iterator<String> iter;
 Predicate pred;
 String next;
 boolean doneNext = false;

 public PredicateIterator(Iterator<String> iter, Predicate pred) {
  this.iter = iter;
  this.pred = pred;
 }

 public void remove() {
  throw new UnsupportedOperationException();
 }

 public boolean hasNext() {
  doneNext = true;
  boolean hasNext;
  while (hasNext = iter.hasNext()) {
   next = iter.next();
   if (pred.predicate(next)) {
    break;
   }
  }
  return hasNext;
 }

 public String next() {
  if (!doneNext) {
   boolean has = hasNext();
   if (!has) {
    throw new NoSuchElementException();
   }
  }
  doneNext = false;
  return next;
 }
}

interface Predicate {
 boolean predicate(Object element);
}


I believe the term you are looking for is 'anonymous inner class'. These result in the the $ output files from the compiler(in this case PredTest$1.class).


I don't know if this answers your question exactly, but Google Guava has Iterables.filter which does what your custom iterator is doing there:

public class PredTest {
  private static Predicate<String> startsWithPredicate = new Predicate<String>() {
    @Override public boolean apply(String input) {
      return input.startsWith("Hi");
    }
  };

  public static void main(String[] args) {
    List<String> someList = ImmutableList.of("Hello", "Hi Java", "Whatever", "Hi World");
    System.out.println(Joiner.on(", ").join(Iterables.filter(someList, startsWithPredicate)));
  }
}

This code should print

Hi Java, Hi World

There are even some pre-defined Predicate implementations in Guava, e.g. Predicates.contains(someRegex), see the Javadoc for class Predicates.


This is known as a Functor or Function Object and is kind of functional programming style. You can think of it is an object equivalent of an if statement. It uses the input object (in your example String) to return a true or false value, and is often used in validation or filtering (in your example you are using for filtering).

In Java, Comparator can be thought of an example of a Predicate. Certain libraries (listed below) provides some support for Functors

  1. commons collections
  2. google-guava
  3. functional java
0

精彩评论

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

关注公众号