Monday, November 3, 2014

Java intstream



Convert int array to IntStream

import java.util.Arrays;
import java.util.stream.IntStream;
public class Main 
{
            public static void main(String[] args) 
             {
             int[] numbers = {2, 3, 5, 7, 11, 13};
    IntStream intStream = Arrays.stream(numbers);
    intStream = intStream.limit(3);
    System.out.println(intStream.sum());
    }
            }

Generate constant values from IntStream

 
import java.util.stream.IntStream;
public class Main 
{
public static void main(String[] args) 
{
                       IntStream.generate(() -> 1)
             .limit(5)
             .forEach(System.out::println);
  }
          }

Map to IntStream from Object Stream

 
import java.util.Arrays;
import java.util.List;
public class Main 
{
 
  public static void main(String[] args) throws Exception
  {
    List menu =
        Arrays.asList( new Dish("pork", false, 800, Dish.Type.Meat),
                       new Dish("beef", false, 700, Dish.Type.Meat),
                       new Dish("chicken", false, 400, Dish.Type.Meat),
                       new Dish("rice", true, 350, Dish.Type.Other),
                       new Dish("pizza", true, 550, Dish.Type.Other),
                       new Dish("prawns", false, 400, Dish.Type.Fish),
                       new Dish("salmon", false, 450, Dish.Type.Fish));
    List<Integer> numbers = Arrays.asList(3,4,5,1,2);
               Arrays.stream(numbers.toArray()).forEach(System.out::println);
    int calories = menu.stream()
                       .mapToInt(Dish::getCalories)
                       .sum();
    System.out.println("Number of calories:" + calories);
             }
          }
          class Dish 
          {
  private final String name;
  private final boolean vegetarian;
  private final int calories;
  private final Type type;
          public Dish(String name, boolean vegetarian, int calories, Type type) 
          {
      this.name = name;
      this.vegetarian = vegetarian;
      this.calories = calories;
      this.type = type;
  }
           public String getName() 
           {
      return name;
  }
           public boolean isVegetarian() 
           {
      return vegetarian;
  }
public int getCalories() 
{
      return calories;
  }
           public Type getType() 
           {
      return type;
  }
           public enum Type { Meat, Fish, Other }
           public String toString() 
           {
      return name;
  }
         }

Pass Int Consumer as parameter

import java.util.function.IntConsumer;
public class Main 
{
  public static void start(IntConsumer cons, int d)
 {
    cons.accept(d);
  }
  public static void main(String[] args) 
  {
    start(e -> System.out.print("Release year: " + e), 2013);
  }
}

Process String as int stream

 
public class Main
 {
public static void main(String[] args) 
{
             String s = "R2D2C3P0";
    int result = s.chars()
            .filter(Character::isDigit)
            .map(ch -> Character.valueOf((char) ch)).sum();
    System.out.println(result);
  }
}

 


No comments:

Post a Comment