Skip to content

Commit

Permalink
Added Java8 Program
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAkashKumar committed Oct 21, 2020
1 parent 30ccebe commit 5746a43
Show file tree
Hide file tree
Showing 38 changed files with 1,409 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.akash.createUnmodifiable;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CreateUnmodifiableExample {

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

Map<String, Integer> map = new HashMap<String, Integer>();

map.put("Akash", 2);
map.put("Ravi", 4);
map.put("Roshan", 5);
map.put("Aditya", 7);
map.put("Anshit", 8);
map.put("Rahul", 9);


Map<String, Integer> unmodifiable = Collections.unmodifiableMap(map);
System.out.println(unmodifiable);

unmodifiable.put("Deepak", 45);

System.out.println(map);

}

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

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

public class FasterCollection {

public static void main(String[] args) {
Set<String> hashSet = new HashSet<>();
Set<String> treeSet = new TreeSet<>();
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();

List<String> base = new ArrayList<>();

for(int i = 0; i<5000000; i++){
if(i%100000==0) System.out.print(".");
base.add(UUID.randomUUID().toString());
}

System.out.println("\nBase size : " + base.size());
String item = base.get(25000);
System.out.println("SEARCHED ITEM : " + item);

hashSet.addAll(base);
treeSet.addAll(base);
arrayList.addAll(base);
linkedList.addAll(base);

long ms = System.currentTimeMillis();
System.out.println("hashSet.contains(item) ? " + (hashSet.contains(item)? "TRUE " : "FALSE") + (System.currentTimeMillis()-ms) + " ms");
System.out.println("treeSet.contains(item) ? " + (treeSet.contains(item)? "TRUE " : "FALSE") + (System.currentTimeMillis()-ms) + " ms");
System.out.println("arrayList.contains(item) ? " + (arrayList.contains(item)? "TRUE " : "FALSE") + (System.currentTimeMillis()-ms) + " ms");
System.out.println("linkedList.contains(item) ? " + (linkedList.contains(item)? "TRUE " : "FALSE") + (System.currentTimeMillis()-ms) + " ms");
}

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

import java.util.HashMap;
import java.util.Map;

public class MapSortedByValueProgram {

public static void main(String[] args) {

HashMap<String, Integer> map = new HashMap<>();
map.put("akash", 1);
map.put("Akhil", 3);
map.put("seema", 2);

sortByValue(map);
SortedByValueAnotherWay(map);
}

private static void SortedByValueAnotherWay(HashMap<String, Integer> map) {
// TODO
map.entrySet().stream()
.sorted((k1, k2) -> -k1.getValue().compareTo(k2.getValue()))
.forEach(k -> System.out.println(k.getKey() + ": " + k.getValue()));
}

private static void sortByValue(HashMap<String, Integer> map) {
// TODO
map.entrySet().stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.limit(10)
.forEach(System.out::println);
}

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

import java.util.ArrayList;
import java.util.Iterator;

public class ConcurrentModificationException {

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

private static void exampleOfModificationException() {
// TODO

ArrayList<String> list = new ArrayList<>();
list.add("akash");
list.add("ravi");
list.add("rahul");
list.add("anshit");
list.add("Guddu");

Iterator<String> itr = list.iterator();

while (itr.hasNext()) {
String value = itr.next();
System.out.println("List Value:" + value);
if (value.contains("ravi"))
list.remove(value);
System.out.println(list);
}
}
}
49 changes: 49 additions & 0 deletions Java8Program/src/com/akash/collableIn/CallableInterfaceDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.akash.collableIn;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class CallableTask implements Callable<Integer> {

@Override
public Integer call() throws Exception {

int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
return sum;
}

}

public class CallableInterfaceDemo {

public static void main(String[] args) {
FutureTask<Integer>[] futureList = new FutureTask[5];

for (int i = 0; i <= 4; i++) {
Callable<Integer> callable = new CallableTask();
futureList[i] = new FutureTask<Integer>(callable);
Thread t = new Thread(futureList[i]);
t.start();

}

for (int i = 0; i <= 4; i++) {
FutureTask<Integer> result = futureList[i];
try {
System.out.println("Future Task" + i + ":" + result.get());
} catch (InterruptedException e) {

e.printStackTrace();
} catch (ExecutionException e) {

e.printStackTrace();
}
}

}

}
53 changes: 53 additions & 0 deletions Java8Program/src/com/akash/collableIn/ExecutorDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.akash.collableIn;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class Worker implements Callable<Integer> {

@Override
public Integer call() throws Exception {

int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
return sum;
}

}

public class ExecutorDemo {

public static void main(String[] args) {
ExecutorService executors = Executors.newFixedThreadPool(4);
Future<Integer>[] futures = new Future[5];
Callable<Integer> w = new Worker();
try {
for (int i = 0; i < 5; i++) {
Future<Integer> future = executors.submit(w);
futures[i] = future;

}

for (int i = 0; i < futures.length; i++) {
try {
System.out.println("Result from Future " + i + ":" + futures[i].get());
} catch (InterruptedException e) {

e.printStackTrace();
} catch (ExecutionException e) {

e.printStackTrace();
}
}
} finally {
executors.shutdown();
}

}

}
23 changes: 23 additions & 0 deletions Java8Program/src/com/akash/java8new/DefaultMethodExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.akash.java8new;


public interface DefaultMethodExample {

String getBrand();

String speedUp();

String slowDown();

default String turnAlramOn() {
return "Akash Kumar";
}

default String turnOn() {
return "Hello I am";
}
default String onClick(String a) {
return "Ahh";
}

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

import java.util.StringJoiner;

public class DelimeterThroughStringJoiner {

public static void main(String[] args) {

StringJoiner joiner = new StringJoiner(",");
joiner.add("Akash");
joiner.add("Ravi");
joiner.add("Rahul");
joiner.add("Anshit");

System.out.println(joiner);

joiner = new StringJoiner("|");

joiner.add("Rahul");
joiner.add("Raju");
joiner.add("Peter");
joiner.add("Raheem");
System.out.println(joiner);


}

}
9 changes: 9 additions & 0 deletions Java8Program/src/com/akash/java8new/DemoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.akash.java8new;

public class DemoImpl implements ExampleStaticMethod{

public void print() {
ExampleStaticMethod.staticDemo();
}

}
9 changes: 9 additions & 0 deletions Java8Program/src/com/akash/java8new/ExampleStaticMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.akash.java8new;

public interface ExampleStaticMethod {

static void staticDemo() {
System.out.println("Static implementation in Interface");
}

}
25 changes: 25 additions & 0 deletions Java8Program/src/com/akash/java8new/RemoveIfMethodImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.akash.java8new;

import java.util.ArrayList;

public class RemoveIfMethodImpl {

public static void main(String[] args) {

ArrayList<Integer> arrayList = new ArrayList<>();

arrayList.add(52);
arrayList.add(23);
arrayList.add(32);
arrayList.add(45);
arrayList.add(63);

/*Added in list find out the divisible value by 3*/

arrayList.removeIf(n-> (n%3!= 0));

for (Integer number : arrayList) {
System.out.println("divisible Value by 3 :: "+ number);
}
}
}
Loading

0 comments on commit 5746a43

Please sign in to comment.