From 5746a434c4f0938ab72f0b64808fe5136c6545f1 Mon Sep 17 00:00:00 2001 From: MrAkashKumar Date: Wed, 21 Oct 2020 22:18:09 +0530 Subject: [PATCH] Added Java8 Program --- .../CreateUnmodifiableExample.java | 31 +++ .../FasterCollection.java | 42 +++++ .../MapSortedByValueProgram.java | 34 ++++ .../ConcurrentModificationException.java | 33 ++++ .../collableIn/CallableInterfaceDemo.java | 49 +++++ .../com/akash/collableIn/ExecutorDemo.java | 53 ++++++ .../akash/java8new/DefaultMethodExample.java | 23 +++ .../DelimeterThroughStringJoiner.java | 28 +++ .../src/com/akash/java8new/DemoImpl.java | 9 + .../akash/java8new/ExampleStaticMethod.java | 9 + .../akash/java8new/RemoveIfMethodImpl.java | 25 +++ .../src/com/akash/streams/DataUtility.java | 61 ++++++ .../src/com/akash/streams/IntegerUtility.java | 17 ++ .../akash/streams/StreamsCollectorsDemo.java | 40 ++++ .../com/akash/streams/StreamsFilterDemo.java | 40 ++++ .../akash/streams/StreamsGroupingByDemo.java | 41 ++++ .../src/com/akash/streams/StreamsMapDemo.java | 57 ++++++ .../com/akash/streams/StreamsReduceDemo.java | 69 +++++++ .../ConcordanceProgram.java | 36 ++++ .../latestProgram/ArmStrongNumberExample.java | 26 +++ .../akash/latestProgram/ArrayListToArray.java | 25 +++ .../latestProgram/CheckNumberInString.java | 16 ++ .../akash/latestProgram/CountOccurance.java | 176 ++++++++++++++++++ .../com/akash/latestProgram/Fibonanci.java | 18 ++ .../latestProgram/FindDuplicateChar.java | 26 +++ .../akash/latestProgram/InsertionSort.java | 27 +++ .../latestProgram/LinkedListReverse.java | 18 ++ .../latestProgram/MaxTwoNumbersInArrays.java | 25 +++ .../com/akash/latestProgram/MergeSort.java | 81 ++++++++ .../akash/latestProgram/MissingNumber.java | 26 +++ .../akash/latestProgram/MostRepeatedWord.java | 53 ++++++ .../com/akash/latestProgram/PrimeNumber.java | 25 +++ .../latestProgram/PrimeNumberDemoApp.java | 36 ++++ .../com/akash/latestProgram/PushZeroEnd.java | 28 +++ .../RemoveDuplicateCharacter.java | 42 +++++ .../ReverseStringORPalidrome.java | 34 ++++ .../src/com/akash/latestProgram/Runner.java | 5 + .../latestProgram/SwapingwithoutThird.java | 25 +++ 38 files changed, 1409 insertions(+) create mode 100644 CollectionProgram/src/com/akash/createUnmodifiable/CreateUnmodifiableExample.java create mode 100644 CollectionProgram/src/com/akash/fasterSearchCollection/FasterCollection.java create mode 100644 CollectionProgram/src/com/akash/mapSortedByValue/MapSortedByValueProgram.java create mode 100644 Exception/src/com/akash/concurrentModificationException/ConcurrentModificationException.java create mode 100644 Java8Program/src/com/akash/collableIn/CallableInterfaceDemo.java create mode 100644 Java8Program/src/com/akash/collableIn/ExecutorDemo.java create mode 100644 Java8Program/src/com/akash/java8new/DefaultMethodExample.java create mode 100644 Java8Program/src/com/akash/java8new/DelimeterThroughStringJoiner.java create mode 100644 Java8Program/src/com/akash/java8new/DemoImpl.java create mode 100644 Java8Program/src/com/akash/java8new/ExampleStaticMethod.java create mode 100644 Java8Program/src/com/akash/java8new/RemoveIfMethodImpl.java create mode 100644 Java8Program/src/com/akash/streams/DataUtility.java create mode 100644 Java8Program/src/com/akash/streams/IntegerUtility.java create mode 100644 Java8Program/src/com/akash/streams/StreamsCollectorsDemo.java create mode 100644 Java8Program/src/com/akash/streams/StreamsFilterDemo.java create mode 100644 Java8Program/src/com/akash/streams/StreamsGroupingByDemo.java create mode 100644 Java8Program/src/com/akash/streams/StreamsMapDemo.java create mode 100644 Java8Program/src/com/akash/streams/StreamsReduceDemo.java create mode 100644 Program/src/com/akash/ConcordanceProgram/ConcordanceProgram.java create mode 100644 Program/src/com/akash/latestProgram/ArmStrongNumberExample.java create mode 100644 Program/src/com/akash/latestProgram/ArrayListToArray.java create mode 100644 Program/src/com/akash/latestProgram/CheckNumberInString.java create mode 100644 Program/src/com/akash/latestProgram/CountOccurance.java create mode 100644 Program/src/com/akash/latestProgram/Fibonanci.java create mode 100644 Program/src/com/akash/latestProgram/FindDuplicateChar.java create mode 100644 Program/src/com/akash/latestProgram/InsertionSort.java create mode 100644 Program/src/com/akash/latestProgram/LinkedListReverse.java create mode 100644 Program/src/com/akash/latestProgram/MaxTwoNumbersInArrays.java create mode 100644 Program/src/com/akash/latestProgram/MergeSort.java create mode 100644 Program/src/com/akash/latestProgram/MissingNumber.java create mode 100644 Program/src/com/akash/latestProgram/MostRepeatedWord.java create mode 100644 Program/src/com/akash/latestProgram/PrimeNumber.java create mode 100644 Program/src/com/akash/latestProgram/PrimeNumberDemoApp.java create mode 100644 Program/src/com/akash/latestProgram/PushZeroEnd.java create mode 100644 Program/src/com/akash/latestProgram/RemoveDuplicateCharacter.java create mode 100644 Program/src/com/akash/latestProgram/ReverseStringORPalidrome.java create mode 100644 Program/src/com/akash/latestProgram/Runner.java create mode 100644 Program/src/com/akash/latestProgram/SwapingwithoutThird.java diff --git a/CollectionProgram/src/com/akash/createUnmodifiable/CreateUnmodifiableExample.java b/CollectionProgram/src/com/akash/createUnmodifiable/CreateUnmodifiableExample.java new file mode 100644 index 0000000..eebaeb4 --- /dev/null +++ b/CollectionProgram/src/com/akash/createUnmodifiable/CreateUnmodifiableExample.java @@ -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 map = new HashMap(); + + 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 unmodifiable = Collections.unmodifiableMap(map); + System.out.println(unmodifiable); + + unmodifiable.put("Deepak", 45); + + System.out.println(map); + + } + +} diff --git a/CollectionProgram/src/com/akash/fasterSearchCollection/FasterCollection.java b/CollectionProgram/src/com/akash/fasterSearchCollection/FasterCollection.java new file mode 100644 index 0000000..9ed05fc --- /dev/null +++ b/CollectionProgram/src/com/akash/fasterSearchCollection/FasterCollection.java @@ -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 hashSet = new HashSet<>(); + Set treeSet = new TreeSet<>(); + List arrayList = new ArrayList<>(); + List linkedList = new LinkedList<>(); + + List 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"); + } + +} diff --git a/CollectionProgram/src/com/akash/mapSortedByValue/MapSortedByValueProgram.java b/CollectionProgram/src/com/akash/mapSortedByValue/MapSortedByValueProgram.java new file mode 100644 index 0000000..d99cbcf --- /dev/null +++ b/CollectionProgram/src/com/akash/mapSortedByValue/MapSortedByValueProgram.java @@ -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 map = new HashMap<>(); + map.put("akash", 1); + map.put("Akhil", 3); + map.put("seema", 2); + + sortByValue(map); + SortedByValueAnotherWay(map); + } + + private static void SortedByValueAnotherWay(HashMap 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 map) { + // TODO + map.entrySet().stream() + .sorted(Map.Entry.comparingByValue().reversed()) + .limit(10) + .forEach(System.out::println); + } + +} diff --git a/Exception/src/com/akash/concurrentModificationException/ConcurrentModificationException.java b/Exception/src/com/akash/concurrentModificationException/ConcurrentModificationException.java new file mode 100644 index 0000000..d133fa9 --- /dev/null +++ b/Exception/src/com/akash/concurrentModificationException/ConcurrentModificationException.java @@ -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 list = new ArrayList<>(); + list.add("akash"); + list.add("ravi"); + list.add("rahul"); + list.add("anshit"); + list.add("Guddu"); + + Iterator 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); + } + } +} \ No newline at end of file diff --git a/Java8Program/src/com/akash/collableIn/CallableInterfaceDemo.java b/Java8Program/src/com/akash/collableIn/CallableInterfaceDemo.java new file mode 100644 index 0000000..c792556 --- /dev/null +++ b/Java8Program/src/com/akash/collableIn/CallableInterfaceDemo.java @@ -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 { + + @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[] futureList = new FutureTask[5]; + + for (int i = 0; i <= 4; i++) { + Callable callable = new CallableTask(); + futureList[i] = new FutureTask(callable); + Thread t = new Thread(futureList[i]); + t.start(); + + } + + for (int i = 0; i <= 4; i++) { + FutureTask result = futureList[i]; + try { + System.out.println("Future Task" + i + ":" + result.get()); + } catch (InterruptedException e) { + + e.printStackTrace(); + } catch (ExecutionException e) { + + e.printStackTrace(); + } + } + + } + +} diff --git a/Java8Program/src/com/akash/collableIn/ExecutorDemo.java b/Java8Program/src/com/akash/collableIn/ExecutorDemo.java new file mode 100644 index 0000000..a2f1061 --- /dev/null +++ b/Java8Program/src/com/akash/collableIn/ExecutorDemo.java @@ -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 { + + @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[] futures = new Future[5]; + Callable w = new Worker(); + try { + for (int i = 0; i < 5; i++) { + Future 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(); + } + + } + +} diff --git a/Java8Program/src/com/akash/java8new/DefaultMethodExample.java b/Java8Program/src/com/akash/java8new/DefaultMethodExample.java new file mode 100644 index 0000000..ea1498e --- /dev/null +++ b/Java8Program/src/com/akash/java8new/DefaultMethodExample.java @@ -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"; + } + +} diff --git a/Java8Program/src/com/akash/java8new/DelimeterThroughStringJoiner.java b/Java8Program/src/com/akash/java8new/DelimeterThroughStringJoiner.java new file mode 100644 index 0000000..55e6dd6 --- /dev/null +++ b/Java8Program/src/com/akash/java8new/DelimeterThroughStringJoiner.java @@ -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); + + + } + +} diff --git a/Java8Program/src/com/akash/java8new/DemoImpl.java b/Java8Program/src/com/akash/java8new/DemoImpl.java new file mode 100644 index 0000000..d485dc0 --- /dev/null +++ b/Java8Program/src/com/akash/java8new/DemoImpl.java @@ -0,0 +1,9 @@ +package com.akash.java8new; + +public class DemoImpl implements ExampleStaticMethod{ + + public void print() { + ExampleStaticMethod.staticDemo(); + } + +} diff --git a/Java8Program/src/com/akash/java8new/ExampleStaticMethod.java b/Java8Program/src/com/akash/java8new/ExampleStaticMethod.java new file mode 100644 index 0000000..4945657 --- /dev/null +++ b/Java8Program/src/com/akash/java8new/ExampleStaticMethod.java @@ -0,0 +1,9 @@ +package com.akash.java8new; + +public interface ExampleStaticMethod { + + static void staticDemo() { + System.out.println("Static implementation in Interface"); + } + +} diff --git a/Java8Program/src/com/akash/java8new/RemoveIfMethodImpl.java b/Java8Program/src/com/akash/java8new/RemoveIfMethodImpl.java new file mode 100644 index 0000000..f107bed --- /dev/null +++ b/Java8Program/src/com/akash/java8new/RemoveIfMethodImpl.java @@ -0,0 +1,25 @@ +package com.akash.java8new; + +import java.util.ArrayList; + +public class RemoveIfMethodImpl { + + public static void main(String[] args) { + + ArrayList 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); + } + } +} \ No newline at end of file diff --git a/Java8Program/src/com/akash/streams/DataUtility.java b/Java8Program/src/com/akash/streams/DataUtility.java new file mode 100644 index 0000000..707a387 --- /dev/null +++ b/Java8Program/src/com/akash/streams/DataUtility.java @@ -0,0 +1,61 @@ +package com.akash.streams; + +import java.util.ArrayList; +import java.util.List; + +public class DataUtility { + + //Utility class + private DataUtility(){ + + } + + public static List getStringList(){ + List stringList = new ArrayList<>(); + stringList.add("a"); + stringList.add("ABCD"); + stringList.add("BCDA"); + stringList.add("BCDAD"); + stringList.add("1234567"); + stringList.add("^%$%$%$%))"); + stringList.add("ABcd23232"); + stringList.add("abCD90%$"); + stringList.add("ZWERTD"); + stringList.add("123456"); + + return stringList; + } + + public static List getStringListWithDuplicates(){ + List stringList = new ArrayList<>(); + stringList.add("a"); + stringList.add(""); + stringList.add("ABCD"); + stringList.add("ABCD"); + stringList.add("BCDAD"); + stringList.add("1234567"); + stringList.add("1234567"); + stringList.add("ABcd23232"); + stringList.add("abCD90%$"); + stringList.add("ZWERTD"); + stringList.add("ZWERTD"); + + return stringList; + } + + public static List getIntegerList(){ + List integerList = new ArrayList<>(); + integerList.add(1); + integerList.add(99); + integerList.add(-450); + integerList.add(345); + integerList.add(25); + integerList.add(60); + integerList.add(-20); + return integerList; + } + + public static List getEmptyIntegerList(){ + return new ArrayList<>(); + } +} diff --git a/Java8Program/src/com/akash/streams/IntegerUtility.java b/Java8Program/src/com/akash/streams/IntegerUtility.java new file mode 100644 index 0000000..4b148c8 --- /dev/null +++ b/Java8Program/src/com/akash/streams/IntegerUtility.java @@ -0,0 +1,17 @@ +package com.akash.streams; + +public class IntegerUtility { + + //Utility Class + private IntegerUtility(){ + + } + + public static boolean isEven(int i){ + return i % 2 == 0; + } + + public static boolean isOdd(int i){ + return i % 2 != 0; + } +} diff --git a/Java8Program/src/com/akash/streams/StreamsCollectorsDemo.java b/Java8Program/src/com/akash/streams/StreamsCollectorsDemo.java new file mode 100644 index 0000000..015843e --- /dev/null +++ b/Java8Program/src/com/akash/streams/StreamsCollectorsDemo.java @@ -0,0 +1,40 @@ +package com.akash.streams; + + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class StreamsCollectorsDemo { + public static void main(String[] args){ + List stringList = getInput(); + + filterConvertListToSet(stringList); + + stringList = DataUtility.getStringList(); + + convertListToMap(stringList); + } + + public static Map convertListToMap(List stringList) { + System.out.println("Convert List to Map"); + Map stringMap = stringList.stream().collect(Collectors.toMap(v -> v + v.length(), v -> v)); + System.out.println(stringMap); + return stringMap; + } + + public static Set filterConvertListToSet(List stringList) { + System.out.println("Convert List to Set"); + Set stringSet = stringList.stream().filter(s -> s.length() > 0).collect(Collectors.toSet()); + System.out.println(stringSet); + return stringSet; + } + + private static List getInput() { + List stringList = DataUtility.getStringListWithDuplicates(); + System.out.println("Input"); + System.out.println(stringList); + return stringList; + } +} diff --git a/Java8Program/src/com/akash/streams/StreamsFilterDemo.java b/Java8Program/src/com/akash/streams/StreamsFilterDemo.java new file mode 100644 index 0000000..b3082f1 --- /dev/null +++ b/Java8Program/src/com/akash/streams/StreamsFilterDemo.java @@ -0,0 +1,40 @@ +package com.akash.streams; + + + +import java.util.List; +import java.util.stream.Collectors; + +public class StreamsFilterDemo { + public static void main(String[] args){ + List stringList = getInput(); + + greaterThan6(stringList); + + getEvenLengths(stringList); + + } + + public static List getEvenLengths(List stringList) { + System.out.println("Map each String to its length and filter only even lengths"); + List evenLengths = stringList.stream().map(String::length).filter(i -> i % 2 == 0).collect(Collectors.toList()); + System.out.println(evenLengths); + return evenLengths; + } + + public static List greaterThan6(List stringList) { + System.out.println("Get only Strings with length greater than 6"); + List greater6 = stringList.stream().filter(str -> str.length() > 6).collect(Collectors.toList()); + System.out.println(greater6); + return greater6; + } + + private static List getInput() { + List stringList = DataUtility.getStringList(); + System.out.println("Input"); + System.out.println(stringList); + return stringList; + } + + +} diff --git a/Java8Program/src/com/akash/streams/StreamsGroupingByDemo.java b/Java8Program/src/com/akash/streams/StreamsGroupingByDemo.java new file mode 100644 index 0000000..1d842f6 --- /dev/null +++ b/Java8Program/src/com/akash/streams/StreamsGroupingByDemo.java @@ -0,0 +1,41 @@ +package com.akash.streams; + + + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class StreamsGroupingByDemo { + + public static void main(String[] args){ + List stringList = getInput(); + + groupByLength(stringList); + + groupByLengthAndUpper(stringList); + } + + public static Map> groupByLengthAndUpper(List stringList) { + System.out.println("group by String lengths and convert to upper case"); + Map> groupByLengthEven = stringList.stream() + .collect(Collectors.groupingBy(String::length, + Collectors.mapping(String::toUpperCase, Collectors.toList()))); + System.out.println(groupByLengthEven); + return groupByLengthEven; + } + + public static Map> groupByLength(List stringList) { + System.out.println("group by String lengths"); + Map> groupByLength = stringList.stream().collect(Collectors.groupingBy(String::length)); + System.out.println(groupByLength); + return groupByLength; + } + + private static List getInput() { + List stringList = DataUtility.getStringList(); + System.out.println("Input"); + System.out.println(stringList); + return stringList; + } +} diff --git a/Java8Program/src/com/akash/streams/StreamsMapDemo.java b/Java8Program/src/com/akash/streams/StreamsMapDemo.java new file mode 100644 index 0000000..0b99f1a --- /dev/null +++ b/Java8Program/src/com/akash/streams/StreamsMapDemo.java @@ -0,0 +1,57 @@ +package com.akash.streams; + +import java.util.List; +import java.util.stream.Collectors; + + + +public class StreamsMapDemo { + + public static void main(String[] args) { + List stringList = getInput(); + + mapToLength(stringList); + + mapToUpperCase(stringList); + + sortAndMapToUpperCase(stringList); + + mapToUpperCaseAndSort(stringList); + + } + + private static List getInput() { + List stringList = DataUtility.getStringList(); + System.out.println("Input"); + System.out.println(stringList); + return stringList; + } + + public static List mapToUpperCaseAndSort(List stringList) { + System.out.println("Map to UpperCase and then Sort"); + List stringUpperSorted = stringList.stream().map(String::toUpperCase).sorted().collect(Collectors.toList()); + System.out.println(stringUpperSorted); + return stringUpperSorted; + } + + public static List sortAndMapToUpperCase(List stringList) { + System.out.println("Sort and Map to UpperCase"); + List stringSortedUpper = stringList.stream().sorted().map(String::toUpperCase).collect(Collectors.toList()); + System.out.println(stringSortedUpper); + return stringSortedUpper; + } + + public static List mapToUpperCase(List stringList) { + System.out.println("Map to UpperCase"); + List stringUpper = stringList.stream().map(String::toUpperCase).collect(Collectors.toList()); + System.out.println(stringUpper); + return stringUpper; + } + + public static List mapToLength(List stringList) { + System.out.println("Map to Lengths"); + List stringLengths = stringList.stream().map(String::length).collect(Collectors.toList()); + System.out.println(stringLengths); + return stringLengths; + } +} diff --git a/Java8Program/src/com/akash/streams/StreamsReduceDemo.java b/Java8Program/src/com/akash/streams/StreamsReduceDemo.java new file mode 100644 index 0000000..c9640c7 --- /dev/null +++ b/Java8Program/src/com/akash/streams/StreamsReduceDemo.java @@ -0,0 +1,69 @@ +package com.akash.streams; + +import java.util.List; + +public class StreamsReduceDemo { + public static void main(String[] args){ + List integerList = getInput(); + + getIntegerSum(integerList); + + List emptyIntegerList = getEmptyIntegerList(); + getEmptyIntegerListSum(emptyIntegerList); + + getEvenIntegerSum(integerList); + + getMinOddInteger(integerList); + + getAbsoluteSum(integerList); + + } + + public static int getAbsoluteSum(List integerList) { + System.out.println("Make all values positive and find the sum"); + int posSum = integerList.stream().map(Math::abs).reduce(Integer::sum).orElse(0); + System.out.println(posSum); + return posSum; + } + + public static int getMinOddInteger(List integerList) { + System.out.println("get Minimum odd Integer"); + int minOddInteger = integerList.stream().filter(IntegerUtility::isOdd).reduce(Integer::min).orElse(0); + System.out.println(minOddInteger); + return minOddInteger; + } + + public static int getEvenIntegerSum(List integerList) { + System.out.println("Get sum of all even integers"); + int evenSumVal = integerList.stream().filter(i -> i %2 == 0).reduce(Integer::sum).orElse(0); + System.out.println(evenSumVal); + return evenSumVal; + } + + public static int getEmptyIntegerListSum(List emptyIntegerList) { + System.out.println("Sum of Empty Integer List"); + int emptySumVal = emptyIntegerList.stream().reduce(Integer::sum).orElse(0); + System.out.println(emptySumVal); + return emptySumVal; + } + + + public static int getIntegerSum(List integerList) { + System.out.println("Get sum of all integers"); + int sumVal = integerList.stream().reduce(Integer::sum).orElse(0); + System.out.println(sumVal); + return sumVal; + } + + private static List getInput() { + List integerList = DataUtility.getIntegerList(); + System.out.println("Input"); + System.out.println(integerList); + return integerList; + } + + + private static List getEmptyIntegerList() { + return DataUtility.getEmptyIntegerList(); + } +} diff --git a/Program/src/com/akash/ConcordanceProgram/ConcordanceProgram.java b/Program/src/com/akash/ConcordanceProgram/ConcordanceProgram.java new file mode 100644 index 0000000..f95dd25 --- /dev/null +++ b/Program/src/com/akash/ConcordanceProgram/ConcordanceProgram.java @@ -0,0 +1,36 @@ +package com.akash.ConcordanceProgram; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class ConcordanceProgram { + + public static void main(String[] args) { + // TODO Auto-generated method stub + String str = "Hello World"; + Map> map = charConcordance(str); + System.out.println(map); + /* + * {r=[8], d=[10], e=[1], W=[6], H=[0], l=[2, 3, 9], o=[4, 7]} + * + * */ + } + private static Map> charConcordance(String str) { + // TODO + Map> map = new HashMap>(); + + for(int i = 0; i set = map.get(ch); + if(set == null) + set = new HashSet<>(); + set.add(i); + map.put(ch, set); + } + return map; + } +} diff --git a/Program/src/com/akash/latestProgram/ArmStrongNumberExample.java b/Program/src/com/akash/latestProgram/ArmStrongNumberExample.java new file mode 100644 index 0000000..ea1d8bf --- /dev/null +++ b/Program/src/com/akash/latestProgram/ArmStrongNumberExample.java @@ -0,0 +1,26 @@ + +package com.akash.latestProgram; + +public class ArmStrongNumberExample { + + public static void main(String[] args) { + + int c= 0, r, temp; + + int n = 153; + + temp = n; + + while(n>0) { + r = n%10; + c = r+c*c*c; + n = n/10; + } + + if(temp == c) { + System.out.println("Armstrong Number"); + }else { + System.out.println("Not Armstrong number"); + } + } +} \ No newline at end of file diff --git a/Program/src/com/akash/latestProgram/ArrayListToArray.java b/Program/src/com/akash/latestProgram/ArrayListToArray.java new file mode 100644 index 0000000..2d7e30b --- /dev/null +++ b/Program/src/com/akash/latestProgram/ArrayListToArray.java @@ -0,0 +1,25 @@ +package com.akash.latestProgram; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayListToArray { + + public static void main(String[] args) { + List list = new ArrayList<>(); + list.add("Apple"); + list.add("Banana"); + list.add("Mango"); + System.out.println("Converting ArrayList to Array: "); + String[] item = list.toArray(new String[list.size()]); + for (String s : item) { + System.out.println(s); + } + System.out.println("Converting Array to ArrayList: "); + List list2 = new ArrayList<>(); + list2 = Arrays.asList(item); + System.out.println(list2); + } + +} diff --git a/Program/src/com/akash/latestProgram/CheckNumberInString.java b/Program/src/com/akash/latestProgram/CheckNumberInString.java new file mode 100644 index 0000000..2267b4c --- /dev/null +++ b/Program/src/com/akash/latestProgram/CheckNumberInString.java @@ -0,0 +1,16 @@ +package com.akash.latestProgram; + +public class CheckNumberInString { + + public static void main(String[] args) { + String str = "Akash has been the text ever since the 1500s."; + Boolean flag = false; + for(int i = 0; i < str.length(); i++) { + flag = Character.isDigit(str.charAt(i)); + if(flag) { + System.out.println(str + "\n ==> contains number"); break; + } + } + } + +} diff --git a/Program/src/com/akash/latestProgram/CountOccurance.java b/Program/src/com/akash/latestProgram/CountOccurance.java new file mode 100644 index 0000000..2a1e965 --- /dev/null +++ b/Program/src/com/akash/latestProgram/CountOccurance.java @@ -0,0 +1,176 @@ +package com.akash.latestProgram; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class CountOccurance { + + public static void main(String[] args) { + String str = "Hello I am Akash Kumar, How are You, I am doing good"; + String word = "am"; + System.out.println(countReference(str, word)); + countOccurenceChar(str, str.length()); + countOccurUsingMap(str, str.length()); + countReference(str, word); + getOccuranceNum(); + getOccurenceNumUsingCollection(); + getOccuranceNumWithoutCollection(); + getOccuranceUsingForLoop(); + } + + private static void getOccuranceUsingForLoop() { + // TODO + int arr [] = {5,9,7,52,4,1,3,4,7,6,9,5,4,52}; + + int [] freq = new int[arr.length]; + + int visited = -1; + + for(int i = 0; i 0 && count[i] == 1){ + System.out.printf("%d occurs %d time\n",i, count[i]); + }else if(count[i] >=2){ + System.out.printf("%d occurs %d times\n",i, count[i]); + } + } + } + + private static void getOccurenceNumUsingCollection() { + // TODO Auto-generated method stub + + String str = "123456784250125326852006"; + + String s[] = str.split(""); + + Map map = new HashMap<>(); + + for(int i = 0; i enteryMap : map.entrySet()) { + System.out.println(enteryMap.getKey() +" MAP "+enteryMap.getValue()); + } + System.out.println("-----------------------------------------------------"); + } + + private static void countOccurUsingMap(String str, int length) { + // TODO + String arr[] = str.split(""); + Map map = new HashMap<>(); + for(int i= 0; i mapEntry : map.entrySet()) { + System.out.println(mapEntry.getKey()+" "+mapEntry.getValue()); + } + } + + private static void countOccurenceChar(String str, int length) { + // TODO + int count; + for(char c = 'A'; c<='z';c++) { + count = 0; + for(int i = 0; i0) { + System.out.println("Occurance : "+ c+ ": count "+ count); + } + } + } + + private static int countReference(String str, String word) { + // TODO + String a[] = str.split(" "); + + int count = 0; + for(int i = 0; i list = Arrays.asList(str); + Set set = new LinkedHashSet<>(); + set.addAll(list); + for(String get : set) { + System.out.println(get + " String Number "+Collections.frequency(list, get)); + } + + /*FOR Integer data */ + + Integer i[] = {1,5,8,6,0,5,8,1,6,7,9,8,6,9,1,3,5,7,9}; + + List list2 = Arrays.asList(i); + Set set2 = new LinkedHashSet<>(); + set2.addAll(list2); + for(Integer ik : set2) { + System.out.println(ik + " Number "+ Collections.frequency(list2, ik)); + } + + + } + +} \ No newline at end of file diff --git a/Program/src/com/akash/latestProgram/Fibonanci.java b/Program/src/com/akash/latestProgram/Fibonanci.java new file mode 100644 index 0000000..face732 --- /dev/null +++ b/Program/src/com/akash/latestProgram/Fibonanci.java @@ -0,0 +1,18 @@ +package com.akash.latestProgram; + +public class Fibonanci { + + public static void main(String[] args) { + int n1 = 0, n2 = 1, n3, count = 10; + + System.out.println("Printing Pattern : "+ n1+ " "+ n2); + + for(int i = 2; i h = new HashSet<>(); + for (int i = 0; i < str.length - 1; i++) { + char c = str[i]; + if (h.contains(c)) { + return c; + } else { + h.add(c); + } + } + return '\0'; + } + + public static void main(String[] args) { + String str = "PRADEEP"; + char[] arr = str.toCharArray(); + System.out.println(getDuplicate(arr)); + } + +} diff --git a/Program/src/com/akash/latestProgram/InsertionSort.java b/Program/src/com/akash/latestProgram/InsertionSort.java new file mode 100644 index 0000000..317568a --- /dev/null +++ b/Program/src/com/akash/latestProgram/InsertionSort.java @@ -0,0 +1,27 @@ +package com.akash.latestProgram; + +public class InsertionSort { + + public static void main(String[] args) { + + int arr[] = {1, 5, 4, 8,3, 6, 9}; + insertedSort(arr); + for(int i = 0; i0 && arr[j-1]>temp) { + arr[j] = arr[j-1]; + j = j-1; + } + arr[j] = temp; + } + } +} \ No newline at end of file diff --git a/Program/src/com/akash/latestProgram/LinkedListReverse.java b/Program/src/com/akash/latestProgram/LinkedListReverse.java new file mode 100644 index 0000000..3f6dc0c --- /dev/null +++ b/Program/src/com/akash/latestProgram/LinkedListReverse.java @@ -0,0 +1,18 @@ +package com.akash.latestProgram; + +public class LinkedListReverse { + + + /*public ListNode reverseList(ListNode head) { + ListNode prev = null; + ListNode curr = head; + while (curr != null) { + ListNode nextTemp = curr.next; + curr.next = prev; + prev = curr; + curr = nextTemp; + } + return prev;*/ + + +} diff --git a/Program/src/com/akash/latestProgram/MaxTwoNumbersInArrays.java b/Program/src/com/akash/latestProgram/MaxTwoNumbersInArrays.java new file mode 100644 index 0000000..56e51a2 --- /dev/null +++ b/Program/src/com/akash/latestProgram/MaxTwoNumbersInArrays.java @@ -0,0 +1,25 @@ +package com.akash.latestProgram; + +public class MaxTwoNumbersInArrays { + + public void getMaxNumber(int[] arr) { + int maxOne = 0; + int maxTwo = 0; + for (int n : arr) { + if (maxOne < n) { + maxTwo = maxOne; + maxOne = n; + } else if (maxTwo < n) { + maxTwo = n; + } + } + System.out.println("First Max Number: " + maxOne); + System.out.println("Second Max Number: " + maxTwo); + } + + public static void main(String[] args) { + int arr[] = { 5, 34, 78, 2, 45, 1, 99, 23 }; + MaxTwoNumbersInArrays obj = new MaxTwoNumbersInArrays(); + obj.getMaxNumber(arr); + } +} \ No newline at end of file diff --git a/Program/src/com/akash/latestProgram/MergeSort.java b/Program/src/com/akash/latestProgram/MergeSort.java new file mode 100644 index 0000000..cc41dba --- /dev/null +++ b/Program/src/com/akash/latestProgram/MergeSort.java @@ -0,0 +1,81 @@ +package com.akash.latestProgram; + +public class MergeSort { + + int[] array; + int[] tempMergeArr; + int length; + + /*it is basically work divide and conquer + * Recursive method + * */ + public static void main(String[] args) { + // TODO + + int arr[] = {15, 12, 96, 2, 46, 71, 63, 13, 6}; + + MergeSort mergeSort = new MergeSort(); + mergeSort.sort(arr); + + for(int i : arr) { + System.out.print(i+" "); + } + } + + private void sort(int[] arr) { + // TODO + this.array = arr; + this.length = arr.length; + this.tempMergeArr = new int[length]; + divideArray(0, length-1); + } + + private void divideArray(int lowerIndex, int higherIndex) { + // TODO + + if(lowerIndex < higherIndex) { + + int middle = lowerIndex +(higherIndex-lowerIndex)/2; + + /*it will sort the left side of an array*/ + divideArray(lowerIndex, middle); + + /*it will sort the right side of an array*/ + divideArray(middle+1, higherIndex); + + mergeArray(lowerIndex, middle, higherIndex); + } + } + + private void mergeArray(int lowerIndex, int middle, int higherIndex) { + // TODO + + for(int i = lowerIndex; i<=higherIndex; i++) { + tempMergeArr[i] = array[i]; + } + + int i = lowerIndex; + int j = middle+1; + int k = lowerIndex; + + while(i<=middle && j<=higherIndex) { + + if(tempMergeArr[i] <= tempMergeArr[j]) { + + array[k] = tempMergeArr[i]; + i++; + }else { + array[k] = tempMergeArr[j]; + j++; + } + k++; + } + while(i<=middle) { + array[k] = tempMergeArr[i]; + k++; + i++; + } + + } + // To do +} \ No newline at end of file diff --git a/Program/src/com/akash/latestProgram/MissingNumber.java b/Program/src/com/akash/latestProgram/MissingNumber.java new file mode 100644 index 0000000..e6fa69f --- /dev/null +++ b/Program/src/com/akash/latestProgram/MissingNumber.java @@ -0,0 +1,26 @@ +package com.akash.latestProgram; + +public class MissingNumber { + + public static void main(String[] args) { + + + int a [] = {2, 3,4, 5, 1, 9,6, 8}; + int getValue = getMissingNumbr(a, a.length); + + System.out.println(getValue); + } + + private static int getMissingNumbr(int[] a, int length) { + // TODO + + int total = (length+1)*(length+2)/2; + + for(int i =0; i words = new ArrayList(); + + // Opens file in read mode + FileReader file; + try { + file = new FileReader("file.txt"); + BufferedReader br = new BufferedReader(file); + + // Reads each line + try { + while ((line = br.readLine()) != null) { + String string[] = line.toLowerCase().split("([,.\\s]+)"); + for (String s : string) { + words.add(s); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + // Determine the most repeated word in a file + for (int i = 0; i < words.size(); i++) { + count = 1; + for (int j = i + 1; j < words.size(); j++) { + if (words.get(i).equals(words.get(j))) { + count++; + } + } + if (count > maxCount) { + maxCount = count; + word = words.get(i); + } + } + System.out.println("Most Repeated words: " + word); + } + +} diff --git a/Program/src/com/akash/latestProgram/PrimeNumber.java b/Program/src/com/akash/latestProgram/PrimeNumber.java new file mode 100644 index 0000000..30fc8f8 --- /dev/null +++ b/Program/src/com/akash/latestProgram/PrimeNumber.java @@ -0,0 +1,25 @@ +package com.akash.latestProgram; + +import java.util.Scanner; + +public class PrimeNumber { + + // % is remainder representation + // / is division representation + public static boolean isPrime(int n) { + for(int i = 2; i linked = new LinkedHashSet<>(); + for(int i = 0; i=0; i-- ) { + reverse = reverse + original.charAt(i); + } + System.out.println("reverse String is a : "+ reverse ); + scanner.close(); + } + + private static void reverseNumberPalidrome() { + // TODO + int n = 121; int reverse = 0, remainder, originalNumber; + originalNumber = n; + while(n!= 0) { + remainder = n%10; + reverse = reverse*10+remainder; + n = n/10; + } + System.out.println(reverse +" : "+ " Reverese number of "+ originalNumber); + } +} \ No newline at end of file diff --git a/Program/src/com/akash/latestProgram/Runner.java b/Program/src/com/akash/latestProgram/Runner.java new file mode 100644 index 0000000..c371c70 --- /dev/null +++ b/Program/src/com/akash/latestProgram/Runner.java @@ -0,0 +1,5 @@ +package com.akash.latestProgram; + +public class Runner { + +} diff --git a/Program/src/com/akash/latestProgram/SwapingwithoutThird.java b/Program/src/com/akash/latestProgram/SwapingwithoutThird.java new file mode 100644 index 0000000..326b67d --- /dev/null +++ b/Program/src/com/akash/latestProgram/SwapingwithoutThird.java @@ -0,0 +1,25 @@ +package com.akash.latestProgram; + +import java.util.Scanner; + +public class SwapingwithoutThird { + + public static void main(String[] args) { + int x, y; + System.out.println("Enter the X and Y"); + Scanner scanner = new Scanner(System.in); + x = scanner.nextInt(); + y = scanner.nextInt(); + + System.out.println("Befor swaping : "+ x+""+y); + + x = x+y; + y = x-y; + x = x-y; + + System.out.println("After swapping : "+ x +""+y); + + scanner.close(); + } + +}