Home > other >  is there a way to have a variable from the main program change within the lambda expression?
is there a way to have a variable from the main program change within the lambda expression?

Time:03-07

I would like to have the most variable in the code below take on a new value when v > most or v == most. If not, I would like to know if there is a workaround. From the internet research I did, a global variable would be acceptable, but not a local variable. I tried declaring the variable between 'public class Occurrences' and 'public static void main()' with no success.

Code:

import java.util.*;

public class Occurrences {

    public static void main(String[] args) {

        int num;
        int i = 0;

        Map<Integer, Integer> obj = new TreeMap<>();

        //System.out.println("How many numbers will you be entering?\n");
        Scanner obj5 = new Scanner(System.in);
        //num = obj5.nextInt();

        ArrayList<Integer> obj2 = new ArrayList<>();
        while (true) {
            while (true) {
                System.out.println("Please enter a number: ");
                num = obj5.nextInt();
                if (num != 0) {
                    obj2.add(num);
                } else {
                    break;
                }

            }

            if (!obj2.isEmpty()) {
                for (i = 0; i < obj2.size();   i) {
                    num = obj2.get(i);
                    if (!obj.containsKey(num)) {
                        obj.put(num, 1);
                    } else {
                        int value = obj.get(num);
                        value  ;
                        obj.put(num, value);
                    }
                }
                int most = -1;
                ArrayList<Integer> obj10 = new ArrayList<>();
                obj.forEach((k, v) ->
                {
                    if (v > most || v == most) 
                    {
                        most = v;
                        obj10.add(k);
                    }
                /*int most = v > most ? k : ** ;
                int most2 = k == most ?*/

                }); //:  //System.out.println(k   "\t"   v));

                for (i = 0; i < obj10.size();   i) {
                    System.out.println(obj10.get(i));
                }
            }
            
            if (obj2.isEmpty()) {
                System.out.println("You did not enter a number. Try again");
                continue;
            } else {
                break;
            }
        }
    }
}

Input:

2 2 2 3 3 3 4 4 4 8 9 10

Output:

2 3 4

CodePudding user response:

Create an anonymous class:

obj.forEach(new BiConsumer<Integer, Integer>() {
                    int most = -1;

                    @Override
                    public void accept(Integer k, Integer v) {
                        if (v > most || v == most) {
                            most = v;
                            obj10.add(k);
                        }
                    }
                });

CodePudding user response:

You need to wrap the int value in a class to achieve that. This wrapper can be AtomicInteger. Or you can write your own wrapper class, if you don't need atomicity. Something as simple as this will suffice:

public class IntWrapper {

  private int value;

  //getter and setter
}

And use it like this:

IntWrapper most = new IntWrapper();
most.setValue(-1);
obj.forEach((k, v) -> {
  //your logic here
  most.setValue(current value);
});

As a side note, you should choose better names for your variables, obj, obj2 etc. are not descriptive enough.

  • Related