Skip to content

Commit fa76658

Browse files
Merge pull request #75 from sayed-moin-ahmed/develop
Develop
2 parents 4d5f276 + ba86078 commit fa76658

File tree

6 files changed

+131
-84
lines changed

6 files changed

+131
-84
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ hs_err_pid*
2626
*.xml
2727
*.lst
2828
.idea/*.*
29+
example.iml

src/main/java/com/java15/example/ExampleApplication.java

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,92 +13,9 @@
1313
public class ExampleApplication {
1414

1515
public static void main(String[] args) {
16-
int result = 0;
17-
Set<String> opNames = new HashSet();
18-
opNames.add("ADD");
19-
opNames.add("SUB");
16+
2017

21-
if(Objects.isNull(args) || 3 != args.length)
22-
throw new IllegalArgumentException("Please enter the value like OperationName(ADD/SUB) value1 value2 : example ADD/SUB 3 4");
23-
String operation = args[0].trim();
24-
if(!opNames.contains(operation))
25-
throw new IllegalArgumentException("Invalid operation put either ADD/SUB");
26-
Integer value1 = Integer.valueOf(args[1]);
27-
Integer value2 = Integer.valueOf(args[2]);
28-
if("ADD".equals(operation)){
29-
result = value1 + value2;
30-
}
31-
else if("SUB".equals(operation)){
32-
result = value1 - value2;
33-
}
34-
System.out.println(result);
35-
36-
}
37-
38-
private static void extracted() {
39-
Employee employee1 = new Employee(1,"sam");
40-
Employee employee2 = new Employee(2,"rock");
41-
List<Employee> list = new ArrayList<Employee>();
42-
list.add(employee1);
43-
list.add(employee1);
44-
System.out.println("list"+list);
45-
Set<Employee> set = new HashSet<Employee>();
46-
set.add(employee1);
47-
set.add(employee2);
48-
System.out.println("set"+set);
49-
Map<Employee,Integer> map = new HashMap<>();
50-
map.put(employee1,1);
51-
map.put(employee2,2);
52-
System.out.println("map"+map);
53-
Stream stream = list.parallelStream().map(Employee::getId).peek(e->System.out.println("eager"+e));
54-
System.out.println("Lazy");
55-
stream.forEach(System.out::println);
56-
}
57-
58-
}
59-
class Employee {
60-
private int id;
61-
private String name;
62-
63-
public Employee(int id, String name) {
64-
this.id = id;
65-
this.name = name;
6618
}
6719

68-
@Override
69-
public boolean equals(Object o) {
70-
if (this == o) return true;
71-
if (o == null || getClass() != o.getClass()) return false;
72-
Employee employee = (Employee) o;
73-
return id == employee.id && Objects.equals(name, employee.name);
74-
}
75-
76-
@Override
77-
public int hashCode() {
78-
return 1;
79-
}
8020

81-
@Override
82-
public String toString() {
83-
return "Employee{" +
84-
"id=" + id +
85-
", name='" + name + '\'' +
86-
'}';
87-
}
88-
89-
public int getId() {
90-
return id;
91-
}
92-
93-
public void setId(int id) {
94-
this.id = id;
95-
}
96-
97-
public String getName() {
98-
return name;
99-
}
100-
101-
public void setName(String name) {
102-
this.name = name;
103-
}
10421
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;
2+
3+
public class Apple {
4+
5+
private int weight;
6+
private String color;
7+
8+
public Apple(int weight){
9+
this.weight = weight;
10+
}
11+
12+
public Apple(int weight,String color) {
13+
this.weight = weight;this.color = color;
14+
}
15+
16+
public int getWeight() {
17+
return weight;
18+
}
19+
20+
public void setWeight(int weight) {
21+
this.weight = weight;
22+
}
23+
24+
public String getColor() {
25+
return color;
26+
}
27+
28+
public void setColor(String color) {
29+
this.color = color;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "Apple{" +
35+
"weight=" + weight +
36+
", color='" + color + '\'' +
37+
'}';
38+
}
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;
2+
3+
public interface Colors {
4+
String GREEN ="GREEN";
5+
String RED ="RED";
6+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;
2+
3+
import java.io.File;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Optional;
8+
import java.util.function.Predicate;
9+
import java.util.stream.Collectors;
10+
11+
import static java.util.stream.Collectors.groupingBy;
12+
13+
public class Test {
14+
15+
public static void main(String[] args) {
16+
List<Apple> apples = getData();
17+
//filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
18+
//filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
19+
//filterGroupApples(apples,(Apple a) -> Colors.RED.equals(a.getColor())).forEach((i,list) ->{System.out.println(i+""+list);});
20+
//filterGroupApples(apples,(Apple a) -> {return true;}).forEach((i,list) ->{System.out.println(i+""+list);});
21+
minAppleByWeight(apples).ifPresent(System.out::println);
22+
maxAppleByWeight(apples).ifPresent(System.out::println);
23+
}
24+
25+
static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
26+
return inventory.stream().filter(p).collect(Collectors.toList());
27+
}
28+
29+
static Map<Integer, List<Apple>> filterGroupApples(List<Apple> inventory, Predicate<Apple> p) {
30+
return inventory.stream().filter(p).collect(groupingBy(Apple::getWeight));
31+
}
32+
33+
static Optional<Apple> minAppleByWeight(List<Apple> inventory) {
34+
return inventory.stream().min(Comparator.comparing(Apple::getWeight));
35+
}
36+
37+
static Optional<Apple> maxAppleByWeight(List<Apple> inventory) {
38+
return inventory.stream().max(Comparator.comparing(Apple::getWeight));
39+
}
40+
41+
private static List<Apple> getData() {
42+
Apple apple1 = new Apple(10,Colors.GREEN);
43+
Apple apple2 = new Apple(1,Colors.RED);
44+
Apple apple3 = new Apple(5,Colors.GREEN);
45+
Apple apple4 = new Apple(3,Colors.RED);
46+
Apple apple5 = new Apple(3,Colors.RED);
47+
Apple apple6 = new Apple(5,Colors.GREEN);
48+
List<Apple> apples = List.of(apple1,apple2,apple3,apple4,apple5,apple6);
49+
return apples;
50+
}
51+
52+
static File[] handlingFiles(){
53+
File[] hiddenFiles = new File(".").listFiles(File::isHidden);
54+
return hiddenFiles;
55+
}
56+
57+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.java15.example.java8inaction;
2+
3+
public class Test {
4+
5+
public static void main(String[] args){
6+
7+
int counter = 0;
8+
int temp;
9+
//9,3,6,5,7,2
10+
int[] arr ={10,9,3,6,5,7,2,13};
11+
for(int i= 0; i<arr.length;i++){
12+
for(int j=1;j<arr.length;j++){
13+
if(arr[i]<arr[j]){
14+
counter++;
15+
if(counter>2) {
16+
break;
17+
}
18+
}
19+
}
20+
21+
}
22+
System.out.println(counter);
23+
24+
25+
}
26+
27+
}

0 commit comments

Comments
 (0)