Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.mycompany.app9;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Set;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /**
- *
- * @author Admin
- */
- public class Main {
- //
- public static void main(String[] args) {
- //
- List<String> list = new ArrayList<>();
- list.add("array1");
- list.add("array2");
- list.stream().forEach(System.out::println);
- //
- Stream<String> s = list.stream();
- s.forEach(System.out::println);
- //s.forEach(System.out::println);
- //
- System.out.println("Iterator for List:");
- Iterator<String> i = list.iterator();
- while(i.hasNext()){
- String value = i.next();
- System.out.println("value=" + value);
- }
- System.out.println("Iterator for Stream:");
- Iterator<String> i1 = list.stream().iterator();
- while(i1.hasNext()){
- String value1 = i1.next();
- System.out.println("stream.value=" + value1);
- }
- System.out.println("PRINT STREAM ELEMENTS:");
- // использование метода of для создания потока Stream
- Stream<String> s3 = Stream.of("ONE", "TWO");
- s3.forEach(System.out::println);
- System.out.println("collect - method (toList):");
- Stream<String> s4 = Stream.of("ONE", "TWO");
- // использовние метода collect и класса Collectors
- List<String> result = s4.collect(Collectors.toList());
- for(String val: result){
- System.out.println("val=" + val);
- }
- System.out.println("collect - method (toSet):");
- Stream<String> s5 = Stream.of("ONE", "TWO", "ONE");
- // использовние метода collect и класса Collectors
- Set<String> result2 = s5.collect(Collectors.toSet());
- for(String val: result2){
- System.out.println("val2=" + val);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement