While working need a structure like Pair in java so tired to emulate the behavior using AbstractMap.SimpleEntry<>() and a technique shared by @starbuxman in his tweet about creating tuple using new Object(){} implementation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Day35 {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.iterate(1.0 , d -> d+0.5);

        List<AbstractMap.SimpleEntry<Double, Double>> pairs =  doubleStream
                .limit(10)
                .mapToObj(number -> new AbstractMap.SimpleEntry<>(new Random().nextDouble(), number))
                .collect(Collectors.toList());

        pairs.forEach(pair -> {
          System.out.println(pair.getKey());
          System.out.println(pair.getValue());
        });

        Stream.generate(() -> new Random().nextDouble())
              .limit(10)
              .map(lon -> new Object() {
                 double lattitude = new Random().nextDouble();
                 double longitude = lon;
              }).forEach(tuple -> {
                  System.out.println(tuple.lattitude);
                  System.out.println(tuple.longitude);
              });

    }
}