gudii9
asked on
functional doubling
Hi,
I am trying to double list numbers using functionala lamda programming
http://codingbat.com/prob/p117665
i am getting below compilation error.
Multiple markers at this line
- Syntax error on token "]", } expected
- Type mismatch: cannot convert from int[] to
List<Integer>
- Syntax error on token "[", { expected
canont i add primitives as below to Integer list
import java.util.ArrayList;
import java.util.List;
public class FuncProgDouble {
public static void main(String[] args) {
ArrayList<Integer> al=new ArrayList<Integer>();
al.add("1");
al.addAll("3");
al.addAll("8");
//al.add(1,"Sachin");
//List<Integer> li=[1, 2, 3];
System.out.println(doublin g(al));
}
public static List<Integer> doubling(List<Integer> nums) {
nums.replaceAll(n -> n * 2);
return nums;
}
}
please advise
I am trying to double list numbers using functionala lamda programming
http://codingbat.com/prob/p117665
Given a list of integers, return a list where each integer is multiplied by 2.
doubling([1, 2, 3]) → [2, 4, 6]
doubling([6, 8, 6, 8, -1]) → [12, 16, 12, 16, -2]
doubling([]) → []
Solution:
public List<Integer> doubling(List<Integer> nums) {
nums.replaceAll(n -> n * 2);
return nums;
// OR the equivalent java streams solution:
// return nums.stream()
// .map(n -> n * 2)
// .collect(Collectors.toList());
}
i am trying to write a java a program to execute it in exclipse to see result in eclipise consoelimport java.util.List;
public class FuncProgDouble {
public static void main(String[] args) {
List<Integer> li=[1, 2, 3];
System.out.println(doubling(li));
}
public static List<Integer> doubling(List<Integer> nums) {
nums.replaceAll(n -> n * 2);
return nums;
}
}
i am getting below compilation error.
Multiple markers at this line
- Syntax error on token "]", } expected
- Type mismatch: cannot convert from int[] to
List<Integer>
- Syntax error on token "[", { expected
canont i add primitives as below to Integer list
import java.util.ArrayList;
import java.util.List;
public class FuncProgDouble {
public static void main(String[] args) {
ArrayList<Integer> al=new ArrayList<Integer>();
al.add("1");
al.addAll("3");
al.addAll("8");
//al.add(1,"Sachin");
//List<Integer> li=[1, 2, 3];
System.out.println(doublin
}
public static List<Integer> doubling(List<Integer> nums) {
nums.replaceAll(n -> n * 2);
return nums;
}
}
please advise
First of all: Sort your tags. Is it Java or .NET??
Here is one way.
import java.util.List;
import java.util.Arrays;
public class FuncProgDouble {
public static void main(String[] args) {
List<Integer> li = Arrays.asList(1, 2, 3);
System.out.println(doubling(li));
}
public static List<Integer> doubling(List<Integer> nums) {
nums.replaceAll(n -> n * 2);
return nums;
}
}
ASKER
is there is a way i can make it work without using arrays here (may be directly initialize List itself with 1, 2, 3 values?)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
List<Integer> li = Stream.of(1,2,3).collect(Collectors.toList());
what is stream and Collectors and collect methods here?
Discussion moved to
https://www.experts-exchange.com/questions/29041669/Collectors-stream-collect-method.html
That way we all can be on the same page.
https://www.experts-exchange.com/questions/29041669/Collectors-stream-collect-method.html
That way we all can be on the same page.
is there is a way i can make it work without using arrays here (may be directly initialize List itself with 1, 2, 3 values?)Another comment on that question:
When Java 9 is released, we will have what we need in this case . They are called "Collection Factory Methods". See
http://iteratrlearning.com/java9/2016/11/09/java9-collection-factory-methods.html
In the future, we will use something like
List<String> list = List.of(“Hello”, “World”, “from”, “Java”);
ASKER
Collection literals are an appealing language feature, adding some easier syntax to perform a common operation. However, the cost-benefit tradeoff is much better for Collection factory methods that are simply a library change. They give us most of the benefits without any language changes. The addition of these factory methods in Java 9 provides Immutable collections that ban the use of null values as elements.looks like interesting stuff coming in java 9. how you keep learnign all these new things like java 8 java 9 java 10 on and on. how to you keep up the pace?
If you enjoy watching conference talks there’s also a good video by Stuart Marks from the Java Core Libraries team on youtube.
how to you keep up the pace?I don't. Java 8 was released three years ago and I am still trying to learn it.