List of usage examples for java.util List stream
default Stream<E> stream()
From source file:Main.java
public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); List<Integer> twoEvenSquares = numbers.stream().filter(n -> n % 2 == 0).map(n -> n * n).limit(2) .collect(toList());/*from w w w.ja v a2 s. c om*/ System.out.println(twoEvenSquares); }
From source file:Main.java
public static void main(String[] args) { List<Integer> _numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Iterator<Integer> iter = _numbers.stream().filter(value -> value % 2 == 0).iterator(); while (iter.hasNext()) { System.out.println(iter.next()); }//from ww w . ja va2 s .co m }
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("Chris", "HTML", "XML", "CSS", ""); Stream<String> s = names.stream(); Predicate<String> isEmpty = String::isEmpty; Predicate<String> notEmpty = isEmpty.negate(); System.out.println(s.filter(notEmpty).collect(Collectors.toList())); }
From source file:Main.java
public static void main(String[] args) { List<Employee> persons = Employee.persons(); String names = persons.stream().map(Employee::getName).collect(Collectors.joining()); String delimitedNames = persons.stream().map(Employee::getName).collect(Collectors.joining(", ")); String prefixedNames = persons.stream().map(Employee::getName) .collect(Collectors.joining(", ", "Hello ", ". Goodbye.")); System.out.println("Joined names: " + names); System.out.println("Joined, delimited names: " + delimitedNames); System.out.println(prefixedNames); }
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("XML", "HTML", "Java", "Javascript", "CSS", "Static"); Optional<String> firstS = names.stream().filter(name -> name.startsWith("S")).findFirst(); System.out.println(firstS.orElse("None found")); }
From source file:Main.java
public static void main(String[] args) { List<String> _names = Arrays.asList("Bob", "Tom", "Jeff", "Scott", "Jennifer", "Steve"); List<String> greetings = _names.stream().map(name -> "Hello " + name).collect(Collectors.toList()); System.out.println(greetings); }
From source file:Main.java
public static void main(String[] argv) { List<String> mascots = new ArrayList<>(); mascots.add("duke"); mascots.add("juggy"); String actual = mascots.stream().reduce((t, u) -> t + "," + u).get(); System.out.println(actual);//from w w w. ja v a 2s. c o m }
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("HTML", "CSS", "CSS3", "Java", "SQL", "Javascript", "MySQL"); String name5 = names.stream().map(s -> s.toUpperCase()).filter(s -> s.length() < 5) .sorted((a, b) -> b.length() - a.length()).findFirst().orElse("a"); System.out.println(name5);/*from www .ja v a2s .c o m*/ String name2 = names.stream().map(s -> s.toUpperCase()).filter(s -> s.length() < 2) .sorted((a, b) -> b.length() - a.length()).findFirst().orElse("Somthing else"); System.out.println(name2); }
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("XML", "HTML", "Java", "Javascript", "CSS", "Static"); Optional<String> firstZ = names.stream().filter(name -> name.startsWith("Z")).findFirst(); if (firstZ.isPresent()) { System.out.println(firstZ.get()); } else {/* ww w . j a v a 2s .c o m*/ System.out.println("No Z's found"); } }
From source file:Main.java
public static void main(String[] args) { List<Integer> numbers = new ArrayList<Integer>(); for (Integer i : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7)) numbers.add(i);//from w w w . j a v a2s. c o m printList(numbers); // 0,1,2,3,4,5,6,7 // replaces each element with twice its value for (int index = 0; index < numbers.size(); index++) { numbers.set(index, numbers.get(index) * 2); } printList(numbers); // does nothing because list is not being changed for (Integer number : numbers) { number++; } printList(numbers); // same as above -- just different syntax for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) { Integer number = iter.next(); number++; } printList(numbers); for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) { Integer number = iter.next(); iter.add(number + 1); } printList(numbers); for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) { Integer number = iter.next(); if (number % 2 == 0) // if number is even iter.remove(); // remove it from the collection } printList(numbers); // 1,3,5,7,9,11,13,15 // ListIterator<?> has a "set" method to replace elements for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) { Integer number = iter.next(); iter.set(number / 2); // divide each element by 2 } printList(numbers); // 0,1,2,3,4,5,6,7 // Use Java 8 Lambda List<Integer> list = numbers; list.stream().forEach(elem -> System.out.println("element " + elem)); }