I'd like to compare class fields in Java, but I have a question

Asked 1 years ago, Updated 1 years ago, 70 views

public class Number {

    int[] array0 = new int[]{2,7,19,25,29,36,16};
    int[] array1 = new int[]{2,10,12,31,33,42,32};
    int[] array2 = new int[]{3,8,19,27,30,41,12};
    int[] array3 = new int[]{2,6,7,12,19,45,38};
    int[] array4 = new int[]{2,10,11,19,35,39,29};
    int[] array5 = new int[]{5,6,13,16,27,28,9};
    int[] array6 = new int[]{12,15,16,20,24,30,38};
    int[] array7 = new int[]{4,6,15,25,26,33,40};

}

public class getnum {

public static void main(String[] args) {
    // // TODO Auto-generated method stub


    Number Num = new Number();

    int[] lonum = new int[Num.array0.length];

    System.arraycopy( Num.array0, 0, lonum, 0, Num.array0.length );

            int[] temp = new int[]{1,2,3,4,5,6};

            Arrays.equals(lotnum, temp);
    }

What I want to do is compare the field of Number class with the value of temp Do I have to compare each field? I wonder if you can solve it with a for statement. I'm going to compare 700, but I can't figure it out I'm a beginner

java reference array compare

2022-09-22 18:45

1 Answers

From java8, stream and lambda are added, so you can handle it conveniently.

public class Number {
    public List<Integer[]> numbersContainer = Stream.of(new Integer[]{2,7,19,25,29,36,16}, new Integer[]{2,5,12,31,33,42,32}).collect(Collectors.toList());
}

Integer[] temp = new Integer[]{1,2,3,4,5,6};

Number number = new Number();

List<List<Integer>> result = number.numbersContainer
                                    .stream()
                                    .map(arr -> Arrays.asList(arr)
                                                        .stream()
                                                        .filter(Arrays.asList(temp)::contains)
                                                        .collect(Collectors.toList()))
                                    .collect(Collectors.toList());


[[2], [2, 5]]


2022-09-22 18:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.