且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Func键和行动Java的等价物

更新时间:2022-10-15 08:22:02

在Java 8,等同是java.util.function.Function<T, R&GT; 和java.util.function.Consumer<T>$c$c>分别接口。同样,java.util.function.$p$pdicate<T>$c$c>相当于系统predicate&LT; T&GT; 。正如在其他地方所提到的,这些都是接口,而不是代表。

搁置相关:我目前厚达以下工具类做LINQ般的扩展方法的东西:

 抽象类IterableUtil {
  公共静态&LT; T&GT;可迭代&LT; T&GT;其中,(可迭代&LT; T&GT;项目,predicate&LT; T&GT; predicate){
    ArrayList的&LT; T&GT;结果=新的ArrayList&LT; T&GT;();
    对于(T项目:项目){
      如果(predicate.test(项目)){
        result.add(项目);
      }
    }
    返回结果;
  }

  公共静态&LT; T,R&GT;可迭代&LT; R&GT;选择(可迭代&LT; T&GT;的项目,功能与LT; T,R&GT; FUNC){
    ArrayList的&LT; R&GT;结果=新的ArrayList&LT; R&GT;();
    对于(T项目:项目){
      result.add(func.apply(项目));
    }
    返回结果;
  }
}
 

System.Linq.Enumerable.Where&LT; TSource&GT; System.Linq.Enumerable.Select&LT; TSource,TResult&GT; 的LINQ样的方法我在这里present不懒惰,完全遍历源集合返回结果集给调用者之前。不过,我觉得他们很有用纯语法的目的,并且可以发懒,如果有必要的。鉴于

 类的Widget {
  公共字符串名称(){/ * ... * /}
}
 

我们可以做到以下几点:

 名单,其中,窗口小部件&GT;小工具= / * ...... * /;
可迭代&LT;窗​​口小部件&GT; filteredWidgets = IterableUtil.select(小部件,W  - &GT; w.name()startsWith(some- preFIX));
 

这点我preFER以下内容:

 名单,其中,窗口小部件&GT;小工具= / * ...... * /;
名单&LT;窗​​口小部件&GT; filteredWidgets =新的ArrayList&LT;窗​​口小部件&GT;();
对于(部件w:窗口小部件){
  如果(w.name()。startsWith(some- preFIX)){
    filteredWidgets.add(瓦特);
  }
}
 

What are Java's equivalents of Func and Action?

I mean, instead of writing this on my own:

public interface Func<TInput, TResult>
{
    TResult call(TInput target) throws Exception;
}
public interface Action<T>
{
    void call(T target) throws Exception;
}

In Java 8, the equivalents are the java.util.function.Function<T, R> and java.util.function.Consumer<T> interfaces respectively. Similarly, java.util.function.Predicate<T> is equivalent to System.Predicate<T>. As mentioned elsewhere, these are interfaces instead of delegates.

Related aside: I'm currently leaning heavily on the following utility class to do LINQ-like extension method stuff:

abstract class IterableUtil {
  public static <T> Iterable<T> where(Iterable<T> items, Predicate<T> predicate) {
    ArrayList<T> result = new ArrayList<T>();
    for (T item : items) {
      if (predicate.test(item)) {
        result.add(item);
      }
    }
    return result;
  }

  public static <T, R> Iterable<R> select(Iterable<T> items, Function<T, R> func) {
    ArrayList<R> result = new ArrayList<R>();
    for (T item : items) {
      result.add(func.apply(item));
    }
    return result;
  }
}

Unlike System.Linq.Enumerable.Where<TSource> and System.Linq.Enumerable.Select<TSource, TResult> the LINQ-like methods I present here are not lazy and fully traverse the source collections before returning the result collections to the caller. Still, I find them useful for purely syntactic purposes and could be made lazy if necessary. Given

class Widget {
  public String name() { /* ... */ }
}

One can do the following:

List<Widget> widgets = /* ... */;
Iterable<Widget> filteredWidgets = IterableUtil.select(widgets, w -> w.name().startsWith("some-prefix"));

Which I prefer to the following:

List<Widget> widgets = /* ... */;
List<Widget> filteredWidgets = new ArrayList<Widget>();
for (Widget w : widgets) {
  if (w.name().startsWith("some-prefix")) {
    filteredWidgets.add(w);
  }
}