java中没有类似c#、scala中的tuple元组类,只能自己动手,丰衣足食了,见下面的代码:
Tuple 抽象类
1 import java.util.Optional; 2 3 /** 4 * Tuple元组类 5 * 6 * @since: 12/4/15. 7 * @author: http://yjmyzz.cnblogs.com/ 8 */ 9 public abstract class Tuple { 10 11 public abstract <A> Optional<A> _1(); 12 13 public abstract <B> Optional<B> _2(); 14 15 public abstract <C> Optional<C> _3(); 16 17 public abstract <D> Optional<D> _4(); 18 19 public abstract <E> Optional<E> _5(); 20 21 public static <A, B> Tuple of(A a, B b) { 22 return new Tuple2(a, b); 23 } 24 25 public static <A, B, C> Tuple of(A a, B b, C c) { 26 return new Tuple3(a, b, c); 27 } 28 29 public static <A, B, C, D> Tuple of(A a, B b, C c, D d) { 30 return new Tuple4(a, b, c, d); 31 } 32 33 public static <A, B, C, D, E> Tuple of(A a, B b, C c, D d, E e) { 34 return new Tuple5(a, b, c, d, e); 35 } 36 37 38 public static void main(String[] args) { 39 Tuple t2 = Tuple.of(1, "one"); 40 System.out.println(t2); 41 System.out.println(t2._1().get() + "," + t2._2().get()); 42 43 44 Tuple t3 = Tuple.of(1, "one", "一"); 45 System.out.println(t3); 46 System.out.println(t3._1().get() + "," + t3._2().get() + "," + t3._3().get()); 47 48 49 Tuple t4 = Tuple.of(1, "one", "一", "壹"); 50 System.out.println(t4); 51 System.out.println(t4._1().get() + "," + t4._2().get() + "," + t4._3().get() + "," + t4._4().get()); 52 53 Tuple t5 = Tuple.of(1, "one", "一", "壹", "yi"); 54 System.out.println(t5); 55 System.out.println(t5._1().get() + "," + t5._2().get() + "," + t5._3().get() + "," + t5._4().get() + "," + t5._5().get()); 56 } 57 58 }
Tuple2
1 import java.util.Optional; 2 3 public class Tuple2<A, B> extends Tuple { 4 private A a; 5 private B b; 6 7 Tuple2(A a, B b) { 8 this.a = a; 9 this.b = b; 10 } 11 12 @Override 13 public Optional<A> _1() { 14 return Optional.of(a); 15 } 16 17 @Override 18 public Optional<B> _2() { 19 return Optional.of(b); 20 } 21 22 @Override 23 public <C> Optional<C> _3() { 24 return Optional.empty(); 25 } 26 27 @Override 28 public <D> Optional<D> _4() { 29 return Optional.empty(); 30 } 31 32 @Override 33 public <E> Optional<E> _5() { 34 return Optional.empty(); 35 } 36 37 @Override 38 public String toString() { 39 return "Tuple2{" + 40 "a=" + a + 41 ", b=" + b + 42 ‘}‘; 43 } 44 }
Tuple3
1 import java.util.Optional; 2 3 public class Tuple3<A, B, C> extends Tuple { 4 private A a; 5 private B b; 6 private C c; 7 8 Tuple3(A e, B t, C k) { 9 this.a = e; 10 this.b = t; 11 this.c = k; 12 } 13 14 public Optional<A> _1() { 15 return Optional.of(a); 16 } 17 18 public Optional<B> _2() { 19 return Optional.of(b); 20 } 21 22 public Optional<C> _3() { 23 return Optional.of(c); 24 } 25 26 @Override 27 public <D> Optional<D> _4() { 28 return Optional.empty(); 29 } 30 31 @Override 32 public <E> Optional<E> _5() { 33 return Optional.empty(); 34 } 35 36 @Override 37 public String toString() { 38 return "Tuple3{" + 39 "a=" + a + 40 ", b=" + b + 41 ", c=" + c + 42 ‘}‘; 43 } 44 }
Tuple4
1 import java.util.Optional; 2 3 public class Tuple4<A, B, C, D> extends Tuple { 4 private A a; 5 private B b; 6 private C c; 7 private D d; 8 9 Tuple4(A a, B b, C c, D d) { 10 this.a = a; 11 this.b = b; 12 this.c = c; 13 this.d = d; 14 } 15 16 public Optional<A> _1() { 17 return Optional.of(a); 18 } 19 20 public Optional<B> _2() { 21 return Optional.of(b); 22 } 23 24 public Optional<C> _3() { 25 return Optional.of(c); 26 } 27 28 public Optional<D> _4() { 29 return Optional.of(d); 30 } 31 32 @Override 33 public <E> Optional<E> _5() { 34 return Optional.empty(); 35 } 36 37 @Override 38 public String toString() { 39 return "Tuple4{" + 40 "a=" + a + 41 ", b=" + b + 42 ", c=" + c + 43 ", d=" + d + 44 ‘}‘; 45 } 46 }
Tuple5
1 import java.util.Optional; 2 3 public class Tuple5<A, B, C, D, E> extends Tuple { 4 private A a; 5 private B b; 6 private C c; 7 private D d; 8 private E e; 9 10 Tuple5(A a, B b, C c, D d, E e) { 11 this.a = a; 12 this.b = b; 13 this.c = c; 14 this.d = d; 15 this.e = e; 16 } 17 18 public Optional<A> _1() { 19 return Optional.of(a); 20 } 21 22 public Optional<B> _2() { 23 return Optional.of(b); 24 } 25 26 public Optional<C> _3() { 27 return Optional.of(c); 28 } 29 30 public Optional<D> _4() { 31 return Optional.of(d); 32 } 33 34 public Optional<E> _5() { 35 return Optional.of(e); 36 } 37 38 @Override 39 public String toString() { 40 return "Tuple5{" + 41 "a=" + a + 42 ", b=" + b + 43 ", c=" + c + 44 ", d=" + d + 45 ", e=" + e + 46 ‘}‘; 47 } 48 }
5个元素一般情况下够用了,如果觉得不够,按这个套路自行扩展吧。
时间: 2024-11-14 04:19:10