泛型方法参数-Java

泛型方法参数-Java,java,generics,collections,generic-collections,Java,Generics,Collections,Generic Collections,我有两个几乎相同的方法,但我试图避免代码重复 它们都以一个唯一的对象作为参数,并从中找出最高值 下面是一个例子: public Integer getHighestIndexValue(List<ObjectA> list) { int highestValue; List<Integer> indexes = new ArrayList<Integer>(); for (ObjectA a: list) { index

我有两个几乎相同的方法,但我试图避免代码重复

它们都以一个唯一的对象作为参数,并从中找出最高值

下面是一个例子:

public Integer getHighestIndexValue(List<ObjectA> list) {
    int highestValue;
    List<Integer> indexes = new ArrayList<Integer>();
    for (ObjectA a: list) {
        indexes.add(Integer.parseInt(a.getA()));
    }
    highestValue = Collections.max(indexes);

    return highestValue;

} 
我以前使用过泛型,但还没有使用泛型参数


有没有办法解决这个问题?

能否将
getA
getB
组合成一个接口方法

例如:

interface ProvidesIndex {
  Integer getIndex();
}
现在,您只需调用
getIndex
,您的方法签名将是:

public Integer getHighestIndexValue(List<? extends ProvidesIndex> list) 

您可以在一个Java命令中实现这一点。以下是Java 5和更紧凑的Java 8:

public static class ObjectA {
    public String getA() { return "1"; /* test */ }
}

public static class ObjectB {
    public String getB() { return "1"; /* test */ }
}

// Java 5+ versions

public static void sampleA7(List<ObjectA> list) {
    int maxValue = Integer.parseInt(Collections.max(list, new Comparator<ObjectA>() {
        @Override
        public int compare(ObjectA a, ObjectA b) {
            return Integer.parseInt(a.getA()) - Integer.parseInt(b.getA());
        }
    }).getA());
}

public static void sampleB7(List<ObjectB> list) {
    int maxValue = Integer.parseInt(Collections.max(list, new Comparator<ObjectB>() {
        @Override
        public int compare(ObjectB a, ObjectB b) {
            return Integer.parseInt(a.getB()) - Integer.parseInt(b.getB());
        }
    }).getB());
}

// Java 8+ versions

public static void sampleA8(List<ObjectA> list) {
    Optional<Integer> maxValue = list.stream().map(a -> Integer.parseInt(a.getA())).max((a, b) -> a - b);
}

public static void sampleB8(List<ObjectB> list) {
    Optional<Integer> maxValue = list.stream().map(a -> Integer.parseInt(a.getB())).max((a, b) -> a - b);
}
公共静态类ObjectA{
公共字符串getA(){return“1”/*test*/}
}
公共静态类ObjectB{
公共字符串getB(){return“1”/*test*/}
}
//Java5+版本
公共静态无效样本7(列表){
int maxValue=Integer.parseInt(Collections.max(列表,新比较器)(){
@凌驾
公共整数比较(ObjectA a,ObjectA b){
返回Integer.parseInt(a.getA())-Integer.parseInt(b.getA());
}
}).getA());
}
公共静态无效样本B7(列表){
int maxValue=Integer.parseInt(Collections.max(列表,新比较器)(){
@凌驾
公共整数比较(ObjectB a,ObjectB b){
返回Integer.parseInt(a.getB())-Integer.parseInt(b.getB());
}
}).getB());
}
//Java8+版本
公共静态无效样本8(列表){
可选的maxValue=list.stream().map(a->Integer.parseInt(a.getA()).max((a,b)->a-b);
}
公共静态无效样本B8(列表){
可选的maxValue=list.stream().map(a->Integer.parseInt(a.getB()).max((a,b)->a-b);
}

在Raman的回答中添加:

public Integer getHighestIndexValue(List<? extends CommonInterface> list) {
    int highestValue;
    List<Integer> indexes = new ArrayList<Integer>();

    for (CommonInterface a: list) {
        indexes.add(Integer.parseInt(a. getIndex()));
    }

    highestValue = Collections.max(indexes);

    return highestValue;
}
public Integer getHighestIndexValue(List提供了一个优雅的解决方案:

//general purpose function to get A, can be used in transform(), etc
private static final Function<ObjectA, Integer> GET_A = new Function<>() {
     @Override
     public Integer apply(ObjectA a) {
         return a.getA();
     }
}

Integer highestIndex = Collections.max(Collections2.transform(list, GET_A));
然后要将其扩展到
ObjectB
,只需实现
GET_B
函数

回到您的helper方法(这基本上是不相关的,因为您有一行代码):


public Integer getHighestIndex(List如果要使此代码通用,使用整数数组列表进行实际比较可能不是一个好主意。如果其中一个对象的长度大于您的最大整数,该怎么办?相反,您的对象必须以某种方式具有可比性,以便找到“最大值”(一组字符串的最大值是多少?)

所以我会尝试这样的方法

import java.util.List;

public abstract class MathMax<E> {
    public abstract boolean isGreaterThan(E x1, E x2);
    public E getHighestIndexValue(List<E> list){
        E highestValue = null;
        for (E a: list) {
            if (isGreaterThan(a,highestValue)){
                highestValue = a;
            }
        }
        return highestValue;
    }
}
import java.util.List;
公共抽象类MathMax{
公共抽象布尔值大于(E-x1,E-x2);
公共E getHighestIndexValue(列表){
E highestValue=null;
对于(E:列表){
如果(大于(a,最大值)){
最高值=a;
}
}
返回最高值;
}
}

import java.util.ArrayList;
导入java.util.List;
公共类MathMaxA扩展了MathMax{
@凌驾
公共布尔值大于(ObjectA x1,ObjectA x2){
//在这里进行比较,注意空值
返回false;
}
公共静态void main(字符串[]args){
MathMaxA m=新的MathMaxA();
列表=新的ArrayList();
System.out.println(m.getHighestIndexValue(列表));
}
}

同意;因为方法名称不同,使用泛型无法解决您的问题。ObjectA和ObjectB需要有一些通用接口。Roman,您关于Comparable的说明还可以,添加Comparator,它就完成了…idea(带Comparator)很好,实现不是,返回max object not int,但它是,java.util.Comparator)不使用
Integer.parseInt(a.getB())-Integer.parseInt(b.getB())
,但是,int)我认为OP的索引不是你想象的那样…@Betista你是对的,索引在对象a和对象B中都是一个变量。它可能像用户通过UIbetlista传递的1-400之间的任何地方,修复了,所以它返回值。如果你能使用Java 8,Java 8版本会更干净。@user2684301对不起,我被Java 6卡住了,或者更好我要感谢大家的回复。我必须选择这一个作为最好的答案,因为它像一个魅力,以最小的努力工作:)
public static class ObjectA {
    public String getA() { return "1"; /* test */ }
}

public static class ObjectB {
    public String getB() { return "1"; /* test */ }
}

// Java 5+ versions

public static void sampleA7(List<ObjectA> list) {
    int maxValue = Integer.parseInt(Collections.max(list, new Comparator<ObjectA>() {
        @Override
        public int compare(ObjectA a, ObjectA b) {
            return Integer.parseInt(a.getA()) - Integer.parseInt(b.getA());
        }
    }).getA());
}

public static void sampleB7(List<ObjectB> list) {
    int maxValue = Integer.parseInt(Collections.max(list, new Comparator<ObjectB>() {
        @Override
        public int compare(ObjectB a, ObjectB b) {
            return Integer.parseInt(a.getB()) - Integer.parseInt(b.getB());
        }
    }).getB());
}

// Java 8+ versions

public static void sampleA8(List<ObjectA> list) {
    Optional<Integer> maxValue = list.stream().map(a -> Integer.parseInt(a.getA())).max((a, b) -> a - b);
}

public static void sampleB8(List<ObjectB> list) {
    Optional<Integer> maxValue = list.stream().map(a -> Integer.parseInt(a.getB())).max((a, b) -> a - b);
}
public Integer getHighestIndexValue(List<? extends CommonInterface> list) {
    int highestValue;
    List<Integer> indexes = new ArrayList<Integer>();

    for (CommonInterface a: list) {
        indexes.add(Integer.parseInt(a. getIndex()));
    }

    highestValue = Collections.max(indexes);

    return highestValue;
}
//general purpose function to get A, can be used in transform(), etc
private static final Function<ObjectA, Integer> GET_A = new Function<>() {
     @Override
     public Integer apply(ObjectA a) {
         return a.getA();
     }
}

Integer highestIndex = Collections.max(Collections2.transform(list, GET_A));
ObjectA highest = Ordering.natural().onResultOf(GET_A).max(list);
public <T> Integer getHighestIndex(List<? extends T> list, Function<? super T, Integer> indexPlucker) {
    return Collections.max(Collections2.transform(list, indexPlucker));
}
import java.util.List;

public abstract class MathMax<E> {
    public abstract boolean isGreaterThan(E x1, E x2);
    public E getHighestIndexValue(List<E> list){
        E highestValue = null;
        for (E a: list) {
            if (isGreaterThan(a,highestValue)){
                highestValue = a;
            }
        }
        return highestValue;
    }
}
import java.util.ArrayList;
import java.util.List;

public class MathMaxA extends MathMax<ObjectA> {

    @Override
    public boolean isGreaterThan(ObjectA x1, ObjectA x2) {
        // do your comparison here, careful with null values
        return false;
    }

    public static void main(String[] args) {
        MathMaxA m = new MathMaxA();
        List<ObjectA> list = new ArrayList<ObjectA>();
        System.out.println(m.getHighestIndexValue(list));
    }
}