How to return multiple objects from a Java method?

前端 未结 25 3072
眼角桃花
眼角桃花 2020-11-21 23:55

I want to return two objects from a Java method and was wondering what could be a good way of doing so?

The possible ways I can think of are: return a HashMap<

25条回答
  •  一向
    一向 (楼主)
    2020-11-22 00:23

    If you know you are going to return two objects, you can also use a generic pair:

    public class Pair {
        public final A a;
        public final B b;
    
        public Pair(A a, B b) {
            this.a = a;
            this.b = b;
        }
    };
    

    Edit A more fully formed implementation of the above:

    package util;
    
    public class Pair {
    
        public static  Pair makePair(P p, Q q) {
            return new Pair(p, q);
        }
    
        public final A a;
        public final B b;
    
        public Pair(A a, B b) {
            this.a = a;
            this.b = b;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((a == null) ? 0 : a.hashCode());
            result = prime * result + ((b == null) ? 0 : b.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            @SuppressWarnings("rawtypes")
            Pair other = (Pair) obj;
            if (a == null) {
                if (other.a != null) {
                    return false;
                }
            } else if (!a.equals(other.a)) {
                return false;
            }
            if (b == null) {
                if (other.b != null) {
                    return false;
                }
            } else if (!b.equals(other.b)) {
                return false;
            }
            return true;
        }
    
        public boolean isInstance(Class classA, Class classB) {
            return classA.isInstance(a) && classB.isInstance(b);
        }
    
        @SuppressWarnings("unchecked")
        public static  Pair cast(Pair pair, Class

    pClass, Class qClass) { if (pair.isInstance(pClass, qClass)) { return (Pair) pair; } throw new ClassCastException(); } }

    Notes, mainly around rustiness with Java & generics:

    • both a and b are immutable.
    • makePair static method helps you with boiler plate typing, which the diamond operator in Java 7 will make less annoying. There's some work to make this really nice re: generics, but it should be ok-ish now. (c.f. PECS)
    • hashcode and equals are generated by eclipse.
    • the compile time casting in the cast method is ok, but doesn't seem quite right.
    • I'm not sure if the wildcards in isInstance are necessary.
    • I've just written this in response to comments, for illustration purposes only.

提交回复
热议问题