numbers = Arrays.asList(8, 9, 10);
13 |
14 | Integer sum = numbers.stream().reduce(sumOf1to7, (currentSum, nextValue) -> currentSum + nextValue);
15 |
16 | System.out.println("sum of first 10 numbers is " + sum);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/keywords/finals/FInalVaraibleExamples.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.keywords.finals;
2 |
3 | public class FInalVaraibleExamples {
4 |
5 | public static void main(String[] args) {
6 |
7 | // normal local variable
8 | int i = 10;
9 | i = 20;
10 | System.out.println("local i = " + i);
11 |
12 | // final local variable with value reassignment
13 | final int j = 10;
14 |
15 | // uncomment the below line to see compile time error
16 | //j = 20; // compile time error
17 | System.out.println("final j = " + j);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/escape/newline/AddNewLineExample6.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.escape.newline;
2 |
3 | public class AddNewLineExample6 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // html break tag
8 | String tag1 = "hello
";
9 | String tag2 = "world
";
10 |
11 | String tag3 = tag1 + "" + tag2;
12 |
13 | // using java \n
14 | String tag4 = tag1 + "\n" + tag3;
15 |
16 | // using unicodes
17 |
18 | String tag5 = "This is paragraph text and
woops there is a new line.
";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/escape/newline/AddNewLineExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.escape.newline;
2 |
3 | public class AddNewLineExample4 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // Using java api system class getProperty() method
8 | String line1 = "Hello engeers";
9 | String line2 = "hope you are staying safe";
10 |
11 | String line3 = line1 + System.getProperty("line.separator") + line2;
12 |
13 | System.out.println("print newline with System.getPropertyr() method");
14 | System.out.println(line3);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/w3schools/programs/ascii/StringToAsciiByteCasting.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.w3schools.programs.ascii;
2 |
3 | public class StringToAsciiByteCasting {
4 |
5 | public static void main(String[] args) {
6 |
7 | String input = "byte casting";
8 |
9 | char[] array = input.toCharArray();
10 |
11 | StringBuffer sb = new StringBuffer();
12 | for (char ch: array) {
13 | sb.append((byte)ch).append(" ");
14 |
15 | }
16 |
17 | System.out.println("byte casting output : "+sb);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/collections/hashmap/print/HashMapPrintExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.collections.hashmap.print;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.Set;
6 |
7 | public class HashMapPrintExample3 {
8 |
9 | public static void main(String[] args) {
10 |
11 | Map map = new HashMap<>();
12 |
13 | map.put(1, "one");
14 | map.put(2, "two");
15 | map.put(3, "three");
16 |
17 | Set> set = map.entrySet();
18 |
19 | set.forEach(System.out::println);
20 |
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/split/StringSplitExample7.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings.split;
2 |
3 | public class StringSplitExample7 {
4 |
5 | public static void main(String[] args) {
6 | String multipleDelimiters = "hello,welcome to;java$program#to.com";
7 |
8 | String[] spaceBasedSplitArray = multipleDelimiters.split("[, ;$#]");
9 |
10 | System.out.println("returned array size : "+spaceBasedSplitArray.length);
11 | for(String value : spaceBasedSplitArray){
12 | System.out.println(value);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/.idea/$PRODUCT_WORKSPACE_FILE$:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | 1.8.0_252
8 |
9 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/localdate/LocalDateCreation.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.localdate;
2 |
3 | import java.time.LocalDate;
4 |
5 | public class LocalDateCreation {
6 |
7 | public static void main(String[] args) {
8 |
9 | // Dates creation with now() and of() methods.
10 | LocalDate currentDate = LocalDate.now();
11 | LocalDate futureDate = LocalDate.of(2025, 10, 10);
12 |
13 | // printing the dates.
14 | System.out.println("Current date : " + currentDate);
15 | System.out.println("Future date : " + futureDate);
16 |
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringHashcodeExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | public class StringHashcodeExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String str = "hello string fan";
8 |
9 | int hashValue = str.hashCode();
10 |
11 | System.out.println("hash code of string : "+hashValue);
12 |
13 | String newString = "this is a new string";
14 |
15 | int newStrHashValue = newString.hashCode();
16 |
17 | System.out.println("New string hash value : "+newStrHashValue);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/design/patterns/strategy1/context/OperationContext.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.design.patterns.strategy1.context;
2 |
3 | import com.javaprogramto.design.patterns.strategy1.strategy.IBaseOperation;
4 |
5 | public class OperationContext {
6 |
7 | private IBaseOperation iBaseOperation;
8 |
9 | public OperationContext(IBaseOperation iBaseOperation) {
10 | this.iBaseOperation = iBaseOperation;
11 | }
12 |
13 | public int executeStrategy(int value1, int value2) {
14 | return iBaseOperation.doOperation(value1, value2);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/exception/IllegalArgumentException/IllegalArgumentExceptionExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.exception.IllegalArgumentException;
2 |
3 | import com.javaprogramto.java8.compare.Employee;
4 |
5 | public class IllegalArgumentExceptionExample3 {
6 |
7 | public static void main(String[] args) {
8 |
9 | // Example 3
10 | Employee employeeRequest = new Employee(222, "Ram", 17);
11 |
12 | if (employeeRequest.getAge() < 18 || employeeRequest.getAge() > 65) {
13 | throw new IllegalArgumentException("Invalid age for the emp req");
14 | }
15 |
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/printwriter/PrintWriterExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.printwriter;
2 |
3 | import java.io.PrintWriter;
4 |
5 | public class PrintWriterExample1 {
6 |
7 | public static void main(String[] args) {
8 |
9 | PrintWriter writer = new PrintWriter(System.out);
10 | writer.write("First line of console ");
11 | writer.write("Second line of console");
12 |
13 | writer.flush();
14 |
15 | boolean hasError = writer.checkError();
16 | System.out.println(" any error in processing - "+hasError);
17 |
18 | writer.close();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/empty/ReturnEmptyArrayExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.empty;
2 |
3 | public class ReturnEmptyArrayExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | if (true) {
8 | int[] array1 = emptyArray();
9 | System.out.println("Length of int array 1 : " + array1.length);
10 | }
11 |
12 | if( 40 % 10 == 0) {
13 | int[] array2 = emptyArray();
14 | System.out.println("Length of int array 2 : " + array2.length);
15 | }
16 |
17 | }
18 |
19 | public static int[] emptyArray() {
20 |
21 | return new int[0];
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/datatypes/diff/FloatVsDoubleDataLoss.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.datatypes.diff;
2 |
3 | public class FloatVsDoubleDataLoss {
4 |
5 | public static void main(String[] args) {
6 |
7 | // double examples
8 | double d = 1.2345678912345678;
9 |
10 | float f = (float) d;
11 |
12 | System.out.println("float value after double to float conversion (data loss) - " + f);
13 |
14 | // float examples
15 | float f1 = 1.1111111f;
16 |
17 | double d1 = f1;
18 |
19 | System.out.println("\n" + "double value after float to double conversion - " + d1);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/exception/classcaseexception/ClassCastExceptionExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.exception.classcaseexception;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class ClassCastExceptionExample2 {
7 |
8 | public static void main(String[] args) {
9 |
10 | List list = new ArrayList<>();
11 |
12 | list.add(10);
13 | list.add(20);
14 | list.add("String");
15 | list.add(30);
16 |
17 | for (int i = 0; i < list.size(); i++) {
18 | Integer value = (Integer) list.get(i);
19 | System.out.println(value);
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/anymatch/AnyMatchOnEmptyStream.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.anymatch;
2 |
3 | import java.util.stream.Stream;
4 |
5 | public class AnyMatchOnEmptyStream {
6 |
7 | public static void main(String[] args) {
8 |
9 | Stream emptyStream = Stream.empty();
10 |
11 | boolean found = emptyStream.anyMatch(str -> str.length() > 0);
12 |
13 | if(found){
14 | System.out.println("Stream has values");
15 | } else {
16 | System.out.println("Stream is empty and no values");
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/chars/AddCharToStringExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.chars;
2 |
3 | public class AddCharToStringExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | // adding the char at the end of string
8 | String addingChar = "hello" + 'A';
9 |
10 | // adding multiple chars at the end of the string
11 | String addingChars = "hello" + 'A' + ',' + 'B';
12 |
13 | System.out.println("adding single char to the string at the end - "+addingChar);
14 | System.out.println("adding miltiple chars to the string at the end - "+addingChars);
15 |
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/foreach/StreamTakeWhile.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.foreach;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | public class StreamTakeWhile {
7 |
8 | public static void main(String[] args) {
9 |
10 | List list = Arrays.asList("one", "two", "three", "seven", "nine");
11 |
12 | // list.stream().takeWhile(value -> value.length() > 3).forEach(value -> {
13 | // if (value.length() > 3) {
14 | // System.out.println(value);
15 | // }
16 | // });
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/count/CountNoCharExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.count;
2 |
3 | public class CountNoCharExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String givenString = "hello world";
8 |
9 | char givenChar = 'l';
10 |
11 | int strLength = givenString.length();
12 | int count = 0;
13 |
14 | for (int i = 0; i < strLength; i++) {
15 |
16 | if(givenChar == givenString.charAt(i)) {
17 | count++;
18 | }
19 | }
20 |
21 | System.out.println("no of occurrences of char 'l' is "+count);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/optionals/returntype/Employee.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.optionals.returntype;
2 |
3 | import java.io.Serializable;
4 | import java.util.Optional;
5 |
6 | public class Employee implements Serializable {
7 |
8 | private int id;
9 |
10 | private Optional salary;
11 |
12 | // setters and getters
13 |
14 | private String name;
15 |
16 | public Optional getName() {
17 | return Optional.ofNullable(name);
18 | }
19 |
20 | public void setName(String name) {
21 | this.name = name;
22 | }
23 |
24 |
25 |
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/scanner/close/ScannerCloseExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.scanner.close;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ScannerCloseExample1 {
6 |
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 | System.out.println("Enter your birth year");
11 | int year = scanner.nextInt();
12 |
13 | System.out.println("Enter your age ");
14 | int age = scanner.nextInt();
15 |
16 | scanner.close();
17 |
18 | System.out.println("Given age and year are (" + age + "," + year + ")");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/remove/character/StringRemoveCharacterExample5.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.remove.character;
2 |
3 | import org.apache.commons.lang3.StringUtils;
4 |
5 | public class StringRemoveCharacterExample5 {
6 |
7 | public static void main(String[] args) {
8 |
9 | String input = "hello +world ++";
10 | char removalCh = '+';
11 |
12 | String output = StringUtils.remove(input, removalCh);
13 |
14 | System.out.println("Input string 1 : " + input);
15 | System.out.println("Output string after removing the + symbol : " + output);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/multidimensional/NestedArraysExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.multidimensional;
2 |
3 | import org.apache.commons.lang3.ArrayUtils;
4 |
5 | public class NestedArraysExample1 {
6 |
7 | public static void main(String[] args) {
8 |
9 | int[] array1 = {};
10 |
11 | int[] array2 = new int[0];
12 |
13 | int[] array3 = ArrayUtils.EMPTY_INT_ARRAY;
14 |
15 | System.out.println("array1 length is "+array1.length);
16 | System.out.println("array2 length is "+array2.length);
17 | System.out.println("array3 length is "+array3.length);
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/timestamp/CurrentTimestampExample7.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.timestamp;
2 |
3 | import java.time.LocalDateTime;
4 | import java.time.format.DateTimeFormatter;
5 |
6 | public class CurrentTimestampExample7 {
7 |
8 | public static void main(String[] args) {
9 |
10 | LocalDateTime localDateTime = LocalDateTime.now();
11 |
12 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
13 |
14 | String timestamp = formatter.format(localDateTime);
15 |
16 | System.out.println("timestamp now : " + timestamp);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/optional/OptionalIfPresentExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.optional;
2 |
3 | import java.util.Optional;
4 |
5 | public class OptionalIfPresentExample {
6 | public static void main(String[] args) {
7 |
8 | // String value optional
9 | Optional string = Optional.ofNullable("12345");
10 |
11 | // converting string to number
12 | Optional numberOptional = string.map(value -> Integer.parseInt(value));
13 |
14 | // printing the number using ifPresent()
15 | numberOptional.ifPresent(newValue -> System.out.println(newValue));
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/large/Largest3NumbersOptimizedExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.large;
2 |
3 | public class Largest3NumbersOptimizedExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | int number1 = 1000;
8 | int number2 = 2000;
9 | int number3 = 300;
10 |
11 | int max = number1;
12 |
13 | if (number1 >= number2 && number1 >= number3) {
14 | max = number1;
15 | } else if (number2 >= number3) {
16 | max = number2;
17 | } else {
18 | max = number3;
19 | }
20 |
21 | System.out.println(max + " is biggest number");
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/count/CountNoCharExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.count;
2 |
3 | public class CountNoCharExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String givenString = "hello world";
8 |
9 | char givenChar = 'l';
10 |
11 | int strOriginalLength = givenString.length();
12 |
13 | String replacedString = givenString.replace(String.valueOf(givenChar), "");
14 |
15 | int count = strOriginalLength - replacedString.length();
16 |
17 | System.out.println("no of occurrences of char 'l' is "+count);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/collections/hashmap/print/HashMapPrintExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.collections.hashmap.print;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.Set;
6 |
7 | public class HashMapPrintExample4 {
8 |
9 | public static void main(String[] args) {
10 |
11 | Map map = new HashMap<>();
12 |
13 | map.put(1, "one");
14 | map.put(2, "two");
15 | map.put(3, "three");
16 |
17 | Set keys = map.keySet();
18 |
19 | for(Integer key : keys) {
20 | System.out.println(key+" - "+map.get(key));
21 | }
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/exception/inputmismatchexception/InputMismatchExceptionExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.exception.inputmismatchexception;
2 |
3 | import java.util.Scanner;
4 |
5 | public class InputMismatchExceptionExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | Scanner scanner = new Scanner(System.in);
10 |
11 | System.out.println("Please enter a number");
12 | int number = scanner.nextInt();
13 |
14 | if (number % 2 == 0) {
15 | System.out.println(number + " is even");
16 | } else {
17 | System.out.println(number + " is odd");
18 | }
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/foreach/NormalForEachListExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.foreach;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class NormalForEachListExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | List list = new ArrayList<>();
11 |
12 | list.add("one");
13 | list.add("two");
14 | list.add("three");
15 | list.add("four");
16 | list.add("five");
17 |
18 | for (String s : list) {
19 | System.out.println(s);
20 | }
21 |
22 | }
23 |
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/integer/IntegerToUnsignedLongExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.integer;
2 |
3 | public class IntegerToUnsignedLongExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | Long long1 = Integer.toUnsignedLong(10);
8 | System.out.println("unsigned long value for int 10 is " + long1);
9 |
10 | Long long2 = Integer.toUnsignedLong(4294967);
11 | System.out.println("unsigned long value for int 4294967 is " + long2);
12 |
13 | Long long3 = Integer.toUnsignedLong(100000);
14 | System.out.println("unsigned long value for int 10 is " + long3);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/foreach/StreamForBreak.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.foreach;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | public class StreamForBreak {
7 |
8 | public static void main(String[] args) {
9 |
10 | List list = Arrays.asList("one", "two", "three", "seven", "nine", "ten");
11 |
12 | for (int i = 0; i < list.size(); i++) {
13 | if (list.get(i).length() > 3) {
14 |
15 | break;
16 | }
17 | System.out.println(list.get(i));
18 |
19 | }
20 |
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/printwriter/newline/PrintWriterNewLine.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.printwriter.newline;
2 |
3 | import java.io.PrintWriter;
4 |
5 | public class PrintWriterNewLine {
6 |
7 | public static void main(String[] args) {
8 |
9 | PrintWriter printWriter = new PrintWriter(System.out);
10 |
11 | printWriter.write("this is the first line");
12 | printWriter.write("\n");
13 | printWriter.write("this is the second line");
14 | printWriter.write("\n");
15 | printWriter.write("third line");
16 |
17 | printWriter.flush();
18 |
19 | printWriter.close();
20 |
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/conversion/sql/SQLDateToLocalDateExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.conversion.sql;
2 |
3 | import java.sql.Date;
4 | import java.time.LocalDate;
5 |
6 | public class SQLDateToLocalDateExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | // Creating sql date
11 | Date sqlDate = Date.valueOf("2020-12-31");
12 |
13 | // converting sql date to localdate using toLocalDate() method.
14 | LocalDate localDate1 = sqlDate.toLocalDate();
15 |
16 | // printing the local date.
17 | System.out.println("Local Date 1 : "+localDate1);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/conversion/totimestamp/LocalDateToTimeStampExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.conversion.totimestamp;
2 |
3 | import java.sql.Timestamp;
4 | import java.time.LocalDate;
5 |
6 | public class LocalDateToTimeStampExample {
7 |
8 | public static void main(String[] args) {
9 |
10 |
11 | LocalDate localDate = LocalDate.now();
12 |
13 | System.out.println("Local Date - "+localDate);
14 |
15 | Timestamp timestamp = Timestamp.valueOf(localDate.atStartOfDay());
16 |
17 | System.out.println("Timestamp from localdate is "+timestamp);
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/large/Largest3NumbersExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.large;
2 |
3 | public class Largest3NumbersExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | int number1 = 10;
8 | int number2 = 20;
9 | int number3 = 30;
10 |
11 | if (number1 >= number2 && number1 >= number3) {
12 | System.out.println(number1 + " is the biggest");
13 | } else if (number2 >= number1 && number2 >= number3) {
14 | System.out.println(number2 + " is the biggest");
15 | } else {
16 | System.out.println(number3 + " is the biggest");
17 | }
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringCodepoinExampleToConvertStream.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | import java.util.stream.IntStream;
4 |
5 | public class StringCodepoinExampleToConvertStream {
6 |
7 | public static void main(String[] args) {
8 |
9 | String str = "Code Points as Stream";
10 |
11 | System.out.println("Input string value : "+str);
12 |
13 | IntStream intStream = str.codePoints();
14 |
15 | System.out.println("Printing each char from string as ASCII value");
16 | intStream.forEach(value -> System.out.print(value+" "));
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/booleanarray/BooleanArrayExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.booleanarray;
2 |
3 | public class BooleanArrayExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // way 1
8 | boolean[] array1 = { false, true, true };
9 |
10 | // way 2
11 | boolean[] array2 = new boolean[4];
12 |
13 | array2[0] = true;
14 | array2[1] = false;
15 | array2[2] = true;
16 | array2[3] = false;
17 |
18 | // way 3
19 | boolean[] array3 = new boolean[5];
20 |
21 | for (int i = 0; i < array3.length; i++) {
22 | array3[i] = i % 2 == 0;
23 | }
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/chars/AddCharToStringExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.chars;
2 |
3 | public class AddCharToStringExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // adding the char at the start of string
8 | String addingCharAtStart = 'A' + " hello";
9 |
10 | // adding multiple chars at the start of the string
11 | String addingCharsAtStart = 'A' + 'B'+ " hello";
12 |
13 | System.out.println("adding single char to the start of the string - "+addingCharAtStart);
14 | System.out.println("adding miltiple chars to the start of the string - "+addingCharsAtStart);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/exception/FinallyBlockAfterReturn.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.exception;
2 |
3 | public class FinallyBlockAfterReturn {
4 |
5 | public static void main(String[] args) {
6 |
7 | int returnedStatus = process();
8 | System.out.println("Returned value : "+returnedStatus);
9 |
10 | }
11 | public static int process() {
12 |
13 | try {
14 | return 1;
15 | } catch (Exception e) {
16 | return 2;
17 | } finally {
18 | System.out.println("Executing Finally block after return statement.");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/directory/CurrentDirectoryPathGetExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.directory;
2 |
3 | import java.nio.file.Path;
4 | import java.nio.file.Paths;
5 |
6 | public class CurrentDirectoryPathGetExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | // Current location from path.
11 | Path currentPath = Paths.get("");
12 |
13 | // getting the location from Path.toAbsolutePath()
14 | String currentLocation = currentPath.toAbsolutePath().toString();
15 |
16 |
17 | System.out.println("Current working directoruy : " + currentLocation);
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/instant/InstantParseExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.instant;
2 |
3 | import java.time.Instant;
4 |
5 | public class InstantParseExample {
6 | public static void main(String[] args) {
7 |
8 | // Creating instant object from parse()
9 | Instant instantFromStr1 = Instant.parse("2020-11-29T13:35:30.01Z");
10 | System.out.println("Instant 1 from string using parse() : " + instantFromStr1);
11 |
12 | Instant instantFromStr2 = Instant.parse("2020-12-29T13:35:31.00Z");
13 | System.out.println("Instant 2 from string using parse() : " + instantFromStr2);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/intstream/toList/IntStreamToListExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.intstream.toList;
2 |
3 | import java.util.LinkedList;
4 | import java.util.List;
5 | import java.util.stream.Collectors;
6 | import java.util.stream.IntStream;
7 | import java.util.stream.Stream;
8 |
9 | public class IntStreamToListExample2 {
10 |
11 | public static void main(String[] args) {
12 |
13 | IntStream evenIntStream = IntStream.iterate(0, i -> i + 2);
14 |
15 | List list = evenIntStream.limit(100).boxed().toList();
16 |
17 | System.out.println("list size : " + list.size());
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/optional/OptionalOrElse.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.optional;
2 |
3 | import java.util.Optional;
4 |
5 | public class OptionalOrElse {
6 | public static void main(String[] args) {
7 |
8 | Optional o1 = Optional.ofNullable(null);
9 |
10 | String value = o1.orElse("Default One");
11 |
12 | System.out.println("Fetching the value from orElse() : "+value);
13 |
14 | Optional intOptional = Optional.empty();
15 |
16 | int defaultValue = intOptional.orElse(15);
17 |
18 | System.out.println("Int default value :"+defaultValue);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/optional/OptionalflatmapExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.optional;
2 |
3 | import java.util.Optional;
4 |
5 | public class OptionalflatmapExample {
6 | public static void main(String[] args) {
7 |
8 | Optional optional1 = Optional.of("Hello Java 8 Optional");
9 | Optional> optional2 = Optional.of(optional1);
10 |
11 | System.out.println("Optional2 value : " + optional2);
12 |
13 | Optional output = optional2.flatMap(value -> value.map(String::toLowerCase));
14 |
15 | System.out.println("output value : " + output);
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/filter/FilterExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.filter;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.stream.Collectors;
6 |
7 | public class FilterExample {
8 |
9 | public static void main(String[] args) {
10 |
11 | List numbers = Arrays.asList(10,50,30,18,35);
12 |
13 | List evenNumbers = numbers.stream().filter(number -> number % 2 == 0).collect(Collectors.toList());
14 |
15 | System.out.println("After applying the filter function : "+evenNumbers);
16 |
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/booleanarray/BooleanArrayExample5.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.booleanarray;
2 |
3 | public class BooleanArrayExample5 {
4 |
5 | public static void main(String[] args) {
6 |
7 | boolean[] array5 = new boolean[5];
8 |
9 | System.out.println("Default value of array5 at index 1 is - " + array5[1]);
10 |
11 | System.out.println("Setting to true for all indexes of array 5 using simple for loop");
12 | for (int i = 0; i < array5.length; i++) {
13 | array5[i] = true;
14 | }
15 | System.out.println("Now Default value of array5 at index 1 is - " + array5[1]);
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/conversion/CalenderToLocalDateExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.conversion;
2 |
3 | import java.time.LocalDate;
4 | import java.time.ZoneId;
5 | import java.util.Calendar;
6 | import java.util.Date;
7 |
8 | public class CalenderToLocalDateExample {
9 |
10 | public static void main(String[] args) {
11 |
12 | Calendar cal = Calendar.getInstance();
13 | Date input = cal.getTime();
14 | LocalDate la = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
15 |
16 | System.out.println("Calender to LocalDate : "+la);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/bytearray/Base64Decode.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.bytearray;
2 |
3 | import java.nio.charset.CharacterCodingException;
4 | import java.util.Base64;
5 |
6 | public class Base64Decode {
7 |
8 | public static void main(String[] args) throws CharacterCodingException {
9 |
10 | // creating string
11 | String inputStr = "javaprogrmto.com जावा";
12 |
13 | // java 8 string to byte[]
14 | byte[] array = Base64.getDecoder().decode(inputStr);
15 |
16 | // printing byte array
17 | for (int j = 0; j < array.length; j++) {
18 | System.out.print(" " + array[j]);
19 | }
20 |
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/reduce/StreamReduceExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.reduce;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Optional;
6 |
7 | public class StreamReduceExample {
8 |
9 | public static void main(String[] args) {
10 |
11 | List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
12 |
13 | Optional sumOptional = numbers.stream().reduce((currentSum, nextValue) -> currentSum + nextValue);
14 |
15 | if(sumOptional.isPresent()) {
16 | System.out.println("sum of first 7 numbers is "+sumOptional.get());
17 | }
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringCodepointCountExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | public class StringCodepointCountExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String heartString = "\uD83D\uDC93\uD83D\uDC93\uD83D\uDC93\uD83D\uDC93";
8 |
9 | System.out.println("Input string value : "+heartString);
10 |
11 | // Getting count of code points for index 0 to 4
12 | int countpointsCount = heartString.codePointCount(0, heartString.length());
13 |
14 | System.out.println("Code points count for indexs range (0, 4) : "+countpointsCount);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/returns/ArrayReturnExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.returns;
2 |
3 | import java.util.Arrays;
4 |
5 | public class ArrayReturnExample4 {
6 |
7 | public static void main(String[] args) {
8 |
9 | int[][] multiArray = getMultiArray();
10 |
11 | System.out.println("Returned Multi dimentional array values : ");
12 | for(int[] a : multiArray) {
13 | System.out.println( Arrays.toString(a));
14 | }
15 |
16 | }
17 |
18 | // returning multi dimens
19 | public static int[][] getMultiArray() {
20 |
21 | int[][] multi = { { 1, 2, 3 }, { 4, 5, 6 } };
22 | return multi;
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/exception/inputmismatchexception/InputMismatchExceptionExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.exception.inputmismatchexception;
2 |
3 | import java.math.BigDecimal;
4 | import java.util.Scanner;
5 |
6 | public class InputMismatchExceptionExample2 {
7 |
8 | public static void main(String[] args) {
9 |
10 | Scanner scanner = new Scanner(System.in);
11 |
12 | System.out.println("Enter BigDecimal array size: ");
13 | int size = scanner.nextInt();
14 |
15 | BigDecimal[] array = new BigDecimal[size];
16 |
17 | for (int i = 0; i < size; i++) {
18 | array[i] = scanner.nextBigDecimal();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/collectors/tolist/StreamCollectToListExample5.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.collectors.tolist;
2 |
3 | import java.util.List;
4 | import java.util.stream.Collectors;
5 | import java.util.stream.IntStream;
6 |
7 | public class StreamCollectToListExample5 {
8 |
9 | public static void main(String[] args) {
10 |
11 | IntStream infiniteStream = IntStream.iterate(1000, i -> i + 1000);
12 |
13 | List numbersList = infiniteStream.limit(5).boxed().collect(Collectors.toList());
14 |
15 | System.out.println("infinite stream collect to ArrayList output : " + numbersList);
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/conversion/totimestamp/LocalDateTimeToTimeStampExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.conversion.totimestamp;
2 |
3 | import java.sql.Timestamp;
4 | import java.time.LocalDateTime;
5 |
6 | public class LocalDateTimeToTimeStampExample {
7 |
8 | public static void main(String[] args) {
9 |
10 |
11 | LocalDateTime localDateTime = LocalDateTime.now();
12 |
13 | System.out.println("Local Date Time - "+localDateTime);
14 |
15 | Timestamp timestamp = Timestamp.valueOf(localDateTime);
16 |
17 | System.out.println("Timestamp from localdatetime is "+timestamp);
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/maths/LCMOfLargeNumbers.java:
--------------------------------------------------------------------------------
1 |
2 | package com.javaprogramto.programs.maths;
3 |
4 | import java.math.BigInteger;
5 |
6 | public class LCMOfLargeNumbers {
7 |
8 | public static void main(String[] args) {
9 |
10 | BigInteger large1 = new BigInteger("123456789123456789123456");
11 | BigInteger large2 = new BigInteger("987654321987654321987654");
12 |
13 | BigInteger gcd = large1.gcd(large2);
14 |
15 | BigInteger multiply = large1.multiply(large2);
16 |
17 | BigInteger lcm = multiply.divide(gcd);
18 |
19 | System.out.println("Final LCM output: "+lcm.toString());
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/todouble/StringToDoubleExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.todouble;
2 |
3 | public class StringToDoubleExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // double value in string format
8 | String to_double = "1234";
9 |
10 | double doubleValue = Double.parseDouble(to_double);
11 |
12 | System.out.println("String to Double value : " + doubleValue);
13 |
14 | to_double = "100d";
15 |
16 | doubleValue = Double.parseDouble(to_double);
17 |
18 | System.out.println("String to Double value with d in the string : " + doubleValue);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringCodepointCountException.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | public class StringCodepointCountException {
4 |
5 | public static void main(String[] args) {
6 |
7 | String heartString = "\uD83D\uDC93\uD83D\uDC93\uD83D\uDC93\uD83D\uDC93";
8 |
9 | System.out.println("Input string value : "+heartString);
10 |
11 | // Getting count of code points for index 0 to 4
12 | int countpointsCount = heartString.codePointCount(-10, heartString.length());
13 |
14 | System.out.println("Code points count for indexs range (0, 4) : "+countpointsCount);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringStartsWithExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | public class StringStartsWithExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String s1 = "hello world";
8 |
9 | boolean isStringStartsWithHello = s1.startsWith("hello");
10 |
11 | System.out.println("String starts with hello or not : "+isStringStartsWithHello);
12 | System.out.println("Phone number starts with +91 "+"+91 12345678".startsWith("+678"));
13 |
14 | System.out.println("javaprogramto.com starts with java : "+"javaprogramto.com".startsWith("java"));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/getdatetime/DateExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.getdatetime;
2 |
3 | import java.util.Date;
4 |
5 | public class DateExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | // using new Date()
10 | Date currentDateTime = new Date();
11 | System.out.println("Current date time using Date() : " + currentDateTime);
12 |
13 | // using System.currentTimeMillis()
14 | long milliSeconds = System.currentTimeMillis();
15 | currentDateTime = new Date(milliSeconds);
16 | System.out.println("Current date time using System.currentTimeMillis() : " + currentDateTime);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/collections/hashmap/print/HashMapPrintExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.collections.hashmap.print;
2 |
3 | import java.util.HashMap;
4 | import java.util.Iterator;
5 | import java.util.Map;
6 |
7 | public class HashMapPrintExample2 {
8 |
9 | public static void main(String[] args) {
10 |
11 | Map map = new HashMap<>();
12 |
13 | map.put(1, "one");
14 | map.put(2, "two");
15 | map.put(3, "three");
16 |
17 | Iterator> it = map.entrySet().iterator();
18 |
19 | it.forEachRemaining( entry -> System.out.println(entry.getKey() + " - "+entry.getValue()));
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/skip/Skip10ValuesExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.skip;
2 |
3 | import java.util.List;
4 | import java.util.stream.Collectors;
5 | import java.util.stream.Stream;
6 |
7 | public class Skip10ValuesExample {
8 | public static void main(String[] args) {
9 |
10 | Stream numnerSeries = Stream.iterate(1, i -> i+ 1);
11 |
12 | List next20Numbers = numnerSeries.skip(10).limit(20).collect(Collectors.toList());
13 |
14 | System.out.println("20 Numbers after skipping first 10 numbers");
15 | next20Numbers.forEach(number -> System.out.println(number));
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/permutations/StringPermutationsExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.permutations;
2 |
3 | public class StringPermutationsExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | stringPermuteAndPrint("", "ABC");
8 | }
9 |
10 | private static void stringPermuteAndPrint(String prefix, String str) {
11 | int n = str.length();
12 | if (n == 0) {
13 | System.out.print(prefix + " ");
14 | } else {
15 | for (int i = 0; i < n; i++) {
16 | stringPermuteAndPrint(prefix + str.charAt(i), str.substring(i + 1, n) + str.substring(0, i));
17 | }
18 | }
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringCodepointBeforeExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | public class StringCodepointBeforeExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String str = "JavaProgramTo.com";
8 |
9 | System.out.println("Input string value : "+str);
10 |
11 | int codepointBefore = str.codePointBefore(2);
12 | System.out.println("Code point for before index 2 : "+codepointBefore);
13 |
14 | codepointBefore = str.codePointBefore(str.length());
15 | System.out.println("Code point for before index "+str.length()+": "+codepointBefore);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/collections/hashmap/print/HashMapPrintExample5.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.collections.hashmap.print;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.Set;
6 | import java.util.function.BiConsumer;
7 |
8 | public class HashMapPrintExample5 {
9 |
10 | public static void main(String[] args) {
11 |
12 | Map map = new HashMap<>();
13 |
14 | map.put(1, "one");
15 | map.put(2, "two");
16 | map.put(3, "three");
17 |
18 | BiConsumer printBiConsumer = (key, value) -> System.out.println(key+" - "+value);
19 |
20 | map.forEach(printBiConsumer);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/infinite/StreamIterateExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.infinite;
2 |
3 | import java.util.List;
4 | import java.util.stream.Collectors;
5 | import java.util.stream.Stream;
6 |
7 | public class StreamIterateExample {
8 |
9 | public static void main(String[] args) {
10 |
11 | // Creating a infinite Stream
12 | Stream even10Numbers = Stream.iterate(0, i -> i +2);
13 |
14 | List first10Numbers = even10Numbers.limit(10).collect(Collectors.toList());
15 |
16 | System.out.println("even10Numbers with limit 10 : "+first10Numbers);
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/maptolist/MapKeysToListExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.maptolist;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | public class MapKeysToListExample {
9 |
10 | public static void main(String[] args) {
11 |
12 | Map idNames = new HashMap<>();
13 |
14 | idNames.put(100, "modi");
15 | idNames.put(101, "trump");
16 | idNames.put(102, "putin");
17 |
18 | List ids =new ArrayList<>(idNames.keySet());
19 |
20 | ids.forEach(id -> System.out.println(id));
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/compare/StringDoesnotEqual.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings.compare;
2 |
3 | public class StringDoesnotEqual {
4 |
5 | public static void main(String[] args) {
6 |
7 | String status = getValidationStatus(10);
8 | System.out.println("status : "+status);
9 |
10 | if (status.intern() != "Failure") {
11 | System.out.println("Valid age");
12 | } else {
13 | System.out.println("Invalid age");
14 | }
15 |
16 | }
17 |
18 | public static String getValidationStatus(int age) {
19 |
20 | if (age < 18) {
21 | return new String("Failure");
22 | }
23 |
24 | return new String ("Success");
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/anymatch/AnyMatchEvenExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.anymatch;
2 |
3 | import java.util.stream.Stream;
4 |
5 | public class AnyMatchEvenExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | Integer[] numbers = {1, 3, 5, 7, 8, 9};
10 |
11 | Stream intStream = Stream.of(numbers);
12 |
13 | boolean matchFound = intStream.anyMatch(number -> number % 2 == 0);
14 |
15 | if(matchFound){
16 | System.out.println("int array has even number");
17 | } else {
18 | System.out.println("int array has no even number");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/comparator/reverse/ComparatorReverse2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.comparator.reverse;
2 |
3 | import java.util.Arrays;
4 | import java.util.Comparator;
5 | import java.util.List;
6 | import java.util.stream.Collectors;
7 |
8 | public class ComparatorReverse2 {
9 |
10 | public static void main(String[] args) {
11 |
12 | List list = Arrays.asList("1", "3", "2");
13 |
14 | System.out.println("Original list - " + list);
15 |
16 | List sortedList = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
17 |
18 | System.out.println("Sorted list - " + sortedList);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/optional/OptionalOrElseGet.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.optional;
2 |
3 | import java.util.Optional;
4 |
5 | public class OptionalOrElseGet {
6 | public static void main(String[] args) {
7 |
8 | Optional o1 = Optional.ofNullable(null);
9 |
10 | String value = o1.orElseGet(() -> "Default One from supplier");
11 |
12 | System.out.println("Fetching the value from orElseGet() : " + value);
13 |
14 | Optional intOptional = Optional.of(134);
15 |
16 | int defaultValue = intOptional.orElseGet(() -> 15);
17 |
18 | System.out.println("orElseGet Int default value :" + defaultValue);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/skip/SkipNegativeIndexExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.skip;
2 |
3 | import java.util.List;
4 | import java.util.stream.Collectors;
5 | import java.util.stream.Stream;
6 |
7 | public class SkipNegativeIndexExample {
8 | public static void main(String[] args) {
9 |
10 | Stream numbers = Stream.of(1, 2, 3);
11 |
12 | List expectingError = numbers
13 | .skip(-25)
14 | .collect(Collectors.toList());
15 |
16 | System.out.println("skipping first 5 even Numbers");
17 | expectingError.forEach(number -> System.out.println(number));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/todouble/StringToDoubleExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.todouble;
2 |
3 | public class StringToDoubleExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // double value in string format
8 | String to_double = "1234";
9 |
10 | Double doubleValue = Double.valueOf(to_double);
11 |
12 | System.out.println("String to Double value with valueOf() : " + doubleValue);
13 |
14 | to_double = "100d";
15 |
16 | doubleValue = Double.valueOf(to_double);
17 |
18 | System.out.println("String to Double value with d in the string using valueOf() : " + doubleValue);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/empty/ReturnEmptyArrayExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.empty;
2 |
3 | public class ReturnEmptyArrayExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | if (true) {
8 | int[] array1 = emptyArrayWIthCurlyBraces();
9 | System.out.println("Length of int array 1 : " + array1.length);
10 | }
11 |
12 | if (100 > 0) {
13 | int[] array2 = emptyArrayWIthCurlyBraces();
14 | System.out.println("Length of int array 2 : " + array2.length);
15 | }
16 |
17 | }
18 |
19 | public static int[] emptyArrayWIthCurlyBraces() {
20 |
21 | int[] emptyArray = {};
22 |
23 | return emptyArray;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/collections/set/add/HashSetAddExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.collections.set.add;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 |
6 | public class HashSetAddExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | Set set = new HashSet<>();
11 |
12 | boolean isAdded = set.add(10);
13 | System.out.println("Is 10 added? " + isAdded);
14 |
15 | isAdded = set.add(10);
16 | System.out.println("Is 10 added again? " + isAdded);
17 |
18 | isAdded = set.add(20);
19 | System.out.println("Is 20 added? " + isAdded);
20 |
21 | System.out.println("Set values : "+set);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/localdate/StringToLocalDateExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.localdate;
2 |
3 | import java.time.LocalDate;
4 |
5 | // String to LocalDate in java 8
6 | public class StringToLocalDateExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | // Example 1
11 | String dateInStr = "2021-01-01";
12 | LocalDate date1 = LocalDate.parse(dateInStr);
13 | System.out.println("String to LocalDate : " + date1);
14 |
15 | // Example 2
16 | String dateInStr2 = "2025-11-30";
17 | LocalDate date2 = LocalDate.parse(dateInStr2);
18 | System.out.println("String to LocalDate : " + date2);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/parallel/streams/ParallelStreamCreation.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.parallel.streams;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.concurrent.ForkJoinPool;
6 | import java.util.stream.Stream;
7 |
8 | public class ParallelStreamCreation {
9 |
10 | public static void main(String[] args) {
11 |
12 | List intList = Arrays.asList(10, 20, 30, 40, 50);
13 |
14 | Stream parallelStream = intList.parallelStream();
15 |
16 | ForkJoinPool.commonPool();
17 |
18 | parallelStream.forEach(value -> System.out.println(value));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/maths/QuotientRemainderExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.maths;
2 |
3 | public class QuotientRemainderExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | // Creating int variable for number 1
8 | int number1 = 200;
9 |
10 | // Creating int variable for number 2
11 | int number2 = 50;
12 |
13 | // Calculating the quotient
14 | int quotient = number1 / number2;
15 |
16 | // Calculating the remainder
17 | int remainder = number1 % number2;
18 |
19 | System.out.println("Quotient value: "+quotient);
20 | System.out.println("Remainder value with zero: "+remainder);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/numbers/math/MathAbsoluteExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.numbers.math;
2 |
3 | public class MathAbsoluteExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // for integer primitive values
8 | int i = -100;
9 | int absValueofI = Math.abs(i);
10 |
11 | System.out.println("abs value of " + i + " is " + absValueofI);
12 |
13 | i = 200;
14 | absValueofI = Math.abs(i);
15 |
16 | System.out.println("abs value of " + i + " is " + absValueofI);
17 |
18 | i = -0;
19 | absValueofI = Math.abs(i);
20 |
21 | System.out.println("abs value of " + i + " is " + absValueofI);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/numbers/math/MathAbsoluteExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.numbers.math;
2 |
3 | public class MathAbsoluteExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // for long primitive values
8 | long l = -100;
9 | long absValueofL = Math.abs(l);
10 |
11 | System.out.println("abs value of " + l + " is " + absValueofL);
12 |
13 | l = 200;
14 | absValueofL = Math.abs(l);
15 |
16 | System.out.println("abs value of " + l + " is " + absValueofL);
17 |
18 | l = -0;
19 | absValueofL = Math.abs(l);
20 |
21 | System.out.println("abs value of " + l + " is " + absValueofL);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/.settings/.jsdtscope:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/find/LinearSearch.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.find;
2 |
3 | public class LinearSearch {
4 |
5 | public static void main(String[] args) {
6 |
7 | int[] array = {6, 7, 10, 5, 70, 9};
8 |
9 | boolean found = false;
10 |
11 | int valueToBeFound = 5;
12 |
13 | for(int value : array){
14 |
15 | if( value == valueToBeFound){
16 | found = true;
17 | }
18 | }
19 |
20 | if(found){
21 | System.out.println("value 5 is present");
22 | } else {
23 | System.out.println("value 5 is not present");
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/exit/JavaProgramExitExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.exit;
2 |
3 | public class JavaProgramExitExample4 {
4 |
5 | public static void main(String[] args) {
6 |
7 | System.out.println("first line from main method");
8 |
9 | int number = 10;
10 | String value = getValue(number);
11 | System.out.println(value);
12 | System.out.println("last line from main method");
13 | }
14 |
15 | private static String getValue(int number) {
16 |
17 | System.out.println("statement 1");
18 |
19 | if(number % 2 == 0) {
20 | return "even number";
21 | }
22 | else {
23 | return "odd number";
24 | }
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/todouble/StringToDoubleExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.todouble;
2 |
3 | public class StringToDoubleExample3 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // double value in string format
8 | String to_double = "1234";
9 |
10 | Double doubleValue = new Double(to_double);
11 |
12 | System.out.println("String to Double value with Double constructor : " + doubleValue);
13 |
14 | to_double = "100d";
15 |
16 | doubleValue = new Double(to_double);
17 |
18 | System.out.println("String to Double value with d in the string using Double constructor : " + doubleValue);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/sort/reverse/ArraySortReverseInts.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.sort.reverse;
2 |
3 | import java.util.Arrays;
4 | import java.util.Collections;
5 |
6 | public class ArraySortReverseInts {
7 |
8 | public static void main(String[] args) {
9 |
10 | Integer[] ints = new Integer[5];
11 | ints[0] = 20;
12 | ints[1] = 50;
13 | ints[2] = 40;
14 | ints[3] = 10;
15 | ints[4] = 30;
16 |
17 | System.out.println("Integer array before sort - " + Arrays.toString(ints));
18 |
19 | Arrays.sort(ints, Collections.reverseOrder());
20 |
21 | System.out.println("Integer array after sort - " + Arrays.toString(ints));
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/directory/CurrentDirectoryFileSystemsExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.directory;
2 |
3 | import java.nio.file.FileSystems;
4 | import java.nio.file.Path;
5 |
6 | public class CurrentDirectoryFileSystemsExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | // Current location from FileSystems.
11 | Path currentPath = FileSystems.getDefault().getPath(".");
12 |
13 | // getting the location from Path.toAbsolutePath()
14 | String currentLocation = currentPath.toAbsolutePath().normalize().toString();
15 |
16 |
17 | System.out.println("Current working directoruy : " + currentLocation);
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/keywords/extend/ClasesExtendsExamples.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.keywords.extend;
2 |
3 | public class ClasesExtendsExamples {
4 |
5 | public static void main(String[] args) {
6 |
7 | HondaCar hondaCar = new HondaCar();
8 | int noOfWheels = hondaCar.getNoOfWheels();
9 | System.out.println("Honda car wheels : " + noOfWheels);
10 |
11 | }
12 | }
13 |
14 | class Car {
15 |
16 | int noOfWheels = 4;
17 | }
18 |
19 | class HondaCar extends Car {
20 |
21 | public int getNoOfWheels() {
22 | return this.noOfWheels;
23 | }
24 | }
25 | /*
26 | class HybridCar extends Car , HondaCar{
27 |
28 | }
29 | */
30 |
31 | class SkodaCar extends Car {
32 |
33 | }
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/conversions/string/StringtoStringArraySplit.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.conversions.string;
2 |
3 | public class StringtoStringArraySplit {
4 |
5 | public static void main(String[] args) {
6 |
7 | // input string
8 | String input = "javaprogramto.com";
9 |
10 | // spliting string into string array using split() method.
11 | String[] stringArray = input.split("");
12 |
13 | // printing the values of string array
14 | for (String string : stringArray) {
15 | System.out.println(string);
16 | }
17 |
18 | System.out.println(stringArray.length);
19 | System.out.println(input.length());
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/arrays/average/ArrayAverage.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.arrays.average;
2 |
3 | public class ArrayAverage {
4 |
5 | public static void main(String[] args) {
6 |
7 | // create an array
8 | int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
9 |
10 | // getting array length
11 | int length = array.length;
12 |
13 | // default sium value.
14 | int sum = 0;
15 |
16 | // sum of all values in array using for loop
17 | for (int i = 0; i < array.length; i++) {
18 | sum += array[i];
19 | }
20 |
21 | double average = sum / length;
22 |
23 | System.out.println("Average of array : "+average);
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/w3schools/programs/ascii/StringToAsciiUSBytes.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.w3schools.programs.ascii;
2 |
3 | import java.nio.charset.StandardCharsets;
4 | import java.util.Arrays;
5 |
6 | public class StringToAsciiUSBytes {
7 |
8 | public static void main(String[] args) {
9 |
10 | String input = "ascii US bytes";
11 |
12 | // converting string to bytes array.
13 | byte[] bytesArray = input.getBytes(StandardCharsets.US_ASCII);
14 |
15 | // bytes array to sscii string.
16 | String asciiStr = Arrays.toString(bytesArray);
17 |
18 | System.out.println("String to ASCII with US_ASCII : "+asciiStr);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/timestamp/CurrentTimestampExample6.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.timestamp;
2 |
3 | import java.time.ZoneId;
4 | import java.time.ZonedDateTime;
5 | import java.time.format.DateTimeFormatter;
6 |
7 | public class CurrentTimestampExample6 {
8 |
9 | public static void main(String[] args) {
10 |
11 | ZoneId zoneId = ZoneId.systemDefault();
12 | ZonedDateTime localDateTime = ZonedDateTime.now(zoneId);
13 |
14 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
15 |
16 | String timestamp = formatter.format(localDateTime);
17 |
18 | System.out.println("timestamp now : " + timestamp);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/integer/IntegerCompareUnsignedExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.integer;
2 |
3 | public class IntegerCompareUnsignedExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | Integer integer1 = Integer.compareUnsigned(10, 20);
8 | System.out.println("Result of comparing two unsigned values (10, 20) : " + integer1);
9 |
10 | Integer integer2 = Integer.compareUnsigned(20, 10);
11 | System.out.println("Result of comparing two unsigned values (20, 10) : " + integer2);
12 |
13 | Integer integer3 = Integer.compareUnsigned(10, 10);
14 | System.out.println("Result of comparing two unsigned values (10, 10) : " + integer3);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/filter/ifelse/Java8StreammultipleIfExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.filter.ifelse;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.stream.Collectors;
6 |
7 | public class Java8StreammultipleIfExample {
8 |
9 | public static void main(String[] args) {
10 |
11 | List numbersList = Arrays.asList(10, 13, 15, 20, 24, 16, 17, 100);
12 |
13 | List evenList = numbersList.stream()
14 | .filter(number -> number > 20)
15 | .filter(number -> number % 2 == 0)
16 | .collect(Collectors.toList());
17 |
18 | System.out.println("Even numbers list - " + evenList);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/scanner/close/ScannerCloseExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.scanner.close;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ScannerCloseExample4 {
6 |
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 | System.out.println("Enter your birth year");
11 | int year = scanner.nextInt();
12 |
13 | System.out.println("Enter your age ");
14 | int age = scanner.nextInt();
15 |
16 | scanner.close();
17 |
18 | System.out.println("Given age and year are (" + age + "," + year + ")");
19 |
20 | System.out.println("Enter your name ");
21 | String name = scanner.next();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/setall/LongArraySetAllExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.setall;
2 |
3 | import java.util.Arrays;
4 |
5 | public class LongArraySetAllExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | // Creating a long array with default values
10 | long[] longArray = new long[7];
11 |
12 | System.out.println("original long array with default values : "+ Arrays.toString(longArray));
13 |
14 | // setting values to longArray using setAll() method
15 | Arrays.setAll(longArray, i -> i + 10 * 20);
16 |
17 | System.out.println("Long array.setAll(long) output: "+ Arrays.toString(longArray));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/getdatetime/SimpleDateFormatExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.getdatetime;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | public class SimpleDateFormatExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | // current date custom format
11 | SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
12 |
13 | // current date and time
14 | Date currentDate = new Date();
15 |
16 | // Util Date to String using format()
17 | String strDate = dateFormatter.format(currentDate);
18 |
19 | System.out.println("Current Date using SimpleDateFormat : "+strDate);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/optional/OfNullableExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.optional;
2 |
3 | import java.util.Optional;
4 |
5 | public class OfNullableExample {
6 | public static void main(String[] args) {
7 |
8 | // Creating optional
9 | Optional optional = Optional.ofNullable("javaprogramto.com");
10 |
11 | System.out.println("Checking if optional has value with isEmpty() method");
12 | System.out.println("isEmpty value : "+optional.isPresent());
13 |
14 | // empty optional
15 | Optional emptyOptional = Optional.ofNullable(null);
16 | System.out.println("isPresent value for empty optional : "+emptyOptional.isPresent());
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/skip/Skip5EvenNumbersExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.skip;
2 |
3 | import java.util.List;
4 | import java.util.stream.Collectors;
5 | import java.util.stream.Stream;
6 |
7 | public class Skip5EvenNumbersExample {
8 | public static void main(String[] args) {
9 |
10 | Stream evenNumbers = Stream.of(2, 4, 5, 8, 10, 12, 14, 16, 18, 20);
11 |
12 | List next20Numbers = evenNumbers
13 | .skip(5)
14 | .collect(Collectors.toList());
15 |
16 | System.out.println("skipping first 5 even Numbers");
17 | next20Numbers.forEach(number -> System.out.println(number));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/arrays/search/FIndValue.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.arrays.search;
2 |
3 | public class FIndValue {
4 |
5 | public static void main(String[] args) {
6 |
7 | int[] array = { 1, 4, 6, 2, 5 };
8 |
9 | int findValueOf = 6;
10 |
11 | boolean isFound = false;
12 | for (int i = 0; i < array.length; i++) {
13 |
14 | if (array[i] == findValueOf) {
15 | isFound = true;
16 | break;
17 | }
18 |
19 | }
20 |
21 | if (isFound) {
22 | System.out.println("Number " + findValueOf + " is found in the array");
23 | } else {
24 | System.out.println("Number " + findValueOf + " is not found in the array");
25 | }
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/bytearray/Base64Encode.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.bytearray;
2 |
3 | import java.nio.charset.CharacterCodingException;
4 | import java.util.Base64;
5 |
6 | public class Base64Encode {
7 |
8 | public static void main(String[] args) throws CharacterCodingException {
9 |
10 | // creating byte array
11 | byte[] byteArray = { 76, 77, 65 };
12 |
13 | // Create base simple decoder object
14 | Base64.Decoder simpleDecoder = Base64.getDecoder();
15 |
16 | // Deconding the encoded string using decoder
17 | String decodedString = new String(simpleDecoder.decode(byteArray));
18 | System.out.println("Decoded String : " + decodedString);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/empty/ReturnEmptyArrayExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.empty;
2 |
3 | import org.apache.commons.lang3.ArrayUtils;
4 |
5 | public class ReturnEmptyArrayExample3 {
6 |
7 | public static void main(String[] args) {
8 |
9 | if (true) {
10 | int[] array1 = emptyArrayWIthCurlyBraces();
11 | System.out.println("Length of int array 1 : " + array1.length);
12 | }
13 |
14 | if (100 > 0) {
15 | int[] array2 = emptyArrayWIthCurlyBraces();
16 | System.out.println("Length of int array 2 : " + array2.length);
17 | }
18 |
19 | }
20 |
21 | public static int[] emptyArrayWIthCurlyBraces() {
22 |
23 | return ArrayUtils.EMPTY_INT_ARRAY;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/readonlywrite/FileSetWritableExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.readonlywrite;
2 |
3 | import java.io.File;
4 |
5 | /**
6 | * Example to convert the file from read only to writable form.
7 | *
8 | * @author javaprogramto.com
9 | *
10 | */
11 | public class FileSetWritableExample {
12 |
13 | public static void main(String[] args) {
14 |
15 | File newFile = new File("src/main/java/com/javaprogramto/files/readonlywrite/make-read-only.txt");
16 |
17 | // Changing the file from read only to writable format.
18 | boolean isWritableNow = newFile.setWritable(true);
19 |
20 | System.out.println("Can write the file ? : " + isWritableNow);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/instant/InstantEpochSecondExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.instant;
2 |
3 | import java.time.Instant;
4 |
5 | public class InstantEpochSecondExample {
6 | public static void main(String[] args) {
7 |
8 | // Creating instant object from ofEpochMilli()
9 | Instant instantFromEpochSecond1 = Instant.ofEpochMilli(1606657474l);
10 | System.out.println("Instant 1 from Epoch Second value using ofEpochMilli() : " + instantFromEpochSecond1);
11 |
12 | Instant instantFromEpochSecond2 = Instant.ofEpochMilli(1667137474l);
13 | System.out.println("Instant 2 from Epoch Second value using ofEpochMilli() : " + instantFromEpochSecond2);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/localdate/AddingDaysExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.localdate;
2 |
3 | import java.time.LocalDate;
4 |
5 | public class AddingDaysExample {
6 |
7 | public static void main(String[] args) {
8 | // creating current date time
9 | LocalDate currentDate = LocalDate.now();
10 |
11 | // Getting the next date
12 | LocalDate nextDay = currentDate.plusDays(1);
13 |
14 | // Getting the previous date
15 | LocalDate previousDay = currentDate.minusDays(1);
16 |
17 | System.out.println("Current date : "+currentDate);
18 | System.out.println("Next date : "+nextDay);
19 | System.out.println("Previous date : "+previousDay);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/optional/OptionalIsEmpty.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.optional;
2 |
3 | import java.util.Optional;
4 |
5 | public class OptionalIsEmpty {
6 | public static void main(String[] args) {
7 |
8 | // Creating optional
9 | Optional optional = Optional.ofNullable("javaprogramto.com");
10 |
11 | System.out.println("Checking if optional has value with isEmpty() method");
12 | //System.out.println("isEmpty value : " + optional.isEmpty());
13 |
14 | // empty optional
15 | Optional emptyOptional = Optional.ofNullable(null);
16 | //System.out.println("isPreisEmptysent value for empty optional : " + emptyOptional.isEmpty());
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/remove/character/StringRemoveCharacterExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.remove.character;
2 |
3 | import java.util.stream.Collectors;
4 |
5 | public class StringRemoveCharacterExample4 {
6 |
7 | public static void main(String[] args) {
8 |
9 | String input = "hello +world ++";
10 | char removalCh = '+';
11 |
12 | String output = input.chars()
13 | .filter(ch -> ch != removalCh)
14 | .mapToObj(ch -> String.valueOf(ch))
15 | .collect(Collectors.joining());
16 |
17 | System.out.println("Input string 1 : " + input);
18 | System.out.println("Output string after removing the + symbol : " + output);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/maths/LCMFromGCDExample.java:
--------------------------------------------------------------------------------
1 |
2 | package com.javaprogramto.programs.maths;
3 |
4 | public class LCMFromGCDExample {
5 |
6 | public static void main(String[] args) {
7 |
8 | printLCMUsingGCD(100, 152);
9 | printLCMUsingGCD(10, 1100);
10 | printLCMUsingGCD(140, 300);
11 |
12 | }
13 |
14 | private static void printLCMUsingGCD(int a, int b) {
15 |
16 | // finding GCD
17 | int gcd = 1;
18 | for (int i = 1; i <= a && i <= b; i++) {
19 |
20 | if(a % i == 0 && b % i == 0) {
21 | gcd = i;
22 | }
23 | }
24 |
25 | int lcm = a * b / gcd;
26 | System.out.printf("\nThe LCM of two numbers %d and %d is %d.", a, b, lcm);
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/w3schools/programs/string/StringSubStringExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.w3schools.programs.string;
2 |
3 | public class StringSubStringExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String accountInfo = "In Active";
8 |
9 | String accountStatus = accountInfo.substring(3);
10 |
11 | System.out.println("Extracted account status from account info text for account 1: "+accountStatus);
12 |
13 | accountInfo = "Is Locked";
14 |
15 | accountStatus = accountInfo.substring(3);
16 |
17 | System.out.println("Extracted account status from account info text for account 2: "+accountStatus);
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.wst.common.component:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/collections/set/add/LinkedHashSetAddExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.collections.set.add;
2 |
3 | import java.util.LinkedHashSet;
4 | import java.util.Set;
5 |
6 | public class LinkedHashSetAddExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | Set set = new LinkedHashSet<>();
11 |
12 | boolean isAdded = set.add(10);
13 | System.out.println("Is 10 added? " + isAdded);
14 |
15 | isAdded = set.add(10);
16 | System.out.println("Is 10 added again? " + isAdded);
17 |
18 | isAdded = set.add(20);
19 | isAdded = set.add(30);
20 | isAdded = set.add(40);
21 |
22 | System.out.println("LinkedHashSet values : " + set);
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/collections/treemap/comparator/TreeMapComparatorExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.collections.treemap.comparator;
2 |
3 | import java.util.Map;
4 | import java.util.TreeMap;
5 |
6 | public class TreeMapComparatorExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | Map designationSalaryInUSD = new TreeMap<>();
11 |
12 | designationSalaryInUSD.put("Software Engineer", 150_000);
13 | designationSalaryInUSD.put("Senior Software Engineer", 210_000);
14 | designationSalaryInUSD.put("Manger", 300_000);
15 | designationSalaryInUSD.put("Lead Engineer", 250_000);
16 |
17 | System.out.println("treemap - " + designationSalaryInUSD);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/reduce/StreamReduceExample7.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.reduce;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | import com.javaprogramto.java8.compare.Employee;
7 |
8 | public class StreamReduceExample7 {
9 |
10 | public static void main(String[] args) {
11 |
12 | List empList = Arrays.asList(new Employee(100, "A", 20), new Employee(200, "B", 30),
13 | new Employee(300, "C", 40));
14 |
15 | Integer ageSum = empList.stream().map(emp -> emp.getAge()).reduce(new Employee(400, "new emp", 60).getAge(),
16 | (age1, age2) -> age1 + age2);
17 |
18 | System.out.println("age sum - " + ageSum);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/count/CountDigitsIterativeExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.count;
2 |
3 | public class CountDigitsIterativeExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | // Input number
8 | int number = 12345;
9 |
10 | // storing the original number in temp variable
11 | int originalNumber = number;
12 |
13 | // default digits count
14 | int countDigits = 0;
15 |
16 | // counting the digits
17 | while (number > 0) {
18 |
19 | number = number / 10;
20 | countDigits++;
21 |
22 | }
23 |
24 | // printing the output
25 | System.out.println("No of digits in number " + originalNumber + " is " + countDigits);
26 |
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java11/shebang/BatchJob.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java11.shebang;
2 |
3 | import java.util.Arrays;
4 | import java.util.stream.IntStream;
5 | import java.util.stream.Stream;
6 |
7 | public class BatchJob {
8 |
9 | public static void main(String[] args) {
10 |
11 | System.out.println("Hello World");
12 |
13 | Stream stream = Arrays.stream(args);
14 |
15 | int sum = stream.mapToInt(Integer::parseInt).sum();
16 | System.out.println("Total sum : "+sum);
17 |
18 | IntStream intStream =IntStream.range(1, 5);
19 |
20 | int output = intStream.sum();
21 | System.out.println("Sum of first 5 numbers - "+output);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/localdatetime/LocalDateTimeCreationExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.localdatetime;
2 |
3 | import java.time.LocalDateTime;
4 |
5 | public class LocalDateTimeCreationExample {
6 |
7 | public static void main(String[] args) {
8 | // Creating the current date and time using LocalDateTime.now() method
9 | LocalDateTime currentDatetime = LocalDateTime.now();
10 | System.out.println("Current date time : "+currentDatetime);
11 |
12 | // Creating the future date and time using LocalDateTime.of() method
13 | LocalDateTime futureDateTime = LocalDateTime.of(2025, 01, 01, 00, 45);
14 | System.out.println("Future date time : "+futureDateTime);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/maptolist/MapValuesToListExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.maptolist;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | public class MapValuesToListExample {
9 |
10 | public static void main(String[] args) {
11 |
12 | Map idNames = new HashMap<>();
13 |
14 | idNames.put(100, "modi");
15 | idNames.put(101, "trump");
16 | idNames.put(102, "putin");
17 |
18 | //converting all values from map to list.
19 | List ids =new ArrayList<>(idNames.values());
20 |
21 | ids.forEach(id -> System.out.println(id));
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/infinite/StreamGenerateExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.infinite;
2 |
3 | import java.util.List;
4 | import java.util.UUID;
5 | import java.util.function.Supplier;
6 | import java.util.stream.Collectors;
7 | import java.util.stream.Stream;
8 |
9 | public class StreamGenerateExample {
10 |
11 | public static void main(String[] args) {
12 |
13 | // Generates the UUID
14 | Supplier randomUUIDSupplier = () -> UUID.randomUUID();
15 |
16 | List uuidList = Stream.generate(randomUUIDSupplier).limit(10).collect(Collectors.toList());
17 |
18 | System.out.println("10 random UUID list : "+uuidList);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/threads/AnonymousRunnableThread.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.threads;
2 |
3 |
4 | public class AnonymousRunnableThread {
5 |
6 | public static void main(String[] args) {
7 |
8 | new Thread(new Runnable() {
9 | @Override
10 | public void run() {
11 |
12 | for (int i = 0; i < 10; i++) {
13 | System.out.println(Thread.currentThread().getName()+", i value from thread- "+i);
14 | }
15 | }
16 | }).start();
17 |
18 | for (int i = 0; i < 10; i++) {
19 | System.out.println(Thread.currentThread().getName()+", i value from main thread - "+i);
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/setall/DoubleArraySetAllExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.setall;
2 |
3 | import java.util.Arrays;
4 |
5 | public class DoubleArraySetAllExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | // Creating a double array with default values
10 | double[] doubleArray = new double[7];
11 |
12 | System.out.println("original double array with default values : "+ Arrays.toString(doubleArray));
13 |
14 | // setting values to doubleArray using setAll() method
15 | Arrays.setAll(doubleArray, i -> i + 10.5);
16 |
17 | System.out.println("Double array.setAll(double) output: "+ Arrays.toString(doubleArray));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/setall/StringArraySetAllExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.setall;
2 |
3 | import java.util.Arrays;
4 |
5 | public class StringArraySetAllExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | // Creating a String array with default values
10 | String[] stringArray = new String[7];
11 |
12 | System.out.println("original long array with default values : "+ Arrays.toString(stringArray));
13 |
14 | // setting values to stringArray using setAll() method
15 | Arrays.setAll(stringArray, i -> "Index " + i);
16 |
17 | System.out.println("String array.setAll(String) output: "+ Arrays.toString(stringArray));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/convert/string/toint/StringToIntException.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.convert.string.toint;
2 |
3 | /**
4 | * Example to exception in string to integer conversion
5 | *
6 | * @author Javaprogramto.com
7 | *
8 | */
9 | public class StringToIntException {
10 |
11 | public static void main(String[] args) {
12 |
13 | // creating string with alphabets
14 | String s = "hello world";
15 |
16 | // convert string to int using parseInt() method. This will throw exception
17 | // int number = Integer.parseInt(s);
18 | // int number = Integer.valueOf(s);
19 | Integer number = new Integer(s);
20 |
21 | // printing vlaues
22 | System.out.println("Number : " + number);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/design/patterns/strategy1/test/TestCalculationStrategy.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.design.patterns.strategy1.test;
2 |
3 | import com.javaprogramto.design.patterns.strategy1.algorithms.AdditionOperation;
4 | import com.javaprogramto.design.patterns.strategy1.context.OperationContext;
5 |
6 | public class TestCalculationStrategy {
7 |
8 | public static void main(String[] args) {
9 |
10 | OperationContext addContext = new OperationContext(new AdditionOperation());
11 | int add = addContext.executeStrategy(10, 20);
12 | System.out.println("Addition Operation strategy output : "+add);
13 |
14 | // write there for the reamining three.
15 |
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/optional/OptionalGetExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.optional;
2 |
3 | import java.util.Optional;
4 |
5 | public class OptionalGetExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | // Creating non null optional object.
10 | Optional optional= Optional.of("hello");
11 |
12 | // checking value present in the optional or not.
13 | if(optional.isPresent()){
14 | String value = optional.get();
15 | System.out.println("Optional value : "+value);
16 | } else {
17 | // if optional has no value
18 | System.out.println("Optional has no value");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/arrays/search/IntStreamFindValue.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.arrays.search;
2 |
3 | import java.util.stream.IntStream;
4 |
5 | public class IntStreamFindValue {
6 |
7 | public static void main(String[] args) {
8 |
9 | int[] array = { 10, 30, 20, 90, 40, 60 };
10 |
11 | int findValueOf = 90;
12 |
13 | boolean isFound = false;
14 |
15 | isFound = IntStream.of(array).anyMatch(condition -> condition == findValueOf);
16 |
17 | if (isFound) {
18 | System.out.println("Number " + findValueOf + " is found in the array in java 8");
19 | } else {
20 | System.out.println("Number " + findValueOf + " is not found in the array in java 8");
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/numbers/NumberPowerMathPowExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.numbers;
2 |
3 | public class NumberPowerMathPowExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | // create two variables to store the base and exponenet values
8 | int base = 5;
9 | int exponent = 7;
10 |
11 | // printing the input values
12 | System.out.println("Base : " + base + ", Exponent : " + exponent);
13 |
14 | // a result variable to store the output
15 | double result = 1;
16 |
17 | // Calculating the power using Math.pow() method
18 | result = Math.pow(base, exponent);
19 |
20 | // Printing the output
21 | System.out.println("Result : " + result);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/split/StringSplitExampleWithSlash.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings.split;
2 |
3 | public class StringSplitExampleWithSlash {
4 |
5 | public static void main(String[] args) {
6 | String fileLocation = "/Users/javaprogramto/Documents/course/blog/workspace/CoreJava/split.java";
7 |
8 | String[] slashBasedSplitArray = fileLocation.split("/");
9 |
10 | System.out.println("Splitting string with dot using regex 1");
11 | for (String value : slashBasedSplitArray) {
12 | System.out.println(value);
13 | }
14 |
15 | System.out.println("No of folders in the path : " + (slashBasedSplitArray.length - 1));
16 |
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/printwriter/tofile/PrintWriterToFileExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.printwriter.tofile;
2 |
3 | import java.io.File;
4 | import java.io.FileNotFoundException;
5 | import java.io.PrintWriter;
6 |
7 | public class PrintWriterToFileExample1 {
8 |
9 | public static void main(String[] args) throws FileNotFoundException {
10 |
11 | File file = new File("src/main/resources/print_writer/test2.txt");
12 |
13 | PrintWriter writer = new PrintWriter(file);
14 |
15 | writer.println("Line 1");
16 | writer.println("Line 2");
17 | writer.println("Line 3");
18 |
19 | writer.flush();
20 |
21 | writer.close();
22 | System.out.println("Done");
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/localdate/LocalDateAtTimeExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.localdate;
2 |
3 | import java.time.LocalDate;
4 | import java.time.LocalDateTime;
5 |
6 | public class LocalDateAtTimeExample {
7 |
8 | public static void main(String[] args) {
9 | // localdate
10 | LocalDate localDate = LocalDate.now();
11 |
12 | int hour = 10;
13 | int minute = 20;
14 | int second = 30;
15 |
16 | // setting time details and converting LocalDate to LocalDateTime.
17 | LocalDateTime dateAndTime = localDate.atTime(hour, minute, second);
18 |
19 | System.out.println("LocalDate : "+localDate);
20 | System.out.println("LocalDateTime : "+dateAndTime);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/characters/IfElseVowel.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.characters;
2 |
3 | public class IfElseVowel {
4 |
5 | public static void main(String[] args) {
6 | char ch = 'a';
7 |
8 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
9 | System.out.println("Chracter '" + ch + "' is a vowel");
10 | } else {
11 | System.out.println("Chracter '" + ch + "' is a consonant");
12 | }
13 |
14 | ch = 'b';
15 |
16 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
17 | System.out.println("Chracter '" + ch + "' is a vowel");
18 | } else {
19 | System.out.println("Chracter '" + ch + "' is a consonant");
20 | }
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/remove/character/StringRemoveCharacterExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.remove.character;
2 |
3 | public class StringRemoveCharacterExample3 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String input = "hello +world ++";
8 | char removalCh = '+';
9 |
10 | char[] chars = input.toCharArray();
11 |
12 | StringBuilder builder = new StringBuilder();
13 |
14 | for (char ch : chars) {
15 |
16 | if (ch != removalCh) {
17 | builder.append(ch);
18 | }
19 |
20 | }
21 |
22 | System.out.println("Input string 1 : " + input);
23 | System.out.println("Output string after removing the + symbol : " + builder.toString());
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/datatypes/diff/FloatVsDoubleDefault.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.datatypes.diff;
2 |
3 | public class FloatVsDoubleDefault {
4 |
5 | static float floatDefaultvalue;
6 | static double doubleDefaultvalue;
7 |
8 | public static void main(String[] args) {
9 |
10 | // default types
11 | double d = 12345.6789; // no error. so it is considered as double type.
12 |
13 | //float f = 12345.6789; // error. Type mismatch: cannot convert from double to float. float type is not
14 | // detected
15 |
16 | // default values
17 |
18 | System.out.println("Float type default value - " + floatDefaultvalue);
19 | System.out.println("Double type default value - " + doubleDefaultvalue);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/printwriter/tofile/PrintWriterToFileExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.printwriter.tofile;
2 |
3 | import java.io.File;
4 | import java.io.FileNotFoundException;
5 | import java.io.PrintWriter;
6 |
7 | public class PrintWriterToFileExample2 {
8 |
9 | public static void main(String[] args) throws FileNotFoundException {
10 |
11 | File file = new File("src/main/resources/print_writer/file/test2.txt");
12 |
13 | PrintWriter writer = new PrintWriter(file);
14 |
15 | writer.println("Line 1");
16 | writer.println("Line 2");
17 | writer.println("Line 3");
18 |
19 | writer.flush();
20 |
21 | writer.close();
22 | System.out.println("Done");
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/arraylist/ArrayListMaxSizeExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.arraylist;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class ArrayListMaxSizeExample2 {
7 |
8 | public static void main(String[] args) {
9 |
10 | System.out.println("Integer.MAX_VALUE - " + Integer.MAX_VALUE);
11 | System.out.println("Math.pow(2, 31) - " + (int) Math.pow(2, 31));
12 |
13 | // default size is 10
14 | int maxValue = Integer.MAX_VALUE - 1;
15 | List intList = new ArrayList<>(maxValue);
16 |
17 | for (int i = 0; i < maxValue; i++) {
18 | intList.add(i);
19 | }
20 |
21 | System.out.println("Int list size - " + intList.size());
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/datetimeformatter/DateTimeFormatterFormatStyleExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.datetimeformatter;
2 |
3 | import java.time.LocalDateTime;
4 | import java.time.format.DateTimeFormatter;
5 | import java.time.format.FormatStyle;
6 |
7 | public class DateTimeFormatterFormatStyleExample {
8 |
9 | public static void main(String[] args) {
10 |
11 | FormatStyle full = FormatStyle.FULL;
12 |
13 | DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(full);
14 |
15 | LocalDateTime datetime = LocalDateTime.now();
16 |
17 | String fullForm = datetime.format(formatter);
18 |
19 | System.out.println("Full date form in string : "+fullForm);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/format/FormatTIme24HoursExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.format;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | public class FormatTIme24HoursExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | // Getting the current date and time
11 | Date currentDate = new Date();
12 |
13 | // Creating simple date formatter to 24 hours
14 | SimpleDateFormat formatter = new SimpleDateFormat("kk:mm:ss");
15 |
16 | // getting the time in 24 hours format
17 | String timeIn24Hours = formatter.format(currentDate);
18 |
19 | // printing the time
20 | System.out.println("Current time in 24 hours : "+timeIn24Hours);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/large/Largest3NumbersNestedIfExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.large;
2 |
3 | public class Largest3NumbersNestedIfExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | int number1 = 100;
8 | int number2 = 20;
9 | int number3 = 300;
10 |
11 | if (number1 >= number2) {
12 | if (number1 >= number3) {
13 | System.out.println(number1 + " is the biggest");
14 | } else {
15 | System.out.println(number3 + " is the biggest");
16 | }
17 | } else {
18 | if (number2 >= number3) {
19 | System.out.println(number2 + " is the biggest");
20 | } else {
21 | System.out.println(number3 + " is the biggest");
22 | }
23 | }
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/substring/count/SubstringCountExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.substring.count;
2 |
3 | public class SubstringCountExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | int count = countSubStringInString("222", "22");
8 | System.out.println(count);
9 |
10 | count = countSubStringInString("madam", "ma");
11 | System.out.println(count);
12 |
13 | }
14 |
15 | public static int countSubStringInString(String string, String toFind) {
16 | int position = 0;
17 | int count = 0;
18 | while ((position = string.indexOf(toFind, position)) != -1) {
19 | position = position + 1;
20 | count++;
21 | }
22 | return count;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/numbers/swap/SwapNumbersWIthoutThirdVariable.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.numbers.swap;
2 |
3 | public class SwapNumbersWIthoutThirdVariable {
4 |
5 | public static void main(String[] args) {
6 |
7 | int firstNumber = 10;
8 | int secondNumber = 20;
9 |
10 |
11 | System.out.println("Before swapping two numbers : firstNumber "
12 | + firstNumber + ", secondNumber " + secondNumber);
13 |
14 |
15 |
16 | firstNumber = firstNumber + secondNumber;
17 | secondNumber = firstNumber - secondNumber;
18 | firstNumber = firstNumber - secondNumber;
19 |
20 |
21 | System.out.println("After swapping two numbers : firstNumber "
22 | + firstNumber + ", secondNumber " + secondNumber);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/convert/chartostring/CharToStringExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.convert.chartostring;
2 |
3 | /**
4 | * char to string using + operator
5 | *
6 | * @author javaprogramto.com
7 | *
8 | */
9 | public class CharToStringExample3 {
10 |
11 | public static void main(String[] args) {
12 |
13 | char ch3 = 'x';
14 | String returnedString = "" + ch3;
15 | System.out.println("Returned string 1 : " + returnedString);
16 |
17 | char ch1 = 'a';
18 | String returnedString2 = "" + ch1;
19 | System.out.println("Returned string 2 : " + returnedString2);
20 |
21 | char ch2 = 'b';
22 | String returnedString3 = "" + ch2;
23 | System.out.println("Returned string 3 : " + returnedString3);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/conversions/string/StringtoStringArraySplitRegularExpression.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.conversions.string;
2 |
3 | public class StringtoStringArraySplitRegularExpression {
4 |
5 | public static void main(String[] args) {
6 |
7 | // input string
8 | String input = "hello geek";
9 |
10 | // splitting string into string array using split() method with regular expression.
11 | String[] stringArray = input.split("(?!^)");
12 |
13 | // printing the values of string array
14 | for (String string : stringArray) {
15 | System.out.println(string);
16 | }
17 |
18 | System.out.println(stringArray.length);
19 | System.out.println(input.length());
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/sort/reverse/ArraySortReverseStrings.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.sort.reverse;
2 |
3 | import java.util.Arrays;
4 | import java.util.Collections;
5 |
6 | public class ArraySortReverseStrings {
7 |
8 | public static void main(String[] args) {
9 |
10 | String[] stringArray = new String[5];
11 | stringArray[0] = "G";
12 | stringArray[1] = "Z";
13 | stringArray[2] = "A";
14 | stringArray[3] = "N";
15 | stringArray[4] = "I";
16 |
17 | System.out.println("String array before sort - " + Arrays.toString(stringArray));
18 | Arrays.sort(stringArray, Collections.reverseOrder());
19 |
20 | System.out.println("String array after sort - " + Arrays.toString(stringArray));
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/collectors/tolist/StreamCollectToListExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.collectors.tolist;
2 |
3 | import java.util.LinkedList;
4 | import java.util.List;
5 | import java.util.stream.Collectors;
6 | import java.util.stream.Stream;
7 |
8 | public class StreamCollectToListExample2 {
9 |
10 | public static void main(String[] args) {
11 | Stream vowels2 = Stream.of("A", "E", "I", "O", "U");
12 |
13 | List list = vowels2.collect(Collectors.toCollection(LinkedList::new));
14 |
15 | if(list instanceof LinkedList) {
16 | System.out.println("Returned list is a instacne of LinkedList");
17 | }
18 | System.out.println("stream collect to LinkedList output : " + list);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/iterate/SplitIteratorForEachRemainingExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.iterate;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 | import java.util.Spliterator;
6 | import java.util.function.Consumer;
7 |
8 | public class SplitIteratorForEachRemainingExample {
9 |
10 | public static void main(String[] args) {
11 |
12 | Set values = new HashSet<>();
13 |
14 | values.add(100);
15 | values.add(200);
16 | values.add(300);
17 | values.add(400);
18 |
19 | Spliterator splitIterator = values.stream().spliterator();
20 |
21 | Consumer consumer = (number) -> System.out.println(number - 1);
22 |
23 | splitIterator.forEachRemaining(consumer);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/permutations/StringPermutationsExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.permutations;
2 |
3 | import java.util.stream.IntStream;
4 |
5 | public class StringPermutationsExample2 {
6 |
7 | public static void main(String[] args) {
8 |
9 | stringPermuteAndPrint("", "ABC");
10 |
11 | }
12 |
13 | // java 8 stream example
14 | private static void stringPermuteAndPrint(String prefix, String str) {
15 | int n = str.length();
16 | if (n == 0) {
17 | System.out.print(prefix + " ");
18 | } else {
19 | IntStream.range(0, n).parallel().forEach(
20 | i -> stringPermuteAndPrint(prefix + str.charAt(i), str.substring(i + 1, n) + str.substring(0, i)));
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/w3schools/programs/string/StringSubEndIndexStringExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.w3schools.programs.string;
2 |
3 | public class StringSubEndIndexStringExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String accountInfo = "Is Active my account?";
8 |
9 | String accountStatus = accountInfo.substring(3, 10);
10 |
11 | System.out.println("Extracted account status from account info text for account 1: "+accountStatus);
12 |
13 | accountInfo = "is my Locked ?";
14 |
15 | accountStatus = accountInfo.substring(6, 12);
16 |
17 | System.out.println("Extracted account status from account info text for account 2: "+accountStatus);
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/keywords/extend/FinalClasesExtendsExamples.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.keywords.extend;
2 |
3 | import java.util.Calendar;
4 |
5 | public class FinalClasesExtendsExamples {
6 |
7 | public static void main(String[] args) {
8 |
9 | }
10 | }
11 |
12 | final class Calculator {
13 |
14 | public int sum(int a, int b) {
15 | return a + b;
16 | }
17 |
18 | public int substract(int a, int b) {
19 | return a - b;
20 | }
21 |
22 | public int multiply(int a, int b) {
23 | return a * b;
24 | }
25 |
26 | public int divide(int a, int b) {
27 | return a / b;
28 | }
29 |
30 | public int reminder(int a, int b) {
31 | return a % b;
32 | }
33 |
34 | }
35 |
36 | class MyCalculator extends Calendar{
37 |
38 |
39 | }
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/joining/JoiningStringDelimiterExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.joining;
2 |
3 | public class JoiningStringDelimiterExample3 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // input 1
8 | String output1 = stringJoinWithDelimiter("-", "hello", "world", "welcome", "to", "java", "programs");
9 | System.out.println("Ouptut 1 : " + output1);
10 |
11 | // input 1
12 | String output2 = stringJoinWithDelimiter("**", "this", "is", "second", "input");
13 | System.out.println("Ouptut 2 : " + output2);
14 |
15 | }
16 |
17 | private static String stringJoinWithDelimiter(String delimiter, String... strings) {
18 |
19 | return String.join(delimiter, strings);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/tolong/StringToLongExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.tolong;
2 |
3 | public class StringToLongExample3 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String positiveString = "12345";
8 |
9 | Long longvalue1 = new Long(positiveString);
10 | System.out.println("Positive Long value 1 : " + longvalue1);
11 |
12 | String positiveString2 = "+12345";
13 |
14 | Long longvalue2 = new Long(positiveString2);
15 | System.out.println("Positive Long value 2 : " + longvalue2);
16 |
17 | String negativeValue1 = "-12345";
18 |
19 | Long longvalue3 = new Long(negativeValue1);
20 | System.out.println("Negative Long value 3 : " + longvalue3);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/split/StringSplitLimitExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings.split;
2 |
3 | public class StringSplitLimitExample3 {
4 |
5 | public static void main(String[] args) {
6 | String stringWithEndingWithDelimiter = "Java:Program:::to:.com:::";
7 |
8 | String[] arrayWithoutLimitValue = stringWithEndingWithDelimiter.split(":");
9 |
10 | String[] arrayWithLimitValue = stringWithEndingWithDelimiter.split(":", -2);
11 |
12 | System.out.println("returned array size with limit parameter : "+arrayWithLimitValue.length);
13 | for(String value : arrayWithLimitValue){
14 | System.out.println(value);
15 | }
16 | System.out.println("Done");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/threads/AnonymousThreadExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.threads;
2 |
3 | public class AnonymousThreadExample {
4 | public static void main(String[] args) {
5 | new Thread() {
6 |
7 | public void run() {
8 |
9 | for (int i = 0; i <= 10; i++) {
10 | System.out.println(
11 | "Executing " + i + " thread name - " + Thread.currentThread().getName());
12 | }
13 |
14 | }
15 |
16 | }.start();
17 |
18 | for (int i = 0; i <= 10; i++) {
19 | System.out.println(
20 | "Executing " + i + " thread name - " + Thread.currentThread().getName());
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/chars/AddCharToStringExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.chars;
2 |
3 | public class AddCharToStringExample4 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String longString = "very difficult to beat 90% in engineering";
8 |
9 | String addMiddle = longString.substring(0, longString.length() / 2) + ','
10 | + longString.substring(longString.length() / 2);
11 |
12 | System.out.println("after adding in middle : " + addMiddle);
13 |
14 | String atStartOfString = 'A' + longString.substring(0);
15 | String atEndOfString = longString.substring(0) + 'A';
16 |
17 | System.out.println("At start of string : "+atStartOfString);
18 | System.out.println("At end of string : "+atEndOfString);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/filter/ThrowingFunctionExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.filter;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.stream.Stream;
6 |
7 | import pl.touk.throwing.ThrowingFunction;
8 |
9 | public class ThrowingFunctionExample {
10 |
11 | public static void main(String[] args) {
12 | Book b1 = new Book(123, "/bookstore/100/23/book123.pdf");
13 | Book b2 = new Book(145, "/bookstore/100/45/book145.pdf");
14 | Book b3 = new Book(167, "/bookstore/100/67/book167.pdf");
15 |
16 | List books = Arrays.asList(b1, b2, b3);
17 |
18 | Stream validBooks = books.stream().map(ThrowingFunction.unchecked(Book::isValidBookLocation));
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/find/FindAnyExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.find;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Optional;
6 |
7 | public class FindAnyExample {
8 |
9 | public static void main(String[] args) {
10 |
11 | // creating a list with string values
12 | List values = Arrays.asList("A", "B", "C", "F", "D");
13 |
14 | // getting the any value from stream.
15 | Optional anyValue = values.stream().findAny();
16 |
17 | // printing the value
18 | if (anyValue.isPresent()) {
19 | System.out.println("Any value from stream : " + anyValue.get());
20 | } else {
21 | System.out.println("No value presnt in the stream.");
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/MultiplicationTableForLoop.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs;
2 |
3 | public class MultiplicationTableForLoop {
4 |
5 | public static void main(String[] args) {
6 | int tableNumber = 10;
7 |
8 | System.out.println("Generating the table 10");
9 | // generating table 10
10 | for (int i = 1; i <= 10; i++) {
11 |
12 | System.out.format("%d * %d = %d \n", tableNumber, i, tableNumber * i);
13 |
14 | }
15 |
16 | // generating the 20 table.
17 | System.out.println("\nGenerating the table 20");
18 | int anotherTableNumber = 20;
19 | for (int i = 1; i <= 10; i++) {
20 |
21 | System.out.format("%d * %d = %d \n", anotherTableNumber, i, anotherTableNumber * i);
22 |
23 | }
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/datatypes/diff/FloatVsDoublePrecision.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.datatypes.diff;
2 |
3 | public class FloatVsDoublePrecision {
4 |
5 | public static void main(String[] args) {
6 |
7 | // double examples
8 | double d = 1.2345678912345678;
9 |
10 | System.out.println("Double type precision value 1 - " + d);
11 |
12 | double d2 = 1.1020304050d;
13 | System.out.println("Double type precision value 2 with suffix d - " + d2);
14 |
15 | // float examples
16 |
17 | float f = 1.23456789123456789f;
18 |
19 | System.out.println("\n" + "Float type precision value 1 - " + f);
20 |
21 | float f2 = (float) 2.10203040;
22 | System.out.println("Float type precision value 2 with suffix d - " + f2);
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/files/printwriter/tofile/PrintWriterToFileExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.files.printwriter.tofile;
2 |
3 | import java.io.File;
4 | import java.io.FileNotFoundException;
5 | import java.io.PrintWriter;
6 |
7 | public class PrintWriterToFileExample3 {
8 |
9 | public static void main(String[] args) throws FileNotFoundException {
10 |
11 | File file = new File("src/main/resources/print_writer/file/test2.txt");
12 | file.getParentFile().mkdirs();
13 |
14 | PrintWriter writer = new PrintWriter(file);
15 |
16 | writer.println("Line 1");
17 | writer.println("Line 2");
18 | writer.println("Line 3");
19 |
20 | writer.flush();
21 |
22 | writer.close();
23 | System.out.println("Done");
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/remove/duplicates/StringRemoveDuplicatesExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.remove.duplicates;
2 |
3 | import java.util.Arrays;
4 | import java.util.HashSet;
5 | import java.util.Set;
6 | import java.util.stream.Collectors;
7 |
8 | public class StringRemoveDuplicatesExample3 {
9 |
10 | public static void main(String[] args) {
11 |
12 | String orignalString = "world world";
13 |
14 | String output = Arrays.asList(orignalString.split(""))
15 | .stream()
16 | .distinct()
17 | .collect(Collectors.joining());
18 |
19 | System.out.println("Original String : " + orignalString);
20 | System.out.println("After removing the duplicates : " + output);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/tolong/StringToLongExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.tolong;
2 |
3 | public class StringToLongExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String positiveString = "12345";
8 |
9 | Long longvalue1 = Long.valueOf(positiveString);
10 | System.out.println("Positive Long value 1 : " + longvalue1);
11 |
12 | String positiveString2 = "+12345";
13 |
14 | Long longvalue2 = Long.valueOf(positiveString2);
15 | System.out.println("Positive Long value 2 : " + longvalue2);
16 |
17 | String negativeValue1 = "-12345";
18 |
19 | Long longvalue3 = Long.valueOf(negativeValue1);
20 | System.out.println("Negative Long value 3 : " + longvalue3);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/threads/delay/DelayWithExecutorService.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.threads.delay;
2 |
3 | import java.util.concurrent.Executors;
4 | import java.util.concurrent.ScheduledExecutorService;
5 | import java.util.concurrent.TimeUnit;
6 |
7 | public class DelayWithExecutorService {
8 |
9 | public static void main(String[] args) {
10 |
11 | ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
12 |
13 | executorService.schedule(new NewTask2(), 1000, TimeUnit.MILLISECONDS);
14 |
15 | }
16 |
17 | }
18 |
19 | class NewTask implements Runnable {
20 |
21 | @Override
22 | public void run() {
23 |
24 | System.out.println("Task completed");
25 | }
26 |
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/threads/delay/DelayWithThreadSleep.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.threads.delay;
2 |
3 | public class DelayWithThreadSleep {
4 |
5 | public static void main(String[] args) {
6 |
7 | System.out.println("Program Started...");
8 | System.out.println("Current Thread name : " + Thread.currentThread().getName());
9 |
10 | // do some task
11 |
12 | try {
13 | System.out.println("Sleepin for 5 seconds");
14 | Thread.sleep(5000);
15 | } catch (InterruptedException e) {
16 | System.out.println("Thread is interrupted");
17 | }
18 | System.out.println("Code delay completed.");
19 |
20 | System.out.println("Program Ended");
21 |
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/functional/interfaces/function/java8FunctionExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.functional.interfaces.function;
2 |
3 | import java.util.function.Function;
4 |
5 | public class java8FunctionExample {
6 |
7 | public static void main(String[] args) {
8 |
9 | Function function = str -> str.length();
10 |
11 | int length = function.apply("Hello world");
12 | System.out.println("Fucntion to find string length :" + length);
13 |
14 | Function function2 = number -> String.valueOf(number) + " is now String";
15 |
16 | String output = function2.apply(1260);
17 | System.out.println("Funtion to covnert Integer to String : "+output);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/numbers/NumberPowerForLoopExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.numbers;
2 |
3 | public class NumberPowerForLoopExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | // create two variables to store the base and exponenet values
8 | int base = 4;
9 | int exponent = 3;
10 |
11 | // printing the input values
12 | System.out.println("Base : " + base + ", Exponent : " + exponent);
13 |
14 | // a result variable to store the output
15 | int result = 1;
16 |
17 | // Calculating the power using for loop
18 | for (int i = 1; i <= exponent; i++) {
19 | result = result * base;
20 | }
21 |
22 | // Printing the output
23 | System.out.println("Result : " + result);
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/palindrome/StringCheckPalindrome3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.palindrome;
2 |
3 | public class StringCheckPalindrome3 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String input1 = "hello";
8 | boolean isPalindrome = isPalindromeWithStringBuilder(input1);
9 | System.out.println("Is " + input1 + " palindrome? " + isPalindrome);
10 |
11 | String input2 = "abcba";
12 | isPalindrome = isPalindromeWithStringBuilder(input2);
13 | System.out.println("Is " + input2 + " palindrome? " + isPalindrome);
14 | }
15 |
16 | private static boolean isPalindromeWithStringBuilder(String input) {
17 |
18 | return input.equals(new StringBuilder(input).reverse().toString());
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/remove/duplicates/StringRemoveDuplicatesExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.remove.duplicates;
2 |
3 | public class StringRemoveDuplicatesExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String orignalString = "world world";
8 |
9 | StringBuilder builder = new StringBuilder();
10 |
11 | for (int i = 0; i < orignalString.length(); i++) {
12 |
13 | if (builder.indexOf(String.valueOf(orignalString.charAt(i))) == -1) {
14 |
15 | builder.append(orignalString.charAt(i));
16 |
17 | }
18 | }
19 |
20 | System.out.println("Original String : " + orignalString);
21 | System.out.println("After removing the duplicates : " + builder.toString());
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/tolong/StringToLongExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.tolong;
2 |
3 | public class StringToLongExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String positiveString = "12345";
8 |
9 | long longvalue1 = Long.parseLong(positiveString);
10 | System.out.println("Positive Long value 1 : " + longvalue1);
11 |
12 | String positiveString2 = "+12345";
13 |
14 | long longvalue2 = Long.parseLong(positiveString2);
15 | System.out.println("Positive Long value 2 : " + longvalue2);
16 |
17 | String negativeValue1 = "-12345";
18 |
19 | long longvalue3 = Long.parseLong(negativeValue1);
20 | System.out.println("Negative Long value 3 : " + longvalue3);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/add/JavaArrayToAddExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.add;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.List;
6 |
7 | public class JavaArrayToAddExample2 {
8 |
9 | public static void main(String[] args) {
10 |
11 | Integer[] array = { 10, 20, 30, 40, 30, 40 };
12 |
13 | System.out.println("Initial array values " + Arrays.toString(array));
14 |
15 | List integers = new ArrayList<>();
16 |
17 | for (int a : array) {
18 | integers.add(a);
19 | }
20 |
21 | int newValue = 50;
22 |
23 | integers.add(newValue);
24 |
25 | array = integers.toArray(array);
26 |
27 | System.out.println("Array after adding 50 value " + Arrays.toString(array));
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/concat/StreamConcatException.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.concat;
2 |
3 |
4 | import java.util.stream.Stream;
5 |
6 | public class StreamConcatException {
7 |
8 | public static void main(String[] args) {
9 |
10 | // Creating stream 1
11 | Stream stream1 = Stream.of(1, 1, 1);
12 |
13 | // Creating stream 2
14 | Stream stream2 = Stream.of(2, 2, 2);
15 |
16 | // Merging two streams in java 8 using concat() method
17 | Stream resultStream = Stream.concat(stream1, stream2);
18 |
19 | // printing stream1 values
20 | System.out.println("Stream 1 values are ");
21 | stream1.forEach(even -> System.out.println(even));
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/arrays/search/IntStreamFindStringValue.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.arrays.search;
2 |
3 | import java.util.Arrays;
4 |
5 | public class IntStreamFindStringValue {
6 |
7 | public static void main(String[] args) {
8 |
9 | String[] stringArray = new String[] { "hello", "how", "life", "is", "going" };
10 |
11 | String findString = "java";
12 |
13 | boolean isFound = false;
14 |
15 | isFound = Arrays.stream(stringArray).anyMatch(value -> value.equals(findString));
16 |
17 | if (isFound) {
18 | System.out.println("String " + findString + " is found in the array in java 8");
19 | } else {
20 | System.out.println("String " + findString + " is not found in the array in java 8");
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/numbers/math/MathAbsoluteExample4.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.numbers.math;
2 |
3 | public class MathAbsoluteExample4 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // for Wrapper Long values
8 | Long longValue = -100l;
9 | Long absValueofLong = Math.abs(longValue);
10 |
11 | System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);
12 |
13 | longValue = 200l;
14 | absValueofLong = Math.abs(longValue);
15 |
16 | System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);
17 |
18 | longValue = -0l;
19 | absValueofLong = Math.abs(longValue);
20 |
21 | System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/todouble/decimal/StringToDoubleDecimalsPlaces2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.todouble.decimal;
2 |
3 | public class StringToDoubleDecimalsPlaces2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String decimalValueInString = "9.144678376262";
8 |
9 | // convert string to double
10 |
11 | double doubleDecimalValue = Double.parseDouble(decimalValueInString);
12 |
13 | System.out.println("Double in string : "+decimalValueInString);
14 | System.out.println("Double value : "+doubleDecimalValue);
15 |
16 | String strDoubleDecimals = String.format("%.2f", doubleDecimalValue);
17 |
18 | System.out.println("Double with 2 decimal values : "+strDoubleDecimals);
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringStartsWithIndexExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | public class StringStartsWithIndexExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String s1 = "hello world";
8 |
9 | boolean isStringStartsWithHello = s1.startsWith("hello", 5);
10 |
11 | System.out.println("String starts with hello or not : "+isStringStartsWithHello);
12 | System.out.println("Phone number starts with +91 "+"+91 12345678".startsWith("+678", 2));
13 |
14 | System.out.println("javaprogramto.com starts with java : "+"javaprogramto.com".startsWith("program",4));
15 |
16 | System.out.println("starts with index : "+"phone: +91 123456789".startsWith("+91", 7));
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/threads/AnonymousRunnableExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.threads;
2 |
3 | public class AnonymousRunnableExample {
4 | public static void main(String[] args) {
5 | new Thread(new Runnable() {
6 | @Override
7 | public void run() {
8 | for (int i = 0; i <= 10; i++) {
9 | System.out.println(
10 | "Executing " + i + " thread name - " + Thread.currentThread().getName());
11 | }
12 | }
13 | }).start();
14 |
15 | for (int i = 0; i <= 10; i++) {
16 | System.out.println(
17 | "Executing " + i + " thread name - " + Thread.currentThread().getName());
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/escape/sequences/EscapeSequencesExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.escape.sequences;
2 |
3 | public class EscapeSequencesExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String newLineEscapeSequence1 = "\n";
8 | System.out.println("\\n length is "+newLineEscapeSequence1.length());
9 |
10 |
11 | String newLineEscapeSequence2 = "\n\n";
12 | System.out.println("\\n\\n length is "+newLineEscapeSequence2.length());
13 |
14 |
15 | String tabEscapeSequence1 = "\t";
16 | System.out.println("\\t length is "+tabEscapeSequence1.length());
17 |
18 |
19 | String backslashEscapeSequence1 = "\\";
20 | System.out.println("\\\\ length is "+backslashEscapeSequence1.length());
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/numbers/NumberPowerWhileLoopExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.numbers;
2 |
3 | public class NumberPowerWhileLoopExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | // create two variables to store the base and exponenet values
8 | int base = 5;
9 | int exponent = 4;
10 |
11 | // printing the input values
12 | System.out.println("Base : " + base + ", Exponent : " + exponent);
13 |
14 | // a result variable to store the output
15 | int result = 1;
16 |
17 | // Calculating the power using while loop
18 | while (exponent > 0) {
19 | result = result * base;
20 | exponent--;
21 | }
22 |
23 | // Printing the output
24 | System.out.println("Result : " + result);
25 |
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/substring/StringContainsSubstringExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.substrin;
2 |
3 | public class StringContainsSubstringExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String input = "hello world - welcome";
8 | String substring = "world";
9 | int index = input.indexOf(substring);
10 |
11 | System.out.println(index);
12 |
13 | substring = "llo";
14 | index = input.lastIndexOf(substring);
15 |
16 | System.out.println(index);
17 |
18 | substring = "java";
19 | index = input.indexOf(substring);
20 |
21 | System.out.println(index);
22 |
23 | substring = "Hello";
24 | index = input.lastIndexOf(substring);
25 |
26 | System.out.println(index);
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/tofloat/StringToFloatExample1.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.tofloat;
2 |
3 | public class StringToFloatExample1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String floatValueInString1 = "-789.123F";
8 |
9 | float floatValue1 = Float.parseFloat(floatValueInString1);
10 |
11 | System.out.println("Float value in String : " + floatValueInString1);
12 | System.out.println("String to Float value : " + floatValue1);
13 |
14 | String floatValueInString2 = "123.45";
15 |
16 | float floatValue2 = Float.parseFloat(floatValueInString2);
17 |
18 | System.out.println("Float value in String : " + floatValueInString2);
19 | System.out.println("String to Float value : " + floatValue2);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/StringHashcodeCheckExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings;
2 |
3 | public class StringHashcodeCheckExample {
4 |
5 | public static void main(String[] args) {
6 |
7 | String str1 = "FB";
8 |
9 | int hashCode1 = str1.hashCode();
10 |
11 | System.out.println("hash code of str 1 : " + hashCode1);
12 |
13 | String str2 = "FB";
14 |
15 | int hashCode2 = str2.hashCode();
16 |
17 | System.out.println("hash code of str 2 : " + hashCode2);
18 |
19 | if(hashCode1 == hashCode2){
20 | System.out.println("Hashcodes of str1 and str2 are same");
21 | } else{
22 | System.out.println("str1 and str2 hashcodes are not same");
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/strings/mix/StringsConcatenateExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.strings.mix;
2 |
3 | public class StringsConcatenateExample3 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String s1 = new String("JavaProgramTo.com");
8 | String s2 = "welcome, Java developer";
9 |
10 | StringBuffer buffer = new StringBuffer();
11 | buffer.append(s1);
12 | buffer.append(s2);
13 | String s3 = buffer.toString();
14 |
15 | System.out.println("Mixed string 1 using StringBuffer : " + s3);
16 |
17 | StringBuilder builder = new StringBuilder();
18 | builder.append("this is a ");
19 | builder.append("new string");
20 | String s4 = builder.toString();
21 |
22 | System.out.println("Mixed string 2 using StringBuilder : " + s4);
23 | }
24 | }
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/datetimeformatter/DateTimeFormatterFormatStyleExample23.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.datetimeformatter;
2 |
3 | import java.time.LocalDateTime;
4 | import java.time.format.DateTimeFormatter;
5 | import java.time.format.FormatStyle;
6 |
7 | public class DateTimeFormatterFormatStyleExample23{
8 |
9 | //DateTimeFormatterFormatStyleExample23.java
10 | public static void main(String[] args) {
11 |
12 | FormatStyle full = FormatStyle.FULL;
13 |
14 | DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(full);
15 |
16 | LocalDateTime datetime = LocalDateTime.now();
17 |
18 | String fullForm = datetime.format(formatter);
19 |
20 | System.out.println("Full date form in string : " + fullForm);
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/zoneddatetime/GetTimezoneExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.zoneddatetime;
2 |
3 | import java.time.ZoneId;
4 | import java.time.ZonedDateTime;
5 |
6 | public class GetTimezoneExample {
7 |
8 | public static void main(String[] args) {
9 | // Create object using of() method
10 | ZonedDateTime zonedtime = ZonedDateTime.of(2023, 01, 01, 01, 01, 30, 1000, ZoneId.of("America/Chicago"));
11 |
12 | // Getting timezone in string
13 | ZoneId currentZoneId = zonedtime.getZone();
14 |
15 | // print the value onto console
16 | System.out.println("Get timezone : " + currentZoneId.getId());
17 |
18 | // printing the offset value
19 | System.out.println("Timezone offset value : " + currentZoneId.getRules());
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/bytearray/CharsetDecode.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.bytearray;
2 |
3 | import java.nio.ByteBuffer;
4 | import java.nio.charset.Charset;
5 | import java.nio.charset.StandardCharsets;
6 |
7 | public class CharsetDecode {
8 |
9 | public static void main(String[] args) {
10 |
11 | // creating byte array
12 | byte[] byteArray = { 106, 97, 118, 97, 112, 114, 111, 103, 114, 109, 116, 111, 46, 99, 111, 109, 32, 63, 63, 63,
13 | 63 };
14 | // create charset object
15 | Charset charset = StandardCharsets.US_ASCII;
16 |
17 | ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
18 | // decoding string
19 | String output = charset.decode(byteBuffer).toString();
20 |
21 | System.out.println("output : " + output);
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/multidimensional/NestedArraysExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.multidimensional;
2 |
3 | import java.util.Arrays;
4 | import java.util.Collections;
5 |
6 | public class NestedArraysExample2 {
7 |
8 | public static void main(String[] args) {
9 |
10 | int[] array1 = { 0, 0 };
11 |
12 | int[] array2 = new int[5];
13 |
14 | int[] array3 = new int[5];
15 | Arrays.fill(array3, 5);
16 |
17 | Integer[] array4 = Collections.nCopies(array3.length, 0).toArray(new Integer[0]);
18 |
19 | System.out.println("array1[0] value " + array1[0]);
20 | System.out.println("array2[0] value " + array2[0]);
21 | System.out.println("array3[0] value " + array3[0]);
22 | System.out.println("array4[0] value " + array4[0]);
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/dates/currentdate/CurrentTimeStamp.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.dates.currentdate;
2 |
3 | import java.time.Instant;
4 |
5 | public class CurrentTimeStamp {
6 |
7 | public static void main(String[] args) {
8 |
9 | // getting the current timestamp from Instant.now() method
10 | Instant currentInstant = Instant.now();
11 |
12 | // getting the current time in milliseoconds from Instant
13 | long timeInMillis = currentInstant.toEpochMilli();
14 |
15 | System.out.println("Current timestamp in milli seconds "+timeInMillis);
16 |
17 | // Getting the current instant in seconds
18 | long timeInSeconds = currentInstant.getEpochSecond();
19 |
20 | System.out.println("Current timestamp in seconds : "+timeInSeconds);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/java8/streams/find/FindAnyParallelStreamExample.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.java8.streams.find;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Optional;
6 |
7 | public class FindAnyParallelStreamExample {
8 |
9 | public static void main(String[] args) {
10 |
11 | // creating a list with string values
12 | List values = Arrays.asList("A", "B", "C", "F", "D");
13 |
14 | // Creating parallel stream.
15 | Optional anyValue = values.stream().parallel().findAny();
16 |
17 | // printing the value
18 | if (anyValue.isPresent()) {
19 | System.out.println("Any value from stream : " + anyValue.get());
20 | } else {
21 | System.out.println("No value presnt in the stream.");
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/numbers/math/MathAbsoluteExample3.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.numbers.math;
2 |
3 | public class MathAbsoluteExample3 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // for Wrapper Integer values
8 | Integer integer = -100;
9 | Integer absValueofInteger = Math.abs(integer);
10 |
11 | System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);
12 |
13 | integer = 200;
14 | absValueofInteger = Math.abs(integer);
15 |
16 | System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);
17 |
18 | integer = -0;
19 | absValueofInteger = Math.abs(integer);
20 |
21 | System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/programs/strings/tofloat/StringToFloatExample2.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.programs.strings.tofloat;
2 |
3 | public class StringToFloatExample2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | String floatValueInString1 = "789.123F";
8 |
9 | Float floatValue1 = Float.valueOf(floatValueInString1);
10 |
11 | System.out.println("Float value in String : " + floatValueInString1);
12 | System.out.println("String to Wrapper Float value : " + floatValue1);
13 |
14 | String floatValueInString2 = "123.45";
15 |
16 | Float floatValue2 = Float.valueOf(floatValueInString2);
17 |
18 | System.out.println("Float value in String : " + floatValueInString2);
19 | System.out.println("String to Wrapper Float value : " + floatValue2);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javaprogramto/arrays/bytearray/CharsetEncode.java:
--------------------------------------------------------------------------------
1 | package com.javaprogramto.arrays.bytearray;
2 |
3 | import java.nio.ByteBuffer;
4 | import java.nio.charset.Charset;
5 | import java.nio.charset.StandardCharsets;
6 |
7 | public class CharsetEncode {
8 |
9 | public static void main(String[] args) {
10 |
11 | // creating string
12 | String inputStr = "javaprogrmto.com जावा";
13 |
14 | // create charset object
15 | Charset charset = StandardCharsets.US_ASCII;
16 |
17 | // encoding string
18 | ByteBuffer buffer = charset.encode(inputStr);
19 |
20 | // ByteBuffer to byte[] array
21 | byte[] array = buffer.array();
22 |
23 | // printing byte array
24 | for (int j = 0; j < array.length; j++) {
25 | System.out.print(" " + array[j]);
26 | }
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------