What is Java equivalent to C ++ for_each pattern?

In the following code, you can apply any function f (for example, add, subtract, etc.). How to do it using Java?

template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function f) { for ( ; first!=last; ++first ) f(*first); return f; } 
+4
source share
4 answers

There are two main changes to this code to make it work in Java. First, you will need to replace STL style iterators with Java style iterators, which, fortunately, aren't too complicated. Secondly, you will have to change the use of a function pointer or functor parameter with an object of a different type, since Java does not support function pointers.

In Java, iterators are heavier than in C ++ and encapsulate the entire range that they go through, not just the beginning or end of that range. Using the Java iterator, you use the .next () and .hasNext () methods to move around the range.

As for the function parameter, you probably need to create your own interface representing some object that can be called a function. For instance:

 public interface Operation<T> { void apply(T argument); } 

This interface is a typical parameter for some type T, which indicates the type of argument, and then exports a function called apply () that applies this function.

Given this, a simple but naive way of for_each replication would be to write

 public static <T> forEach(Iterator<T> itr, Operation<T> op) { while (itr.hasNext()) op.apply(itr.next()); } 

The reason I say it is naive is that it does not use restricted wildcards correctly in order to expand the scope where it can be used. A more suitable version, more friendly to Java, would be:

 public static <T> forEach(Iterator<T> itr, Operation<? super T> op) { while (itr.hasNext()) op.apply(itr.next()); } 

Consult a link to Java Genetics for why this works.

Hope this helps!

+7
source

To implement the template class, you must use Java Generics

For the callback, most likely you will want to create an Interface and pass an object that implemented it in the Generic class where you would name the method defined by therin.

0
source

You should use interfaces, but if you want to do it in a complicated way using reflection (not tested, not an exception - checked, etc. But you can get this idea):

 public void myFunction(Collection items, String methodName) { foreach(Object o : items) { Method method = o.getClass().getMethod(methodName); method.invoke(o); } } 
0
source

With a good OO design, first through the latter there would be objects of a specific (implements the interface) type, and f () would take an object of this type (interface) as a parameter so that you do this - it is very similar to what you wrote.

if you are talking about doing it on primitives then you will need generics, but at this point you will want to find a better design or a better way to do it.

You did not give enough information for a more detailed answer if you knew more about the problem you were trying to solve, and about the limitations that we could give better.

0
source

Source: https://habr.com/ru/post/1344319/


All Articles