Java 7 はラムダ式をサポートしていません。'GetMinOfList' と 'GetMaxOfList' の 2 つの同様のメソッドを最適化するにはどうすればよいですか?
package com.example;
import java.util.ArrayList;
import java.util.List;
public class MinMax<T extends Comparable<T>> {
private List<T> lst = null;
public MinMax(List<T> com)
{
this.lst = com;
}
public Pair<T, T> GetMinMaxOfList()
{
return GetMinMaxOfList(this.lst);
}
private Pair<T, T> GetMinMaxOfList(List<T> list)
{
if(list == null || list.size() == 1)
return null;
//Pair<T, T> minMax = new Pair<T, T>();
T min, max;
if(list.size() == 2)
{
if(list.get(0).compareTo(list.get(1)) < 0)
{
min = list.get(0);
max = list.get(1);
return new Pair<T,T>(min, max);
}
}
//T sentry = list.get(0);
min = GetMinOfList(list);
max = GetMaxOfList(list);
return new Pair<T, T>(min, max);
}
private T GetMinOfList(List<T> littleList)
{
T sentry = littleList.get(0);
if(littleList.size() == 1)
return sentry;
List<T> nextLittle = new ArrayList<T>(1);
for(T t: littleList)
{
if(t.compareTo(sentry) < 0)
nextLittle.add(t);
}
if(nextLittle.size() == 0)
return sentry;
return GetMinOfList(nextLittle);
}
private T GetMaxOfList(List<T> lagerList)
{
T sentry = lagerList.get(0);
if(lagerList.size() == 1)
return sentry;
List<T> nextLarge = new ArrayList<T>(1);
for(T t: lagerList)
{
if(t.compareTo(sentry) > 0)
nextLarge.add(t);
}
if(nextLarge.size() == 0)
return sentry;
return GetMaxOfList(nextLarge);
}
}