删除Java中的重载方法

有2种重载方法。
这些方法中的每一种都将一种类型的列表转换为另一种类型的列表。但是第一种方法使用比较器。

class Someclass {
    public static <T,G> List<G> toListOfNewType(List<T> inputList,Function<T,G> mapperFunction,Comparator<? super G> comparator) {
           return Stream.ofNullable(inputList)
                        .flatMap(List::stream)
                        .map(mapperFunction)
                        .sorted(comparator)
                        .collect(Collectors.toList());
    }
    public static <T,G> mapperFunction) {
           return Stream.ofNullable(inputList)
                        .flatMap(List::stream)
                        .map(mapperFunction)
                        .collect(Collectors.toList());
    }
}

如您所见,大多数行都是重复的。我如何摆脱第二种方法,以便将null作为比较器传递给第一种方法不会破坏它?

换句话说,如何在没有比较器的情况下使第一个工作正常?

wcr997 回答:删除Java中的重载方法

使用if语句检查null

public static <T,G> List<G> toListOfNewType(List<T> inputList,Function<T,G> mapperFunction,Comparator<? super G> comparator) {
    Stream<G> stream = Stream.ofNullable(inputList)
            .flatMap(List::stream)
            .map(mapperFunction);
    if (comparator != null)
        stream = stream.sorted(comparator);
    return stream.collect(Collectors.toList());
}

public static <T,G> mapperFunction) {
    return toListOfNewType(inputList,mapperFunction,null);
}
,

虽然我的目标是消除实现方面的代码重复,但我考虑为null分配特殊含义,而不是将重载方法清除向错误的方向。

为了方便调用者,您仍然可以有两种方法,实现中无需重复代码,例如:

public static <T,Comparator<? super G> comparator) {
  List<G> resultList = toListOfNewType(inputList,mapperFunction);
  if(!resultList.isEmpty()) resultList.sort(comparator);
  return resultList;
}
public static <T,G> mapperFunction) {
  return inputList==null? Collections.emptyList():
    inputList.stream().map(mapperFunction).collect(Collectors.toCollection(ArrayList::new));
}

我什至会考虑放弃对null输入列表的支持,因为这只会导致隐藏问题而不是解决问题。

一种替代方法是:

public static <T,Comparator<? super G> comparator) {
  return toListOfNewTypeImpl(inputList,s -> s.sorted(comparator));
}
public static <T,G> mapperFunction) {
  return toListOfNewTypeImpl(inputList,UnaryOperator.identity());
}
private static <T,G> List<G> toListOfNewTypeImpl(List<T> inputList,UnaryOperator<Stream<G>> lastOp) {
  return lastOp.apply(inputList.stream().map(mapperFunction)).collect(Collectors.toList());
}
,

我可能只是用很小的输入就对它进行了测试(当然,即使不需要时,执行元素比较的效率也较低),但是可能可以做到这一点:

public static <T,(a,b) -> 0);
}
,

在我的团队中也要进行一些代码审查,这将是一件有趣的事情……这里的问题是,如果您允许Enumerable nullable,那么您将达到目的。 Comparatorargument,而Comparator,是自然顺序排序的完全有效含义,这意味着完全有效:

null

即使您的内部实现不允许这样做,这也意味着:

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);

Collections.sort(list,null);

会抛出List.of(4,3,2) .stream() .sorted(null) .forEachOrdered(System.out::println);

因此除非您明确记录了此方法的意图,否则用户可能会感到困惑(我会)。

恕我直言,“重复”代码并不多,您可以通过执行以下操作来强制用户传递一个返回{something” NullPointerException的{​​{1}}:

Function

缺点是1)重复代码-试图避免所有代码似乎意味着2)这些不能重载的方法-因为擦除相同,并且编译器不允许这样做。

但是,如上所述,这种方法非常主观。

,

我认为没有简单的方法可以做到这一点,或者我不知道。但是,如果您想尝试我的图书馆:abacus-util,可以执行以下操作:

StreamEx.of(inputList)
    .map(mapperFunction)
    .__(s -> comparator == null ? s : s.sort(comparator))
    .toList()
本文链接:https://www.f2er.com/3131797.html

大家都在问