This question already has answers here:
java8: method reference from another method reference
(3 answers)
Closed 1 year ago.
I want to know how to convert lambda expression printing string length to using '::' operation.
String[] arr = new String[]{"1", "234", "56"};
Arrays.stream(arr).forEach(s -> System.out.println(s.length()));
Excellent question! Here you could do so twice. Once to map each String to an int by calling length() on each String and a second time by calling println(int) on System.out. Like,
Arrays.stream(arr).mapToInt(String::length).forEach(System.out::println);
Related
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 2 years ago.
I have a method rotateImage that returns an int[][] array, I am having trouble understaing how to display the actual values of the array instead of just the hashcode:
[I#548c4f57
[I#1218025c
[I#816f27d
I tried doing this:
Arrays.stream(rotateImage(matrix)).forEach(System.out::println);
but that only returns the hashcode, not the actual numbers themselves. I also tried using to Arrays.toString() method in several ways but always get an error.
It's cause you're accessing the address and not the actual values. You can simply do a lambda function and iterate through each row
Arrays.stream(rotateImage(matrix)).
forEach(row -> {
for(int i : row) {
System.out.println(i);
}
});
This question already has answers here:
Converting 'ArrayList<String> to 'String[]' in Java
(17 answers)
Closed 3 years ago.
I am trying to initialize an array object with a get method which returns an arraylist object. I have tried using .toArray() to convert but it didn't work.
Would Project[] projects = list.toArray(new Project[[0]]) work? The reason it doesn't work normally is because by default toArray returns an Object[], and the JVM is unable to cast that to a Project[]. Passing in the project array allows it to determine the type of the desired array.
This question already has answers here:
Collections.emptyList() vs. new instance
(7 answers)
Closed 7 years ago.
I have a line of code
List<List<String>> get_valuesofValues = new ArrayList<List<String>>((Collection<? extends List<String>>)map.get(value_atIndex));
"get_valuesofValues" will be null in some places. How can I assign it to a empty list in order to avoid exception?
Should be something like this :
if(get_valuesofValues == null){
get_valuesofValues =((Collection.<List<String>>)emptyList();
}
Use Collections.emptyList() method. Please note, that such collections cannot be modified (add/replace/remove of elements produces an exception).
This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 8 years ago.
Java:
int [] list = new int [7];
System.out.print(list);
In the console it prints: [I#1f96302
Not only is it giving values other than ints, but it is giving more than I asked it too.
[l#1f96302 is the default way an Object is printed (that's what Object's toString() method returns for arrays). Try System.out.print(Arrays.toString(list)) instead, which will display the elements of the array.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Why does the toString method in java not seem to work for an array
(9 answers)
Closed 9 years ago.
When i try to split a String "Element" into a String array, and then print out the result i get an output which i don't understand. My code is as follows:
String element = mapArray.get(i);
elementSplit = element.split("(?!^)");
System.out.println(elementSplit);
And the output produced when i print the String Array is:
[Ljava.lang.String;#3dee2310
Could someone please advise, as i do not know why it is printing this output.
Thanks very much
You have to use Arrays.toString method.
System.out.println(Arrays.toString(elementSplit));
Due to speed You should use toCharArray method instead of split("(?!^)") and in order to print array you should use Arrays.toString method
String element = mapArray.get(i);
elementSplit = element.toCharArray();
System.out.println(Arrays.toString(elementSplit));