1

If I call forEach function for a list created as Collection.singletonList or Arrays.asList, the required parameter for the function accept for the Consumer required in the forEach function as parameter is type of int[](for new int[]{}) or Interger[](for new Integer[]{})

It seems like every element of the array is passed like an array of size one. So when I'm printing the value like Arrays.toString(x) where type of x is int[], it's working fine

Collections.singletonList(new int[] {1, 2, 3})
               .forEach(new Consumer<int[]>() {
    @Override
    public void accept(int[] o) {
        System.out.println(o);
    }
});

Why the required parameter is of type int[] instead of int

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • Because you created a singleton list (i.e. a list with only ONE element), and this sole element is of type int[]. – Benoit Jul 17 '19 at 07:49
  • 1
    It's strange when an anonymous class is created to implement an interface that was particularly meant to be a functional interface... – ernest_k Jul 17 '19 at 07:49
  • Possibly related: [Arrays.asList() not working as it should?](https://stackoverflow.com/q/1467913) see especially Jon Skeet's answer https://stackoverflow.com/a/1467940 – Pshemo Jul 17 '19 at 08:00
  • @Benoit It behaves same if I use `Arrays.asList()` instead of `Collections.singletonList()` – Ritam Chakraborty Jul 18 '19 at 05:51

2 Answers2

2

Collection.singletonList creates, as the name states, a list with one element - in your case with an array. What I assume you want to achieve is something like

Arrays.asList(1, 2, 3).forEach(new Consumer<Integer>() {
    @Override
    public void accept(Integer o) {
        System.out.println(o);
    }
});
Smutje
  • 17,733
  • 4
  • 24
  • 41
  • Looks like `Collections.singletonList` and `Arrays.asList` both create an array of size 1 for each elements of the array. That's must be because of the implementation of those function. I should stick with `IntStream.range(1, 4).boxed().forEach(System.out::println)`. – Ritam Chakraborty Jul 18 '19 at 05:57
  • 1
    No, Arrays.asList creates a list of elements where each element has the type you pass into Arrays.asList. `Collections.singletonList(1)` is a shortcut/simplification for `Arrays.asList(1)`. – Smutje Jul 23 '19 at 06:09
2

The way you build the list is incorrect. You actually create a singleton list (i.e. a list with only ONE element), and this sole element is of type int[].

Note also Consumer is intended to be used with lambda or method reference:

        Stream.of(1,2,3).forEach(System.out::println);
Benoit
  • 5,118
  • 2
  • 24
  • 43
  • I know that that can be done, after all `List.of(1, 2, 3).forEach(System.out::println)` this is another implementation from Java-9. I just wanted to know why is it taking int[] as input. – Ritam Chakraborty Jul 18 '19 at 05:53
  • Looks like `IntStream.of(new int[] {1, 2, 3}).boxed().forEach(System.out::println)` this must be the only way. – Ritam Chakraborty Jul 18 '19 at 06:00