Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ hs_err_pid*
*.xml
*.lst
.idea/*.*
example.iml
85 changes: 1 addition & 84 deletions src/main/java/com/java15/example/ExampleApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,9 @@
public class ExampleApplication {

public static void main(String[] args) {
int result = 0;
Set<String> opNames = new HashSet();
opNames.add("ADD");
opNames.add("SUB");


if(Objects.isNull(args) || 3 != args.length)
throw new IllegalArgumentException("Please enter the value like OperationName(ADD/SUB) value1 value2 : example ADD/SUB 3 4");
String operation = args[0].trim();
if(!opNames.contains(operation))
throw new IllegalArgumentException("Invalid operation put either ADD/SUB");
Integer value1 = Integer.valueOf(args[1]);
Integer value2 = Integer.valueOf(args[2]);
if("ADD".equals(operation)){
result = value1 + value2;
}
else if("SUB".equals(operation)){
result = value1 - value2;
}
System.out.println(result);

}

private static void extracted() {
Employee employee1 = new Employee(1,"sam");
Employee employee2 = new Employee(2,"rock");
List<Employee> list = new ArrayList<Employee>();
list.add(employee1);
list.add(employee1);
System.out.println("list"+list);
Set<Employee> set = new HashSet<Employee>();
set.add(employee1);
set.add(employee2);
System.out.println("set"+set);
Map<Employee,Integer> map = new HashMap<>();
map.put(employee1,1);
map.put(employee2,2);
System.out.println("map"+map);
Stream stream = list.parallelStream().map(Employee::getId).peek(e->System.out.println("eager"+e));
System.out.println("Lazy");
stream.forEach(System.out::println);
}

}
class Employee {
private int id;
private String name;

public Employee(int id, String name) {
this.id = id;
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return id == employee.id && Objects.equals(name, employee.name);
}

@Override
public int hashCode() {
return 1;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;

public class Apple {

private int weight;
private String color;

public Apple(int weight){
this.weight = weight;
}

public Apple(int weight,String color) {
this.weight = weight;this.color = color;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

@Override
public String toString() {
return "Apple{" +
"weight=" + weight +
", color='" + color + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;

public interface Colors {
String GREEN ="GREEN";
String RED ="RED";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;

import java.io.File;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

public class Test {

public static void main(String[] args) {
List<Apple> apples = getData();
//filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
//filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
//filterGroupApples(apples,(Apple a) -> Colors.RED.equals(a.getColor())).forEach((i,list) ->{System.out.println(i+""+list);});
//filterGroupApples(apples,(Apple a) -> {return true;}).forEach((i,list) ->{System.out.println(i+""+list);});
minAppleByWeight(apples).ifPresent(System.out::println);
maxAppleByWeight(apples).ifPresent(System.out::println);
}

static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
return inventory.stream().filter(p).collect(Collectors.toList());
}

static Map<Integer, List<Apple>> filterGroupApples(List<Apple> inventory, Predicate<Apple> p) {
return inventory.stream().filter(p).collect(groupingBy(Apple::getWeight));
}

static Optional<Apple> minAppleByWeight(List<Apple> inventory) {
return inventory.stream().min(Comparator.comparing(Apple::getWeight));
}

static Optional<Apple> maxAppleByWeight(List<Apple> inventory) {
return inventory.stream().max(Comparator.comparing(Apple::getWeight));
}

private static List<Apple> getData() {
Apple apple1 = new Apple(10,Colors.GREEN);
Apple apple2 = new Apple(1,Colors.RED);
Apple apple3 = new Apple(5,Colors.GREEN);
Apple apple4 = new Apple(3,Colors.RED);
Apple apple5 = new Apple(3,Colors.RED);
Apple apple6 = new Apple(5,Colors.GREEN);
List<Apple> apples = List.of(apple1,apple2,apple3,apple4,apple5,apple6);
return apples;
}

static File[] handlingFiles(){
File[] hiddenFiles = new File(".").listFiles(File::isHidden);
return hiddenFiles;
}

}
27 changes: 27 additions & 0 deletions src/main/java/com/java15/example/java8inaction/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.java15.example.java8inaction;

public class Test {

public static void main(String[] args){

int counter = 0;
int temp;
//9,3,6,5,7,2
int[] arr ={10,9,3,6,5,7,2,13};
for(int i= 0; i<arr.length;i++){
for(int j=1;j<arr.length;j++){
if(arr[i]<arr[j]){
counter++;
if(counter>2) {
break;
}
}
}

}
System.out.println(counter);


}

}