Commit 215e0450 authored by Pinky Sabu's avatar Pinky Sabu

Java 8 review comments updated [Pinky Sabu]

parent a600359b
X
(gradle/wrapper/gradle-wrapper.properties,f/b/fbe448ebfc3eb2d4e308f6b8b043666f5b57235e
7
gradlew,5/b/5bbfa66edb4db3c7c33c5181f43510990d3307f9
<
build.gradle,f/0/f07866736216be0ee2aba49e392191aeae700a35
?
settings.gradle,0/5/05efc8b1657769a27696d478ded1e95f38737233
;
gradlew.bat,2/a/2a45a911a8f1836b0b6c5b758962572012d8f8c3
\ 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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
......@@ -5,6 +5,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
public class BinaryOperatorExamples {
......
......@@ -12,16 +12,15 @@ public class ConsumerExamples {
}
private static void inbuiltBiConsumer() {
Map<String,String> map=new HashMap<>();
map.put("1","1");
map.put("2","2");
map.forEach((x,y)->System.out.println("inbuiltBiConsumer"+x.concat(y)));
Map<String, String> map = new HashMap<>();
map.put("1", "1");
map.put("2", "2");
map.forEach((x, y) -> System.out.println("inbuiltBiConsumer" + x.concat(y)));
}
private static void biConsumer() {
BiConsumer<String, String> biConsumer = (x, y) -> x.concat(y);
System.out.println(biConsumer);
BiConsumer<String, String> biConsumer = (x, y) -> System.out.println(x.concat(y));
biConsumer.accept("hi", "hello");
}
private static void simpleConsumer() {
......
......@@ -19,13 +19,16 @@ public class GroupByExamples {
public static void main(String[] args) {
List<Person> list = Arrays.asList(new Person(1, "Alex", 100d, new Department(1, "HR")), new Person(2, "Brian",
200d, new Department(1, "HR")), new Person(3, "Charles", 900d, new Department(2, "Finance")), new Person(4,
"David", 200d, new Department(2, "Finance")), new Person(5, "Edward", 200d, new Department(2, "Finance")),
new Person(6, "Frank", 800d, new Department(3, "ADMIN")), new Person(7, "George", 900d, new Department(3,
"ADMIN")));
List<Person> list = Arrays.asList(
new Person(1, "Alex", 100d, new Department(1, "HR")),
new Person(2, "Brian", 200d, new Department(1, "HR")),
new Person(3, "Charles", 900d, new Department(2, "Finance")),
new Person(4, "David", 200d, new Department(2, "Finance")),
new Person(5, "Edward", 200d, new Department(2, "Finance")),
new Person(6, "Frank", 800d, new Department(3, "ADMIN")),
new Person(7, "George", 900d, new Department(3, "ADMIN")));
System.out.println("GroupByExamples by Simple Condition :" + simpleCondition(list));
System.out.println("GroupByExamples by Simple Condition :" + simpleCondition(list));
System.out.println(" collect only the person ids in all departments" + personIds(list));
......@@ -40,6 +43,12 @@ public class GroupByExamples {
System.out.println("GroupByExamples with Filtering" + filter(list));
//Reveiew suggestions
// return map for complex grouping by
System.out.println("GroupByExamples by Complex Condition" + complexGroupBy(list));
Map<Integer, Department> departmentMap = list
.stream()
.collect(Collectors.toMap(Person::getId, Person::getDepartment));
......@@ -66,14 +75,23 @@ public class GroupByExamples {
return list.stream().collect(Collectors.groupingBy(Person::getDepartment, counting()));
}
private static Map<Object, List<Integer>> complex(final List<Person> list) {
return list
.stream()
.collect(Collectors.groupingBy(person -> new Pair<>(person.getSalary(), person.getSalary()),
.collect(Collectors.groupingBy(person -> new Pair<>(person.getSalary(), person.getDepartment()),
Collectors.mapping(Person::getId, toList())));
}
private static Map<Pair<Double, Department>, List<Person>> complexGroupBy(final List<Person> list) {
return list
.stream()
.collect(Collectors.groupingBy(person -> new Pair<>(person.getSalary(), person.getDepartment())));
}
private static Map<Department, List<Integer>> personIds(final List<Person> list) {
return list.stream().collect(Collectors.groupingBy(Person::getDepartment, mapping(Person::getId, toList())));
}
......
......@@ -24,12 +24,10 @@ public class PredicateExamples {
BiPredicate<String, Integer> biPredicate = (x, y) -> {
return (x.equals("google.com") || y == 0);
};
System.out.println(domainList
.stream()
.filter(domain -> biPredicate.test(domain.getName(), domain.getScore()))
.collect(Collectors.toList()));
}
private static void biPredicate() {
BiPredicate<String, Integer> biPredicate = (x, y) -> x.length() == y;
System.out.println(biPredicate.test("Bat", 3));
......
......@@ -5,7 +5,7 @@ import java.util.stream.Collectors;
public class SortExample {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8);
list = list.stream().sorted((o1, o2) -> o2.compareTo(o1)).collect(Collectors.toList());
list = list.stream().sorted((o1, o2) -> o1.compareTo(o1)).collect(Collectors.toList());
System.out.println(list);
}
}
package domain;
import java.util.Optional;
public class Address {
private Optional<String> addressText;
private Optional<Long> contactNumber;
/**
* Parameterised constructor
* @param address
* @param contactNumber
*/
public Address(String address, long contactNumber) {
this.addressText = Optional.ofNullable(address);
this.contactNumber = Optional.ofNullable(contactNumber);
}
public Optional<String> getAddressText() {
return addressText;
}
public void setAddressText(Optional<String> addressText) {
this.addressText = addressText;
}
public Optional<Long> getContactNumber() {
return contactNumber;
}
public void setContactNumber(Optional<Long> contactNumber) {
this.contactNumber = contactNumber;
}
}
/**
*
*/
package domain;
import enums.Gender;
import java.util.List;
import java.util.Optional;
public class Employee {
private String firstName;
private String lastName;
private String fullName;
private List<Address> addresses;
private Long salary;
private String departmentName;
private Integer emplId;
private Gender gender;
/**
* Parameterised constructor.
*
* @param firstName first name
* @param lastName last name
* @param address addresses
* @param salary salary
* @param departmentName department name
* @param gender gender(male or female)
*/
public Employee(int emplId, String firstName, String lastName, List<Address> address, long salary,
String departmentName, Gender gender) {
this.emplId = emplId;
this.firstName = firstName;
this.lastName = lastName;
this.addresses = address;
this.salary = salary;
this.departmentName = departmentName;
this.gender = gender;
}
public Optional<String> getFirstName() {
return Optional.ofNullable(firstName);
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Optional<String> getLastName() {
return Optional.ofNullable(lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Optional<List<Address>> getAddresses() {
return Optional.ofNullable(addresses);
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public Optional<Long> getSalary() {
return Optional.ofNullable(salary);
}
public void setSalary(Long salary) {
this.salary = salary;
}
public Optional<String> getFullName() {
return Optional.ofNullable(fullName);
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Optional<String> getDepartmentName() {
return Optional.ofNullable(departmentName);
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public Optional<Integer> getEmplId() {
return Optional.ofNullable(emplId);
}
public void setEmplId(Integer emplId) {
this.emplId = emplId;
}
public Optional<Gender> getGender() {
return Optional.ofNullable(gender);
}
public void setGender(Gender gender) {
this.gender = gender;
}
/**
* Displays employee information.
*
* @return string which displays employee full name and salary
*/
public void displayEmployee() {
System.out.println(new StringBuilder()
.append("Employee Id : ")
.append(this.emplId)
.append("Employee Name:: ")
.append(this.firstName)
.append(this.lastName)
.append(" and Salary :: ")
.append(this.salary)
.append(" Dept ")
.append(this.departmentName)
.toString());
}
}
/**
*
*/
package enums;
public enum Gender {
MALE, FEMALE;
public static void main(String[] args) {
Gender s = Gender.MALE;
System.out.println(s.name().toLowerCase());
}
}
package reviewcomments;
import domain.Address;
import domain.Employee;
import enums.Gender;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class EmployeeUtil {
/**
* Fetch employees.
*
* @return employee optional object
*/
public static Optional<List<Employee>> fetchEmployees() {
List<Employee> employeesList = new ArrayList<Employee>();
Address empAddress1 = new Address("Hyderabad, address1", 0);
Address empAddress2 = new Address("Hyderabad, address2", 0);
Address empAddress3 = new Address("Hyderabad, address3", 0);
List<Address> empAddresses = Arrays.asList(empAddress1, empAddress2, empAddress3);
Employee emp = new Employee(10101, "Naga ", "maheshwar", empAddresses, 9000, "computers", Gender.MALE);
employeesList.add(emp);
Address emp2Address1 = new Address("Vikarabad, address1", 37468787);
Address emp2Address2 = new Address("Kondapur, address2", 387468);
Address emp2Address3 = new Address("Hyderabad, address3", 0);
List<Address> emp2Addresses = Arrays.asList(emp2Address1, emp2Address2, emp2Address3);
Employee emp2 = new Employee(202020, "Lakshmi", "K", emp2Addresses, 5000, "chemical", Gender.FEMALE);
employeesList.add(emp2);
Address emp3Address1 = new Address("Miyapur, address1", 0);
Address emp3Address2 = new Address("Hitech city, address2", 37473657);
Address emp3Address3 = new Address("Gachibowli, address3", 0);
List<Address> emp3Addresses = Arrays.asList(emp3Address1, emp3Address2, emp3Address3);
Employee emp3 = new Employee(303030, "Nagalakshmi", "K", emp3Addresses, 6000, "civil", Gender.FEMALE);
employeesList.add(emp3);
Address emp4Address1 = new Address("Anantapur, address1", 0);
Address emp4Address2 = new Address("Kurnool, address3", 0);
List<Address> emp4Addresses = Arrays.asList(emp4Address1, emp4Address2, empAddress3);
Employee emp4 = new Employee(4040404, "Naga Mohan", "M", emp4Addresses, 9000, "computers", Gender.MALE);
employeesList.add(emp4);
Address emp5Address1 = new Address("Bangalore, address1", 0);
Address emp5Address2 = new Address("Chennai, address3", 0);
List<Address> emp5Addresses = Arrays.asList(emp5Address1, emp5Address2);
Employee emp5 = new Employee(203404, "Rajendran", "T", emp5Addresses, 7000, "civil", Gender.MALE);
employeesList.add(emp5);
Address emp6Address1 = new Address("Delhi, address1", 0);
Address emp6Address2 = new Address("Pune", 0);
List<Address> emp6Addresses = Arrays.asList(emp6Address1, emp6Address2);
Employee emp6 = new Employee(101010, "Rajeshwari", "T", emp6Addresses, 17000, "civil", Gender.FEMALE);
employeesList.add(emp6);
return Optional.of(employeesList);
}
}
package reviewcomments;
import domain.Employee;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Function;
public class OptionalExample {
public static void main(String[] args) {
System.out.println("displayFirstEmployeeFromList" + displayFirstEmployeeFromList().get().getFirstName());
maxEmployeeId();
}
private static Function<Employee, Employee> employeeFullName = employee -> {
employee.setFirstName(employee.getFirstName().get().concat(employee.getLastName().get()));
return employee;
};
private static Optional<Employee> displayFirstEmployeeFromList() {
return EmployeeUtil.fetchEmployees().orElseGet(Collections::emptyList).stream().findFirst();
}
private static void maxEmployeeId() {
EmployeeUtil
.fetchEmployees()
.orElseGet(Collections::emptyList)
.stream()
.min(Comparator.comparing(o -> o.getEmplId().get()))
.ifPresent(employee -> System.out.println(employee.getEmplId()));
}
}
\ No newline at end of file
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