Example usage for java.util List stream

List of usage examples for java.util List stream

Introduction

In this page you can find the example usage for java.util List stream.

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    boolean allStartsWithA = stringCollection.stream().allMatch((s) -> s.startsWith("a"));

    System.out.println(allStartsWithA); // false
}

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    boolean noneStartsWithZ = stringCollection.stream().noneMatch((s) -> s.startsWith("z"));

    System.out.println(noneStartsWithZ); // true
}

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    Optional<String> reduced = stringCollection.stream().sorted().reduce((s1, s2) -> s1 + "#" + s2);
    reduced.ifPresent(System.out::println);

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    // Update all graduates so that the students from Milan are set to Cambridge
    transactions.stream().map(Graduate::getTrader).filter(trader -> trader.getCity().equals("Milan"))
            .forEach(trader -> trader.setCity("Cambridge"));
    System.out.println(transactions);

}

From source file:com.bobby.peng.learning.java.stream.StreamMap.java

public static void main(String[] args) {
    //        List<Integer> list = StreamMap.newRandomList();
    ////from w  ww .jav  a  2  s .  c  o  m
    //        List<Integer> list2 = list.stream().map(i->i*5).collect(Collectors.toList());
    //
    //        StreamMap.printOut(list2);
    //
    //        list = list.subList(0,11);
    //        StreamMap.printOut(list);
    //
    //        String value = "INSERT INTO tech_subao_00.subao_renew_list (id, extra_info, gmt_created, gmt_modified, is_deleted, creator, modifier, remark, status, unqiue_flag, list_biz_id, province_name, province_code, city_name, city_code, license_no, engine_no, frame_no, factory_plate_model, first_register_date, applicant_name, owner_name, owner_certificate_type, owner_id_no, owner_mobile, contact_phone1, contact_phone2, bi_end_date, ci_end_date, vehicle_id, list_allocation_time, booking_start_date, booking_end_date, user_id, renew_batch_id, renew_biz_name, source, organization_id) VALUES (%d, null, '2018-08-07 02:50:36', '2018-08-07 02:50:37', 'N', '', 'system', null, 10, '000000%d', '1', '', '110000', '', '110100', '12312313', '12313', '31231', '111', '2018-05-12', '123', '123', 1, '310111111111111111', '13311111111', '13311111111', '13311111111', '2019-08-07 05:24:56', '2019-08-07 05:25:03', 1, null, null, null, null, %d, 'batch1', 50, 1);";
    //
    //        int id = 3;
    //        for(int i=3;i<100;i++) {
    //            for(int j=0;j<2;j++) {
    //                System.out.println(String.format(value,id,id,i));
    //                id++;
    //            }
    //        }

    List<Integer> test = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        test.add(i);
    }

    test.parallelStream().map(i -> {
        System.out.println(i);
        return i;
    }).collect(Collectors.toList());

    System.out.println("===================================");

    test.stream().map(i -> {
        System.out.println(i);
        return i;
    }).collect(Collectors.toList());

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    // Get all students from Cambridge and sort them by name.

    List<Student> traders = transactions.stream().map(Graduate::getTrader)
            .filter(trader -> trader.getCity().equals("Cambridge")).distinct()
            .sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList());
    System.out.println(traders);/*from  ww w . j  a  va2s . com*/

}

From source file:Main.java

public static void main(String[] args) {

    List<Trade> trades = TradeUtil.createTrades();
    Function<Trade, Integer> doubleQty = t -> {
        t.setQuantity(t.getQuantity() * 2);
        return t.getQuantity();
    };/*from w  ww.j  a v a 2 s  .co  m*/

    BinaryOperator<Integer> adder = (a, b) -> {
        System.out.println("A and B: " + a + "," + b);
        return a + b;
    };
    Integer sum = trades.stream().map(doubleQty).reduce(1, adder);

    System.out.println("Sum of qtys:" + sum);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> 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));
    boolean b = menu.stream().anyMatch(d -> d.getCalories() >= 1000);

    System.out.println(b);/*from  w  w  w. j a  v a 2s. c o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> 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));
    boolean b = menu.stream().noneMatch(d -> d.getCalories() >= 1000);

    System.out.println(b);//from  w  w w .j a v  a  2 s  .co  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> 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));
    boolean b = menu.stream().allMatch(d -> d.getCalories() >= 1000);

    System.out.println(b);//from  ww w. jav  a2 s.c  o m
}