Commit 3be07cb7 authored by Ravinder Pannala's avatar Ravinder Pannala

Initial Commit

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="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="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Java8POC.iml" filepath="$PROJECT_DIR$/Java8POC.iml" />
</modules>
</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
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="8f863ec8-1d44-4b4b-b95d-fc49f43d843d" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Java8POC.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/Employee.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/EmployeeUtils.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/Collectors/CollectorsEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/Collectors/CollectorsEx1.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/Collectors/Joining.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/School.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/AnyMatchEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/ComparatorEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/ComparatorEx2.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/Filter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/Find.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/FlatMapEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/MapEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/MaxAndMin.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/ReduceEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/SkipAndLimit.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/FunctionalInterfaces/streams/StreamCreation.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/LamdaExpressions/Function/BiFunctionEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/LamdaExpressions/Function/FunctionEx1.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/LamdaExpressions/consumer/ConsumerEx1.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/LamdaExpressions/predicate/BipredicateEx.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/com/java8/features/LamdaExpressions/predicate/Example1.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2MSb37o1Lq5WiHVakfh9wOE4N1l" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;
}
}</component>
<component name="RunManager" selected="Application.CollectorsEx1">
<configuration name="CollectorsEx" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.java8.features.FunctionalInterfaces.Collectors.CollectorsEx" />
<module name="Java8POC" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.java8.features.FunctionalInterfaces.Collectors.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="CollectorsEx1" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.java8.features.FunctionalInterfaces.Collectors.CollectorsEx1" />
<module name="Java8POC" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.java8.features.FunctionalInterfaces.Collectors.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Joining" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.java8.features.FunctionalInterfaces.Collectors.Joining" />
<module name="Java8POC" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.java8.features.FunctionalInterfaces.Collectors.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="MaxAndMin" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.java8.features.FunctionalInterfaces.streams.MaxAndMin" />
<module name="Java8POC" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.java8.features.FunctionalInterfaces.streams.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StreamCreation" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.java8.features.FunctionalInterfaces.streams.StreamCreation" />
<module name="Java8POC" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.java8.features.FunctionalInterfaces.streams.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.CollectorsEx1" />
<item itemvalue="Application.Joining" />
<item itemvalue="Application.CollectorsEx" />
<item itemvalue="Application.StreamCreation" />
<item itemvalue="Application.MaxAndMin" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="8f863ec8-1d44-4b4b-b95d-fc49f43d843d" name="Changes" comment="" />
<created>1677763733236</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1677763733236</updated>
</task>
<servers />
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
package com.java8.features;
public class Employee {
private int id;
private String firstName;
private String lastName;
private double salary;
private String dept;
public Employee() {
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", salary=" + salary +
", dept='" + dept + '\'' +
'}';
}
public Employee(int id, String firstName, String lastName, Double salary, String dept) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
this.dept = dept;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Double getSalary() {
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
package com.java8.features;
import java.util.ArrayList;
import java.util.List;
public class EmployeeUtils {
public static List<Employee> fetchEmployee() {
List<Employee> empList = new ArrayList<>();
empList.add(new Employee(3434, null, "developer", 10001.00, "BA"));
empList.add(new Employee(123, "ashoke", "manager", 10001.00, "DEV"));
empList.add(new Employee(424, "bharat", "lead", 10002.00, "QA"));
empList.add(new Employee(621, "chandu", "director", 10005.00, "BA"));
empList.add(new Employee(93, "danish", "developer", 10006.00, "DEV"));
empList.add(new Employee(448, "eenadu", "developer", 10001.00, "BA"));
empList.add(new Employee(432, null, "developer", 10001.00, "BA"));
return empList;
}
}
package com.java8.features.FunctionalInterfaces.Collectors;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CollectorsEx {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,34,5,66,77);
//Averaging the List values
Double collect = list.stream().collect(Collectors.averagingDouble(a -> a));
System.out.println(collect);
//Summary Statistics
IntSummaryStatistics collect1 = list.stream().collect(Collectors.summarizingInt(a -> a));
System.out.println("Sum---"+collect1.getSum()+" avg--"+collect1.getAverage()+" min ----"+collect1.getMin()+" max ---"+collect1.getMax());
//Grouping the elements based on conditions
Function<Integer,Boolean> function = (i)->{
return i%2==0;
};
Map<Boolean, List<Integer>> collect2 = list.stream().collect(Collectors.groupingBy(function));
System.out.println("Grouping the list with even and add--->"+ collect2);
Map<Boolean, Long> collect3 = list.stream().collect(Collectors.groupingBy(function, Collectors.counting()));
System.out.println("Even and odd count in list----- "+ collect3);
}
}
package com.java8.features.FunctionalInterfaces.Collectors;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class CollectorsEx1 {
public static void main(String[] args) {
List<Employee> employeeList = EmployeeUtils.fetchEmployee();
//Get Employee List
List<Employee> collect = employeeList.stream().collect(Collectors.toList());
//Get Employee Set
Set<Employee> set = employeeList.stream().collect(Collectors.toSet());
//Get Employee Map
Map<Integer, Employee> map = employeeList.stream().collect(Collectors.toMap(Employee::getId, e -> e));
//Employee salary sum
Double collect1 = employeeList.stream().collect(Collectors.summingDouble(Employee::getSalary));
//CollectingAndThen
String collect2 = employeeList.stream().collect(Collectors
.collectingAndThen(Collectors.maxBy(Comparator.comparing(Employee::getSalary)),
(emp) -> emp.isPresent() ? emp.get().getFirstName() : "none"));
System.out.println("Collecting max Salaried Employee--->" + collect2);
//Partioning By Salaray
Map<Boolean, List<Employee>> collect3 = employeeList.stream().collect(Collectors.partitioningBy(i -> i.getSalary() > 10002.00));
System.out.println("Partioning by--->" + collect3);
//Grouping by Salary
Map<Double, List<Employee>> collect4 = employeeList.stream().collect(Collectors.groupingBy(i -> i.getSalary()));
//Partioning and Mapping to Uppercase
List<String> stringList = Arrays.asList("APPLE","KIWI","BANANA");
Map<Boolean, List<String>> collect5 = stringList.stream()
.collect(Collectors.partitioningBy(f -> f.length() > 4, Collectors.mapping(String::toUpperCase, Collectors.toList())));
System.out.println("Partioning and Mapping to Uppercase--->" + collect5);
Map<String, List<String>> collect6 = stringList.stream()
.collect(Collectors.groupingBy(s -> s,
Collectors.mapping(String::toUpperCase, Collectors.toList())));
System.out.println("Grouping and Mapping to Uppercase--->" + collect6);
}
}
package com.java8.features.FunctionalInterfaces.Collectors;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
public class Joining {
public static void main(String[] args) {
/* StringJoiner class internally uses StringBuilder class to join the strings.
It is placed in java.util package. Using Java 8 StringJoiner, you can join only the strings,
but not the array of strings or list of strings.
//StringJoiner class to join the String
*/
StringJoiner joiner = new StringJoiner(":", "[", "]");
joiner.add("Hello");
joiner.add("World");
joiner.add("Example");
System.out.println("Joining with String Joiner Class:--->" + joiner.toString());
StringJoiner joiner1 = new StringJoiner("|");
joiner1.add("Ravi").add("Ramu").add("Raju");
StringJoiner merge = joiner1.merge(joiner);
System.out.println("merge with String Joiner Class:--->" + merge + " length-->" + merge.length());
/*
String.join() method internally uses StringJoiner class.
This method can be used to join strings or array of strings or list of strings,
but only with delimiter not with prefix and suffix.
*/
//Joining String with String.join()
List<String> l1 = Arrays.asList("A", "B", "C");
String join = String.join("~", l1);
String b = String.join("|","A","B");
String c = String.join("*",new String[]{"AA","BB","CC"});
System.out.println("Joining with String ,join method---->" + join +"---##"+b +"Joining the Array of Strings-->"+c);
//Join String with Joining method
List<String> list = Arrays.asList("Ravi", "Ramu", "Raghu");
String collect = list.stream().collect(Collectors.joining("|","[","]"));
System.out.println("Joining String : " + collect);
}
}
package com.java8.features.FunctionalInterfaces;
@FunctionalInterface
interface Marks {
int total(int x);
}
@FunctionalInterface
interface Grade {
String grade();
static String grader() {
return "A";
}
}
class Student implements Marks, Grade {
@Override
public int total(int x) {
return 0;
}
@Override
public String grade() {
return "A";
}
}
public class School {
public static void main(String[] args) {
Marks marks = (i) -> {
return i * 10;
};
Marks marks1 = (a) -> {
return a + 10;
};
Grade g = () -> {
return "A";
};
System.out.println(g.grade());
System.out.println(marks.total(100));
System.out.println(marks1.total(10));
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class AnyMatchEx {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,4,5,6,2);
//Check if any elements of stream are divisible by 3 or not using
Predicate<Integer> anyMatchP = i -> i % 2 == 0;
boolean anyMatch = list.stream().anyMatch(anyMatchP);
System.out.println("any elements are divisible by 2 or not : "+ anyMatch);
//Check if all elements of stream are divisible by 3 or not using
Predicate<Integer> allMatchP = i-> i %3==0;
boolean b = list.stream().allMatch(allMatchP);
System.out.println("All elements are divisible by 3 or not : "+ b);
// returns true if either no elements of the stream match the provided predicate or the stream is empty,
// otherwise false
Predicate<Integer> noneMatchP = i->i<4;
boolean b1 = list.stream().noneMatch(noneMatchP);
System.out.println("To check that there is no number of length 4. "+b1);
//Count the number of elements in stream
long count = list.stream().count();
System.out.println("Count the number of elements avaialble in Stream"+ count);
//Distinct elements from the stream
list.stream().distinct().forEach(System.out::println);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collector;
public class ComparatorEx {
public static void main(String[] args) {
List<Employee> employeeList = EmployeeUtils.fetchEmployee();
Comparator<Employee> employeeComparator = Comparator.comparing(Employee::getFirstName);
Comparator<Employee> reversed = employeeComparator.reversed();
Comparator<Employee> employeeComparator1 = Comparator.comparingInt(Employee::getId);
Comparator<Employee> employeeComparator2 = Comparator.comparingDouble(Employee::getSalary);
Comparator<Employee> employeeComparator3 = Comparator.nullsFirst(employeeComparator);
Comparator<Employee> employeeComparator4 = Comparator.nullsLast(employeeComparator);
Comparator<Employee> employeeComparator5 = Comparator.comparing(Employee::getLastName).thenComparing(Employee::getId);
Comparator<Employee> employeeComparator6 = Comparator.comparingInt(Employee::getId).thenComparingDouble(Employee::getSalary);
//Sort Last Name in Reverse Order
employeeList.stream().sorted(Comparator.comparing(Employee::getLastName, Comparator.reverseOrder())).forEach(System.out::println);
//Sort Last Name in Natural Order
employeeList.stream().sorted(Comparator.comparing(Employee::getLastName)).forEach(System.out::println);
//Sort Last Name in Reverser Order
employeeList.stream().sorted(Comparator.comparing(Employee::getLastName).reversed()).forEach(System.out::println);
//Sort By LastName and Id
employeeList.stream().sorted(employeeComparator5).forEach(System.out::println);
//Sort By ID and Salary
employeeList.stream().sorted(employeeComparator6).forEach(System.out::println);
//Collections ReverserOrdered
employeeList.stream().sorted(Collections.reverseOrder(employeeComparator1)).forEach(System.out::println);
//null values coming as first
employeeList.stream()
.sorted(Comparator.comparing(Employee::getFirstName,
Comparator.nullsFirst(Comparator.naturalOrder())))
.forEach(System.out::println);
//null Values coming as Last
employeeList.stream()
.sorted(Comparator.comparing(Employee::getFirstName, Comparator.nullsLast(Comparator.reverseOrder())))
.forEach(System.out::println);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ComparatorEx2 {
public static void main(String[] args) {
List<String> list = Arrays.asList("Hello", "Cat", null, "Apple", "Is");
list.stream().sorted(Comparator.comparing(s -> s, Comparator.nullsLast(Comparator.reverseOrder()))).forEach(System.out::println);
list.stream().sorted(Comparator.comparing(s -> s, Comparator.nullsFirst(Comparator.naturalOrder()))).forEach(System.out::println);
list.stream().sorted(Comparator.comparing(s->s)).forEach(System.out::println);
list.stream().sorted(Comparator.comparing(s->s,Comparator.reverseOrder())).forEach(System.out::println);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class Filter {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 3, 4, 5, 6, 7);
Predicate<Integer> integerPredicate = (i) -> i < 5;
Consumer<Integer> integerConsumer = (i) -> System.out.println(i);
list.stream().filter(integerPredicate).forEach(integerConsumer);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
public class Find {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,3,4,5,6,7);
//FindAny any element from stream
Optional<Integer> any = list.stream().findAny();
//list.stream().parallel().findAny() : it will give different value
if(any.isPresent()){
System.out.println(any.get());
}
//Find First : it always gives first element from the Stream
Optional<Integer> first = list.stream().findFirst();
if(first.isPresent()){
System.out.println(first.get());
}
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
public class FlatMapEx {
public static void main(String[] args) {
List<List<Integer>> list = Arrays.asList(Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8));
list.stream().flatMap(List::stream).forEach(System.out::println);//1,2,3,4,5,6,7,8
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;
import java.util.stream.Stream;
public class MapEx {
public static void main(String[] args) {
List<String> list = Arrays.asList("ravi", "ramu", "raghu");
Function<String, String> function = (s) -> s.toUpperCase();
Function<String, String> f1 = (s) -> s.concat("D");
Stream<String> stringStream = list.stream().map(function).map(f1);
stringStream.forEach(s -> System.out.println(s));
//maptoInt
List<String> list1 = Arrays.asList("1", "2", "3", "4");
IntConsumer consumer = i -> System.out.println(i);
ToIntFunction<String> integerFunction = Integer::parseInt;
list1.stream().mapToInt(integerFunction).forEach(consumer);
//MapToDouble
ToDoubleFunction<String> doubleFunction = Double::valueOf;
DoubleConsumer doubleConsumer = i -> System.out.println(i);
list1.stream().mapToDouble(doubleFunction).forEach(doubleConsumer);
//MaptoLong
ToLongFunction<String> longFunction = Long::parseLong;
LongConsumer longConsumer = i -> System.out.println(i);
list1.stream().mapToLong(longFunction).forEach(longConsumer);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class MaxAndMin {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,45,3,89,32,22);
//get Max element from List
Optional<Integer> max = list.stream().max(Comparator.comparing(a -> a));
if(max.isPresent()){
System.out.println(max.get());
}
//get Min element from List
Integer integer = list.stream().min(Comparator.comparing(a -> a)).get();
System.out.println(integer);
List<Employee> employees = EmployeeUtils.fetchEmployee();
//Minimum salary employee
Employee employee = employees.stream().min(Comparator.comparing(Employee::getSalary)).get();
System.out.println("Minimum salaried employee"+ employee);
//Maximum salaried EMployee
Employee employee1 = employees.stream().max(Comparator.comparing(Employee::getSalary)).get();
System.out.println("Maximum salaried employee"+ employee1);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;
public class ReduceEx {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);
Integer integer = list.stream().reduce(0, (a, b) -> a + b);
System.out.println(integer);
//Reduce to Some of all elements in List
BinaryOperator<Integer> binaryOperator = (a, b) -> a + b;
Optional<Integer> reduce = list.stream().reduce(binaryOperator);
if (reduce.isPresent()) {
System.out.println(reduce.get());
}
//reduce to max number in list
BinaryOperator<Integer> binaryOperator1 = (a, b) -> a > b ? a : b;
Optional<Integer> reduce1 = list.stream().reduce(binaryOperator1);
if (reduce1.isPresent()) {
System.out.println(reduce1.get());
}
//Based on length reduce to Upper case or lower case
List<String> stringList = Arrays.asList("Ravi", "Pannala", "Reddy");
BinaryOperator<String> binaryOperator2 = (a, b) -> a.length() > b.length() ? a.toUpperCase() : b.toUpperCase();
Optional<String> reduce2 = stringList.stream().reduce(binaryOperator2);
if (reduce2.isPresent()) {
System.out.println(reduce2.get());
}
//Sum of salaries in employee list
List<Employee> employees = EmployeeUtils.fetchEmployee();
Double reduce3 = employees.stream().map(Employee::getSalary).reduce(0.0, (a, b) -> a + b, Double::sum);
System.out.println("Sum of all salaries in employees "+ reduce3);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SkipAndLimit {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,4,5,6,7);
//Skip the elemets from list
List<Integer> collect1 = list.stream().skip(1).collect(Collectors.toList());
System.out.println("Skip the n elements from list"+ collect1);
//Limit the Elemets from list
List<Integer> collect = list.stream().limit(1).collect(Collectors.toList());
System.out.println("Limit the n elements from list"+ collect);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamCreation {
public static void main(String[] args) {
Stream<Object> empty = Stream.empty();
System.out.println(empty.findFirst());//Optional.empty
//Create stream with of
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
//Create Stream with Collection
Collection<String> integers = Arrays.asList("A","B","C");
Stream<String> stream = integers.stream();
stream.forEach(System.out::println);
List<String> list = Arrays.asList("D","E","F");
Stream<String> stream1 = list.stream();
stream1.forEach(System.out::println);
//From Array
String[] stringArray = new String[]{"Ravi","Raju","Pannala"};
Stream<String> stream2 = Arrays.stream(stringArray);
stream2.forEach(System.out::println);
//StreamBuilder
Stream<Character> build = Stream.<Character>builder().add('a').add('b').add('c').add('d').build();
build.forEach(System.out::println);
//create stream with generate
Supplier<Integer> integerSupplier = ()-> 10;
Stream<Integer> limit = Stream.generate(integerSupplier).limit(10);
limit.forEach(System.out::println);
//Create Stream with Iterate
UnaryOperator<Integer> integerUnaryOperator = i->i*10;
Stream<Integer> limit1 = Stream.iterate(1, integerUnaryOperator).limit(10);
limit1.forEach(System.out::println);
//With The help of Primities
IntStream range = IntStream.range(0, 4);
range.forEach(System.out::println);
DoubleStream limit2 = DoubleStream.iterate(0.0, i -> i + 10.0).limit(2);
limit2.forEach(System.out::println);
}
}
package com.java8.features.LamdaExpressions.Function;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
public class BiFunctionEx {
public static void main(String[] args) {
List<Employee> employees = EmployeeUtils.fetchEmployee();
//List<Employee> matchedEmployees = fetchEmployeesMatchingToKey.apply("MOHAN", employees);
}
private BiFunction<String, List<Employee>, List<Employee>> fetchEmployeesMatchingToKey = (search, empList) -> {
List<Employee> collect = empList.stream().filter(emp -> this.biPredicate.test(emp, search)).collect(Collectors.toList());
return collect;
};
private BiPredicate<Employee, String> biPredicate = (employee, search) -> {
boolean matches = employee.getFirstName().toLowerCase().matches(search)
|| employee.getLastName().toUpperCase().matches(search);
return matches;
};
public void biFunctionExamples() {
BiFunction<Integer, Integer, Integer> biFunction = Integer::compareTo;
System.out.println(biFunction.apply(10, 20));
BiFunction<Integer, Integer, Integer> biFunction1 = Integer::max;
System.out.println(biFunction1.apply(10, 20));
BiFunction<String, String, String> biFunction2 = (s1, s2) -> {
return s1.toUpperCase() + s2.toUpperCase();
};
System.out.println(biFunction2.apply("Ravi", "Pannala"));
BiFunction<Integer, Integer, Integer> biFunction3 = (i, i2) -> {
return i * i2;
};
System.out.println(biFunction3.apply(20, 30));
}
}
package com.java8.features.LamdaExpressions.Function;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public class FunctionEx1 {
public static void main(String[] args) {
Function<Integer,String> function= (i)-> i+"ravi";
System.out.println(function.apply(2));
Function<Integer,Integer> integerFunction= (i)-> i*i;
System.out.println(integerFunction.apply(5));
Function<String,String> function1= x->"First"+x;
Function<String,String> function2=x->"Second"+x;
Function<String, String> stringFunction = function1.andThen(function2);
System.out.println(stringFunction.apply("Ravinder"));
Function<String, String> andThen2 = function2.andThen(function1);
System.out.println("andThen Output2 : " + andThen2.apply("Ravinder"));
Function<String, String> compose = function1.compose(function2);
System.out.println(compose.apply("Ravinder"));
List<Employee> employees = EmployeeUtils.fetchEmployee();
Function<Employee,String> function3= (employee)->{
return employee.getFirstName()+employee.getLastName();
};
Consumer<String> employeeConsumer=employee->System.out.println(employee);
employees.stream().map(function3).forEach(employeeConsumer);
Function<Double, BigDecimal> decimalFunction= (salary)->{
return new BigDecimal(salary);
};
employees.stream().map(e->e.getSalary()).map(decimalFunction).forEach(System.out::println);
Function<Employee,Employee> employeeFunction= (employee)->{
String fullName = new StringBuilder().append(employee.getFirstName()).append(employee.getLastName()).toString();
employee.setFirstName(fullName);
return employee;
};
Consumer<Employee> employeeConsumer1 = (e)->System.out.println(e);
employees.stream().map(employeeFunction).forEach(employeeConsumer1);
}
}
package com.java8.features.LamdaExpressions.consumer;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class ConsumerEx1 {
public static void main(String[] args) {
Consumer<String> consumer = (string) -> {
System.out.println("Hello");
};
Consumer<Integer> integerConsumer = (i) -> {
return;
};
consumer.accept("Ravi");
integerConsumer.accept(1);
Consumer<String> stringConsumer = (string) -> System.out.println(string.toLowerCase());
Consumer<String> stringConsumer1 = stringConsumer.andThen(s -> System.out.println(s.toUpperCase()));
Consumer<String> stringConsumer2 = s -> System.out.println(s.charAt(2));
stringConsumer1.andThen(stringConsumer2).accept("Ravinder");
List<Employee> employees = EmployeeUtils.fetchEmployee();
Consumer<Employee> employeeConsumer = emp -> System.out.println(emp);
employees.stream().forEach(employeeConsumer);
//Bi Consumer
BiConsumer<String,String> biConsumer = (s1,s2)->System.out.println(s1+" "+s2);
BiConsumer<String, String> biConsumer1 = biConsumer.andThen((s1, s2) -> System.out.println(s1.toLowerCase() + "" + s2.toUpperCase()));
biConsumer1.accept("Ravi","Pannala");
}
}
package com.java8.features.LamdaExpressions.predicate;
import java.util.function.BiPredicate;
public class BipredicateEx {
public static void main(String[] args) {
BiPredicate<String, String> biPredicate = (s, s1) -> s.matches("Ra") && s1.contains("Ra");
BiPredicate<String, String> biPredicate1 = biPredicate.and((s, s1) -> s.endsWith("Ra") && s1.startsWith("Ra"));
System.out.println(biPredicate1.test("Ra", "Ra"));
BiPredicate<Integer,Integer> biPredicate2 = (i1,i2)->i1>20&& i2<20;
System.out.println(biPredicate2.test(30,10));
}
}
package com.java8.features.LamdaExpressions.predicate;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example1 {
public static void main(String[] args) {
Predicate<String> p = s -> s.equals("Ravi");
Predicate<String> pr = str -> str.length() < 12;
boolean test = pr.or(str -> str.contains("Ra")).and(str -> str.equals("Ravi")).test("Ravinder");
//System.out.println(test);
//System.out.println(p.test("Hello"));
//System.out.println(p.test("Ravi"));
Predicate<Integer> integerPredicate = i -> i % 2 == 0;
//System.out.println(integerPredicate.test(10));
Predicate<Integer> and = integerPredicate.or(i -> i.equals(0)).and(i -> i >= 0);
System.out.println(and.test(20));
Predicate<String> stringPredicate = (string)->{
List<String> list = Arrays.asList("Ravi","Ramu");
boolean contains = list.contains(string);
return contains;
};
System.out.println(stringPredicate.test("Hello"));
Predicate<Integer> even = i->i%2==0;
Predicate<Integer> odd = even.negate();
List<Integer> integers = Arrays.asList(1,4,2,3,5,6,2,3);
List<Integer> collect = integers.stream().filter(even).collect(Collectors.toList());
List<Integer> collect1 = integers.stream().filter(odd).collect(Collectors.toList());
collect1.forEach(c->System.out.println(c));
//BiPredicate
BiPredicate<Integer,String> biPredicate= (i,s)->i>3 && s.equals("Ravi");
System.out.println(biPredicate.test(10,"Ravi"));
}
}
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