Printing the max of groups of three random ints I'm trying to teach myself streaming and lambda expressions. Here's the scenario: I have a collection of 10 Thing objects each containing an int[] of 3 random number. I've written code to print out the largest number in each Thing.

I've already written two statements and an accompanying method that print the result I want, however the code is pretty ropey... improvements and suggestions would be greatly appreciated.

import java.util.Random;
public class Thing {
    private Random r = new Random();
    private int a = r.nextInt(), b = r.nextInt(), c = r.nextInt();

    public int[] getCollectionOfInts() {
        return collectionOfInts;
    }

    private int[] collectionOfInts = new int[]{a, b, c};

}//end of class

//main in separate driver class
public static void main(String[] args) {
        Thing[] things = new Thing[10];
        int a=0,b=0,c=0;
        for (int x =0;x&lt;10;x++){
            things[x] = new Thing();
            for (int y=0;y&lt;things[x].getCollectionOfInts().length;y++)
            {
                if(y==0)
                    a = things[x].getCollectionOfInts()[y];
                if(y==1)
                    b = things[x].getCollectionOfInts()[y];
                if(y==2)
                    c = things[x].getCollectionOfInts()[y];
            }
            //print 3 numbers in each Thing object
            System.out.println(x+": "+a+", "+b+", "+c);
        }
        System.out.println();

        //compare each number and print out largest... too many ternaries 
        Stream.of(things).forEach(Thing-&gt;System.out.println(Stream.of(Thing.getCollectionOfInts()).mapToInt(z-&gt;z[0]&gt;z[1]&amp;&amp;z[0]&gt;z[2]?z[0]:z[1]&gt;z[2]?z[1]:z[2]).reduce((x,y)-&gt;x+y).getAsInt()));
        System.out.println();
        //improper use of .max() or .mapToInt(...)?
        Stream.of(things).forEach(Thing-&gt;System.out.println(Stream.of(Thing.getCollectionOfInts()).mapToInt(x-&gt;maxInt(x)).max().getAsInt()));

    }
    public static int maxInt(int[] x) {
        int max=x[0];
        for (int y:x) {
            if(y&gt;max)
                max = y;
        }
        return max;
    }


I get the expected results for both streams, but I'm still unhappy with how I get there!
The first thing about your code are the two lines inside your class Thing:

private Random r = new Random();
private int a = r.nextInt(), b = r.nextInt(), c = r.nextInt();


If Random r is used just to initialize the array and not in other methods inside the class , it is better to use it in the costructor of the class :

public Thing() {
    Random r = new Random();
    this.arr = new int[] {r.nextInt(), r.nextInt(), r.nextInt()};
}


You can check I initialize directy here the array of ints instead of defining variables a, b, c.
A good thing is also override the String method to print the internal state of a object:

@Override
public String toString() {
    return Arrays.toString(arr);
}


Now you can print the state of your Thing object in this way:

for (int i = 0;i &lt; 10; ++i){
    things[i] = new Thing();
    //print 3 numbers in each Thing object
    System.out.println(i + ": " + things[i]);
}


Your iterations with Stream are ok, but instead of use Stream because you are working with int values you can use instead IntStream simplifyng the code:

Stream.of(things).forEach(t -&gt; System.out.println(Arrays.stream(t.getArr()).max().getAsInt()));


Below the code of class Thing including all my modifies:

package codereview;

import java.util.Arrays;
import java.util.Random;
import java.util.stream.Stream;

public class Thing {

    private int[] arr;

    public Thing() {
        Random r = new Random();
        this.arr = new int[] {r.nextInt(), r.nextInt(), r.nextInt()};
    }

    public int[] getArr() {
        return arr;
    }

    @Override
    public String toString() {
        return Arrays.toString(arr);
    }


    public static void main(String[] args) {
        Thing[] things = new Thing[10];
        for (int i = 0;i &lt;10; ++i){
            things[i] = new Thing();
            //print 3 numbers in each Thing object
            System.out.println(i + ": " + things[i]);
        }
        System.out.println();
        Stream.of(things).forEach(t -&gt; System.out.println(Arrays.stream(t.getArr()).max().getAsInt()));
    }  
}