Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

functional doubling

Hi,

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([]) → []

Open in new window


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());
}

Open in new window

i am trying to write a java a program to execute it in exclipse to see result in eclipise consoel

import 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;
}

	
}

Open in new window


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(doubling(al));
      }

      
      public static List<Integer> doubling(List<Integer> nums) {
        nums.replaceAll(n -> n * 2);
        return nums;
}

      
}

please advise
Avatar of ste5an
ste5an
Flag of Germany image

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;
    }
}

Open in new window

Avatar of gudii9

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
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

List<Integer> li = Stream.of(1,2,3).collect(Collectors.toList());

Open in new window


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.
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”);

Open in new window

Avatar of gudii9

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.

If you enjoy watching conference talks there’s also a good video by Stuart Marks from the Java Core Libraries team on youtube.
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?
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.