OnJava8-Examples/generics/TupleTest.java

42 lines
1.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// generics/TupleTest.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
import onjava.*;
2015-06-15 17:47:35 -07:00
public class TupleTest {
static Tuple2<String, Integer> f() {
2015-06-15 17:47:35 -07:00
// Autoboxing converts the int to Integer:
return new Tuple2<>("hi", 47);
2015-06-15 17:47:35 -07:00
}
static Tuple3<Amphibian, String, Integer> g() {
return new Tuple3<>(new Amphibian(), "hi", 47);
2015-06-15 17:47:35 -07:00
}
static
Tuple4<Vehicle, Amphibian, String, Integer> h() {
2015-06-15 17:47:35 -07:00
return
new Tuple4<>(
2015-06-15 17:47:35 -07:00
new Vehicle(), new Amphibian(), "hi", 47);
}
static
2016-01-25 18:05:55 -08:00
Tuple5<Vehicle, Amphibian, String, Integer, Double> k() {
2015-06-15 17:47:35 -07:00
return new
Tuple5<>(
2015-06-15 17:47:35 -07:00
new Vehicle(), new Amphibian(), "hi", 47, 11.1);
}
public static void main(String[] args) {
Tuple2<String, Integer> ttsi = f();
2015-06-15 17:47:35 -07:00
System.out.println(ttsi);
// ttsi.a1 = "there"; // Compile error: final
2015-06-15 17:47:35 -07:00
System.out.println(g());
System.out.println(h());
System.out.println(k());
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
(hi, 47)
(Amphibian@4554617c, hi, 47)
(Vehicle@677327b6, Amphibian@14ae5a5, hi, 47)
(Vehicle@135fbaa4, Amphibian@45ee12a7, hi, 47, 11.1)
2015-09-07 11:44:36 -06:00
*/