2015-09-07 11:44:36 -06:00
|
|
|
|
// generics/TupleTest.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 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.
|
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-11-11 20:20:04 -08:00
|
|
|
|
import onjava.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
|
|
class Amphibian {}
|
|
|
|
|
class Vehicle {}
|
|
|
|
|
|
|
|
|
|
public class TupleTest {
|
2015-11-11 20:20:04 -08:00
|
|
|
|
static Tuple2<String, Integer> f() {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// Autoboxing converts the int to Integer:
|
2015-11-11 20:20:04 -08:00
|
|
|
|
return new Tuple2<>("hi", 47);
|
2015-06-15 17:47:35 -07:00
|
|
|
|
}
|
2015-11-11 20:20:04 -08:00
|
|
|
|
static Tuple3<Amphibian, String, Integer> g() {
|
|
|
|
|
return new Tuple3<>(new Amphibian(), "hi", 47);
|
2015-06-15 17:47:35 -07:00
|
|
|
|
}
|
|
|
|
|
static
|
2015-11-11 20:20:04 -08:00
|
|
|
|
Tuple4<Vehicle, Amphibian, String, Integer> h() {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
return
|
2015-11-11 20:20:04 -08:00
|
|
|
|
new Tuple4<>(
|
2015-06-15 17:47:35 -07:00
|
|
|
|
new Vehicle(), new Amphibian(), "hi", 47);
|
|
|
|
|
}
|
|
|
|
|
static
|
2015-11-11 20:20:04 -08:00
|
|
|
|
Tuple5<Vehicle, Amphibian, String, Integer, Double> k(){
|
2015-06-15 17:47:35 -07:00
|
|
|
|
return new
|
2015-11-11 20:20:04 -08:00
|
|
|
|
Tuple5<>(
|
2015-06-15 17:47:35 -07:00
|
|
|
|
new Vehicle(), new Amphibian(), "hi", 47, 11.1);
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
2015-11-11 20:20:04 -08:00
|
|
|
|
Tuple2<String, Integer> ttsi = f();
|
2015-06-15 17:47:35 -07:00
|
|
|
|
System.out.println(ttsi);
|
2015-11-11 20:20:04 -08:00
|
|
|
|
// ttsi._1 = "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@139a55, hi, 47)
|
|
|
|
|
(Vehicle@1db9742, Amphibian@106d69c, hi, 47)
|
|
|
|
|
(Vehicle@52e922, Amphibian@25154f, hi, 47, 11.1)
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|