Skip to content

Commit

Permalink
added java8 collection
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAkashKumar committed Nov 18, 2020
1 parent 83d4964 commit 0a271a8
Show file tree
Hide file tree
Showing 18 changed files with 529 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Java8/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions Java8/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Java8</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions Java8/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
26 changes: 26 additions & 0 deletions Java8/src/com/akash/filterJava8/FilterFromArrayUsingJava8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.akash.filterJava8;

import java.util.Arrays;
import java.util.function.IntConsumer;

public class FilterFromArrayUsingJava8 {

public static void main(String[] args) {

int arr [] = {4,6,8,2,9,75,36,41};

Arrays.stream(arr).filter(e-> e>9).forEach(i -> System.out.println(i));

System.out.println("Same result trying anoher ways");

getFilterForLessThan(arr);

}

private static void getFilterForLessThan(int[] arr) {
// TODO Auto-generated method stub
IntConsumer icons = i -> System.out.print(i + " ");
Arrays.stream(arr).filter(e-> e>9).forEach(icons);
}

}
69 changes: 69 additions & 0 deletions Java8/src/com/akash/lambdaExpressionProgram/ProgramExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.akash.lambdaExpressionProgram;

import java.util.ArrayList;
import java.util.List;

public class ProgramExample {

static int c = 0;

public static void main(String[] args) {
getLogicalQuestion();
getLogicalQuestion2();
getLogicalQuestion1();
getLogicalQuestion3();
getLogicalQuestion4();
}

private static void getLogicalQuestion4() {
// TODO Auto-generated method stub

List list = new ArrayList();

System.out.println(list.get(0) instanceof Object);
System.out.println(list.get(1) instanceof Object);
String str = "strawbe";

str.substring(2, 5);
System.out.println(str);

for(int i = 0; i<10; i++) {
i+=1;
System.out.println("Helllo");
}
}
private static void getLogicalQuestion3() {
// TODO Auto-generated method stub
String ess = "hello ";
String newMess = ess.substring(6, 12);
/*+ess.substring(12, 6);*/ // Error
System.out.println(newMess);
}
private static void getLogicalQuestion1() {
// TODO Auto-generated method stub
if(c <3) {
c++;
main(null);
}else {
return;
}
System.out.println(c);
}
private static void getLogicalQuestion2() {
// TODO Auto-generated method stub
int x = 5;
x= 10;
System.out.println(x);
}

private static void getLogicalQuestion() {
// TODO Auto-generated method stub
List<Boolean> list = new ArrayList();

list.add(true);
list.add(Boolean.parseBoolean("FalSe"));
list.add(Boolean.TRUE);
System.out.println(list.size());
System.out.println(list.get(1) instanceof Boolean);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.akash.lambdaExpressionProgram;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SortStringArrayUsingExpression {

public static void main(String[] args) {
sortStringUsingLambda();
sortIntegerUsingLambda();
}

private static void sortIntegerUsingLambda() {
// TODO
String str[] = {"abc", "def", "cde", "ghf", "bcd"};

List<String> list = Arrays.asList(str);
Collections.sort(list, (a, b) -> a.compareTo(b));

System.out.println("forSorted String :: "+list);

}


private static void sortStringUsingLambda() {
// TODO
Integer i [] = {1, 3, 2, 6, 7,5,4,9,14,12};

/*Ascending order*/
List<Integer> listForIntegr = Arrays.asList(i);
Collections.sort(listForIntegr, (a, b) -> a.compareTo(b));

System.out.println("Total Sorted Array "+listForIntegr);

/*find the first biggest value from Array*/
System.out.println("1st Biggest value from Array : "+listForIntegr.get(i.length-1));

/*find the 2nd biggest value from array*/
System.out.println("2nd Biggest value : "+ listForIntegr.get(i.length-2));

/*find the smallest value from array*/
System.out.println("Smallest Value : "+ listForIntegr.get(0));
}
}
16 changes: 16 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/AddSumNumberAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.akash.rangeJava8Program;

import java.util.stream.IntStream;

public class AddSumNumberAll {

public static void main(String[] args) {
// TODO

int reduceNumber = IntStream.of(4,5,6,1,3,7).reduce(10, (x,y) -> x+y);

System.out.println(reduceNumber);

}

}
27 changes: 27 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/AllEvenNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.akash.rangeJava8Program;

import java.util.stream.IntStream;

public class AllEvenNumber {

/*Find all even number between 1 to 15*/

public static void main(String[] args) {

getRangeOfEvenNum();
getGivenNumberRangeOfEven();
/* IntPredicate is a functional interface */

}

private static void getGivenNumberRangeOfEven() {
// TODO
IntStream.of(2,8,9,7,12,16,19).filter(n -> n%2 ==0).forEach(System.out::println);
}

private static void getRangeOfEvenNum() {
// TODO
IntStream.range(1, 15).filter(n -> n%2==0).forEach(System.out:: print);
}

}
26 changes: 26 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/AverageNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.akash.rangeJava8Program;

import java.util.OptionalDouble;
import java.util.stream.IntStream;

public class AverageNumber {

public static void main(String[] args) {
getAverageNumber();
getAllAverageGivenNumber();
}

private static void getAllAverageGivenNumber() {
// TODO
OptionalDouble doubleValue = IntStream.range(1, 5).average();
System.out.println("Range Value : "+ doubleValue.getAsDouble());

}

private static void getAverageNumber() {
// TODO
OptionalDouble doubleValue = IntStream.of(5,6,8).average();
System.out.println("Given DoubleValue Number "+doubleValue.getAsDouble());
}

}
22 changes: 22 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/CountAllNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.akash.rangeJava8Program;

import java.util.stream.IntStream;

public class CountAllNumber {

public static void main(String[] args) {
// TODO
System.out.println("Count number of elements");
getAllNumberCount();
}

private static void getAllNumberCount() {
// TODO

long count = IntStream.of(5,3,8,6,2).count();

System.out.println("Count :: "+count);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.akash.rangeJava8Program;

import java.util.stream.IntStream;

public class CountNumberRecordGreaterthanTwo {

public static void main(String[] args) {
getGreaterNumberCount();
}

private static void getGreaterNumberCount() {
// TODO
long countValue = IntStream.of(4,5,6,8,3, 1).filter(n -> n>2).count();
System.out.println("All count value :: "+countValue);
}

}
22 changes: 22 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/MaximunNumOfAllNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.akash.rangeJava8Program;

import java.util.OptionalInt;
import java.util.stream.IntStream;

public class MaximunNumOfAllNumber {

public static void main(String[] args) {
// TODO
System.out.println(" Find maximum of all numbers in IntStream");
maximumNumberOfAllNumber();
}

private static void maximumNumberOfAllNumber() {
// TODO
OptionalInt max = IntStream.of(9,5,4,3,2).max();

System.out.println(max.getAsInt());

}

}
20 changes: 20 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/MinimumAllNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.akash.rangeJava8Program;

import java.util.OptionalInt;
import java.util.stream.IntStream;

public class MinimumAllNumber {

public static void main(String[] args) {
// TODO
getAllMinimumNumber();
}

private static void getAllMinimumNumber() {
// TODO
OptionalInt intValue = IntStream.of(5,6,8,3,12,15).min();

System.out.println("Minimum value :: "+intValue.getAsInt());
}

}
29 changes: 29 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/NumberGreaterThanTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.akash.rangeJava8Program;

import java.util.stream.IntStream;

public class NumberGreaterThanTwo {

public static void main(String[] args) {

System.out.println("find all number greater than 2 ");
getAllNumberByGreaterThanTwo();
System.out.println("----Find all even numbers greater than 2 -----");
getAllNumGreaterThanTwoEvenNum();
}

private static void getAllNumGreaterThanTwoEvenNum() {
// TODO

IntStream.of(2,6,8,7,2,12,15,36,13).filter(n -> n>2).filter(n-> n%2==0).forEach(System.out::println);

}

private static void getAllNumberByGreaterThanTwo() {
// TODO

IntStream.of(1, 5, 6, 8, 6, 9).filter(n -> n>2).forEach(System.out::println);

}

}
27 changes: 27 additions & 0 deletions Java8/src/com/akash/rangeJava8Program/SumAllRangeNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.akash.rangeJava8Program;

import java.util.stream.IntStream;

public class SumAllRangeNumber {

public static void main(String[] args) {
// TODO
System.out.println("Find sum of all between 1 to 5");
getSumValueOfGivenNum();
System.out.println("fina sum of all even number between 1 to 10");
getSumAllEvenNumber();
}

private static void getSumAllEvenNumber() {
// TODO
int sumValue = IntStream.of(1,6,8,4,12,13,14,17,19,37).filter(e -> e%2==0).sum();
System.out.println("All Even number value : "+sumValue);
}

private static void getSumValueOfGivenNum() {
// TODO
int sum = IntStream.of(1,6,8,5,36,12).sum();
System.out.println(sum);
}

}
Loading

0 comments on commit 0a271a8

Please sign in to comment.