0

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");
 }
 };

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);
}
Aravind Yarram
80.5k49 gold badges239 silver badges335 bronze badges
asked Jan 6, 2011 at 6:17

3 Answers 3

3

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 PredTest1ドル.class).

answered Jan 6, 2011 at 6:53
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Jan 6, 2011 at 6:27

Comments

0

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
answered Jan 6, 2011 at 6:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.