Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java combinatorics. Creating an object with generated data

This is my first post in here. Today I started using combinatoric library for Java.

This one: https://github.com/dpaukov/combinatoricslib3

I've got more than 10k length of sides of triangle in Excel. Than i pulled them into 2d Integer array.

Than i created class Triangle:

public class Triangle {

private int a;
private int b;
private int c;

public Triangle(int a, int b, int c)
{
    this.a = a;
    this.b = b;
    this.c = c;
}

public boolean isCorrect()
{
    if(this.a + this.b > this.c)
        return true;

    return false;
}

}

My problem is that I can generate all possible combinations of Triangles but have no idea how to created object Triangle. Only know how to print result.

public static void main(String[] args) throws IOException {


    Generator.combination(sides).simple(3).stream().forEach(System.out::println);



}

Thank you in advance. Cheers!

EDIT:

This is example of sides:

static final int[][] sides = new int[][]{

        {71, 100, 1231, 832, 127},
        {336, 447, 815, 658, 373},
        {126, 444, 556, 221, 1322},
        {1226, 662, 985, 87, 991},
        {555, 512, 111, 339, 22},
    };

I want to generate all possible Triangles with this data.

like image 928
whiskermrr Avatar asked Aug 31 '26 20:08

whiskermrr


1 Answers

Should look something like that:

Generator
.combination(sides)
.simple(3)
.stream()
.forEach(
     sides -> new Triangle(sides[0],sides[1],sides[2])
);

Note that this implies that the sides are integer, if not (e.g. strings) you might need to additionally convert (map) them to a proper type.

Now, if you want for example to collect them all to list, you can do:

List<Triangle> triangles = Generator
    .combination(sides)
    .simple(3)
    .stream()
    .map(sides -> new Triangle(sides[0],sides[1],sides[2]))
    .collect(Collectors.toList())

You can iterate over a two-dimensional int[][] array, and find all the combinations for each line, like that:

Arrays.stream(sides)
    .forEach(
    line -> {
         Generator.combination(Arrays.stream(line).boxed().collect(Collectors.toList()))
        .simple(3)
        .stream()
        .forEach(System.out::println);
    }
);
like image 190
zeppelin Avatar answered Sep 02 '26 10:09

zeppelin