java 8 streams, lambda, method reference

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="java-8-streams" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="java-8-streams" options="-parameters" />
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
Qadeer,23,6
Naveed,25,6
Kevin,29,6
Jimmy,29
Kate,34,6
Qadeer
Naveed
Kevin
Jimmy
Kate
Josh
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
public class CheckValue {
public int add(int a, int b) {
return a + b;
}
}
public class CheckValueUtil {
public static int add(int a, int b) {
return a + b;
}
}
public class Item {
private int id;
private Integer price;
public Item(int id, Integer price) {
this.id = id;
this.price = price;
}
public Item(int id) {
this.id = id;
this.price = 1000;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Item{" +
"id=" + id +
", price=" + price +
'}';
}
}
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamApp {
public static void main(String[] args) throws IOException {
//// 1. created integer stream from 1 to 9 with IntStream
// IntStream
// .range(1, 10)
// .forEach(System.out::print);
// System.out.println();
//
// // 2. create integer stream skipping first 5 elements
// IntStream
// .range(1, 10)
// .skip(5)
// .forEach(System.out::print);
// // 3. create integer stream then sum it.
// System.out.println(IntStream
// .range(1,5)
// .sum());
// //4. Stream.of, create stream of Strings, then sort and find first element
// Stream.of("qadeer", "jack", "hammad", "alex")
// .sorted()
// .findFirst()
// .ifPresent(System.out::println);
// // 5. sort, filter and print
// String[] names = {"Kevin", "Alex", "Jimmy", "Carl", "Ben", "Adam"};
// Arrays.stream(names)
// .filter(x -> x.startsWith("A"))
// .sorted()
// .forEach(System.out::println);
// // 6. average of squares
// // won't allow non-primitive types for list of integers because it allows null values
// // which leads to errors when using sum and average methods
// int[] numbers = {1,2,3,4,5};
// Arrays.stream(numbers)
// .map(x -> x * x)
// .average()
// .ifPresent(System.out::println); // average returns an optionalDouble hence we can use ifPresent() method
// // 7. map all strings of a list to lowerCase
// // filter a list of strings by their last letter
// List<String> names = Arrays.asList("Kevin", "Alex", "Jimmy", "Carl", "Ben", "Adam");
// names
// .stream()
// .map(String::toLowerCase)
// .filter(x -> x.endsWith("m"))
// .forEach(System.out::println);
// // 8. Stream rows from a text file, sort, filter, and print
// // must add throws ioException in main method
// Stream<String> names = Files.lines(Paths.get("names.txt"));
// names
// .sorted()
// .filter(x -> x.startsWith("Q"))
// .forEach(System.out::println);
// names.close();
//
// String name = "Qadeer";
// String n1 = "Ahmed";
// String n2 = "Naveed";
// String n3 = "Kevin";
// String n4 = "Brad";
// List<String> nameList = Arrays.asList(n1,n2,n3,n4);
// nameList
// .stream()
// .map(x -> {
// if (Objects.equals(x, "Naveed")) {
// x = name;
// }
// return x;
// })
// .forEach(System.out::println);
// 9. Stream rows from a text file and filter by letters of name
// List<String> names = Files.lines(Paths.get("names.txt"))
// .filter(x -> x.contains("Ka")) // case sensitive
// .collect(Collectors.toList());
//
// names.forEach(System.out::println);
// 10. Stream rows from a csv file and count rows
// Stream<String> data = Files.lines(Paths.get("data.csv"));
// long rowCount = data
// .map(x -> x.split(","))
// .filter(x -> x.length == 3)
// .count();
// System.out.println(rowCount);
// data.close();
// // 11. Stream rows from a csv file and parse date
// Stream<String> data2 = Files.lines(Paths.get("data.csv"));
// data2
// .map(x -> x.split(","))
// .filter(x -> x.length == 3)
// .filter(x -> Integer.parseInt(x[1]) > 23)
// .forEach(x -> System.out.println(x[0] + " " + x[1] + " " + x[2]));
// // 12. Stream rows from csv, store fields in a HashMap
// Stream<String> data3 = Files.lines(Paths.get("data.csv"));
// Map<String, Integer> mapStore = new HashMap<>();
// mapStore = data3
// .map(x -> x.split(","))
// .filter(x -> x.length == 3)
// .filter(x -> Integer.parseInt(x[1]) > 23)
// .collect(Collectors.toMap(
// x -> x[0],
// x -> Integer.parseInt(x[1])));
// data3.close();
// for (String key: mapStore.keySet()) {
// System.out.println(key + " " + mapStore.get(key));
// }
// // 13. Reduction - sum
// double total = Stream.of(1.1,5.3,1.3,6.1)
// .reduce(0.0, (Double a, Double b) -> a + b);
// System.out.println("Total = " + total);
// // 14. Reduction - summary statistics
// // returns the count, sum, min, average, and max of integer list
// IntSummaryStatistics summary = IntStream.of(1,5,2,1,8)
// .summaryStatistics();
// System.out.println(summary);
// List<Integer> integerList = Arrays.asList(1,2,3,4,5);
// Integer sums = integerList
// .stream()
// .reduce(0, (x, y) -> x + y);// x = result of previous application
// // y = current element in the stream
// // 0 = initial value
// System.out.println(sums);
// List<Integer> integerList = Arrays.asList(1,2,3,-4,5);
// Integer sums = integerList
// .stream()
// .reduce(0, CheckValueUtil::add);
// System.out.println(sums);
// List<Integer> integers = Arrays.asList(1,2,3,4,5);
// Integer sums = integers.stream()
// .mapToInt(Integer::intValue)
// .sum();
// System.out.println(sums);
// Item item1 = new Item(1, 10);
// Item item2 = new Item(2, 20);
// Item item3 = new Item(3, 33);
// Item item4 = new Item(4, 45);
//
// List<Item> items = Arrays.asList(item1, item2, item3, item4);
//
// Integer sum = items.stream()
// .map(Item::getPrice)
// .reduce(0,Integer::sum);
//
// Integer sum1 = items.stream()
// .map(Item::getPrice)
// .reduce(0, CheckValue::add);
//
// Integer sum2 = items.stream()
// .map(Item::getPrice)
// .reduce(0, (a, b) -> a + b);
//
// Integer sum3 = items.stream()
// .map(Item::getPrice)
// .collect(Collectors.summingInt(Integer::intValue));
//
// Integer sum4 =
// items.stream()
// .mapToInt(Item::getPrice)
// .sum();
//
// System.out.println(sum);
// System.out.println(sum1);
// System.out.println(sum2);
// System.out.println(sum3);
// System.out.println(sum4);
// String string = "One 1 Two 2 Three 3 Four 4 Five 5 Six 6";
// List<String> sums = Arrays.stream(string.split(" "))
// .filter((s) -> !s.matches("\\d"))
// .collect(Collectors.toList());
//
// System.out.println(sums);
// // instance methods of particular objects
// CheckValue value = new CheckValue();
// List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
// Integer sums = numbers.stream()
// .reduce(0, value::add);
// System.out.println(sums);
// List<Integer> ids = Arrays.asList(1,2,3,4,5);
// Item[] items = ids.stream()
// .map(Item::new)
// .toArray(Item[]::new);
// System.out.println(Arrays.toString(items));
// Arrays.stream(items).map(Item::toString).forEach(System.out::println);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment