[BAEL-9555] - Created a core-java-modules folder
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package com.baeldung.breakcontinue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* @author Santosh
|
||||
*
|
||||
*/
|
||||
|
||||
public class BreakContinue {
|
||||
|
||||
public static int unlabeledBreak() {
|
||||
String searchName = "Wilson";
|
||||
int counter = 0;
|
||||
List<String> names = Arrays.asList("John", "Peter", "Robert", "Wilson", "Anthony", "Donald", "Richard");
|
||||
|
||||
for (String name : names) {
|
||||
counter++;
|
||||
if (name.equalsIgnoreCase(searchName)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
public static int unlabeledBreakNestedLoops() {
|
||||
String searchName = "Wilson";
|
||||
int counter = 0;
|
||||
Map<String, List<String>> nameMap = new HashMap<>();
|
||||
nameMap.put("Grade1", Arrays.asList("John", "Peter", "Robert", "Wilson"));
|
||||
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Richard", "Arnold"));
|
||||
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Stephen", "Ryan"));
|
||||
|
||||
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
|
||||
.iterator();
|
||||
Entry<String, List<String>> entry = null;
|
||||
List<String> names = null;
|
||||
while (iterator.hasNext()) {
|
||||
entry = iterator.next();
|
||||
names = entry.getValue();
|
||||
for (String name : names) {
|
||||
if (name.equalsIgnoreCase(searchName)) {
|
||||
counter++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
public static int labeledBreak() {
|
||||
String searchName = "Wilson";
|
||||
int counter = 0;
|
||||
Map<String, List<String>> nameMap = new HashMap<>();
|
||||
nameMap.put("Grade1", Arrays.asList("John", "Peter", "Robert", "Wilson"));
|
||||
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Richard", "Arnold"));
|
||||
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Stephen", "Ryan"));
|
||||
|
||||
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
|
||||
.iterator();
|
||||
Entry<String, List<String>> entry = null;
|
||||
List<String> names = null;
|
||||
compare:
|
||||
while (iterator.hasNext()) {
|
||||
entry = iterator.next();
|
||||
names = entry.getValue();
|
||||
for (String name : names) {
|
||||
if (name.equalsIgnoreCase(searchName)) {
|
||||
counter++;
|
||||
break compare;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
public static int unlabeledContinue() {
|
||||
String searchName = "Wilson";
|
||||
int counter = 0;
|
||||
Map<String, List<String>> nameMap = new HashMap<>();
|
||||
nameMap.put("Grade1", Arrays.asList("John", "Wilson", "Robert", "Wilson"));
|
||||
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Wilson", "Arnold"));
|
||||
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Wilson", "Ryan"));
|
||||
|
||||
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
|
||||
.iterator();
|
||||
Entry<String, List<String>> entry = null;
|
||||
List<String> names = null;
|
||||
while (iterator.hasNext()) {
|
||||
entry = iterator.next();
|
||||
names = entry.getValue();
|
||||
for (String name : names) {
|
||||
if (!name.equalsIgnoreCase(searchName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
public static int labeledContinue() {
|
||||
String searchName = "Wilson";
|
||||
int counter = 0;
|
||||
Map<String, List<String>> nameMap = new HashMap<>();
|
||||
nameMap.put("Grade1", Arrays.asList("John", "Wilson", "Robert", "Wilson"));
|
||||
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Wilson", "Arnold"));
|
||||
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Wilson", "Ryan"));
|
||||
|
||||
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
|
||||
.iterator();
|
||||
Entry<String, List<String>> entry = null;
|
||||
List<String> names = null;
|
||||
compare:
|
||||
while (iterator.hasNext()) {
|
||||
entry = iterator.next();
|
||||
names = entry.getValue();
|
||||
for (String name : names) {
|
||||
if (name.equalsIgnoreCase(searchName)) {
|
||||
counter++;
|
||||
continue compare;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.enums;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Pizza {
|
||||
|
||||
private static EnumSet<PizzaStatusEnum> deliveredPizzaStatuses = EnumSet.of(PizzaStatusEnum.DELIVERED);
|
||||
|
||||
private PizzaStatusEnum status;
|
||||
|
||||
public enum PizzaStatusEnum {
|
||||
ORDERED(5) {
|
||||
@Override
|
||||
public boolean isOrdered() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
READY(2) {
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
DELIVERED(0) {
|
||||
@Override
|
||||
public boolean isDelivered() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private int timeToDelivery;
|
||||
|
||||
public boolean isOrdered() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isDelivered() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getTimeToDelivery() {
|
||||
return timeToDelivery;
|
||||
}
|
||||
|
||||
PizzaStatusEnum(int timeToDelivery) {
|
||||
this.timeToDelivery = timeToDelivery;
|
||||
}
|
||||
}
|
||||
|
||||
public PizzaStatusEnum getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(PizzaStatusEnum status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public boolean isDeliverable() {
|
||||
return this.status.isReady();
|
||||
}
|
||||
|
||||
public void printTimeToDeliver() {
|
||||
System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days");
|
||||
}
|
||||
|
||||
public static List<Pizza> getAllUndeliveredPizzas(List<Pizza> input) {
|
||||
return input.stream().filter((s) -> !deliveredPizzaStatuses.contains(s.getStatus())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static EnumMap<PizzaStatusEnum, List<Pizza>> groupPizzaByStatus(List<Pizza> pzList) {
|
||||
return pzList.stream().collect(Collectors.groupingBy(Pizza::getStatus, () -> new EnumMap<>(PizzaStatusEnum.class), Collectors.toList()));
|
||||
}
|
||||
|
||||
public void deliver() {
|
||||
if (isDeliverable()) {
|
||||
PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this);
|
||||
this.setStatus(PizzaStatusEnum.DELIVERED);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.enums;
|
||||
|
||||
public enum PizzaDeliveryStrategy {
|
||||
EXPRESS {
|
||||
@Override
|
||||
public void deliver(Pizza pz) {
|
||||
System.out.println("Pizza will be delivered in express mode");
|
||||
}
|
||||
},
|
||||
NORMAL {
|
||||
@Override
|
||||
public void deliver(Pizza pz) {
|
||||
System.out.println("Pizza will be delivered in normal mode");
|
||||
}
|
||||
};
|
||||
|
||||
public abstract void deliver(Pizza pz);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.enums;
|
||||
|
||||
public enum PizzaDeliverySystemConfiguration {
|
||||
INSTANCE;
|
||||
|
||||
PizzaDeliverySystemConfiguration() {
|
||||
// Do the configuration initialization which
|
||||
// involves overriding defaults like delivery strategy
|
||||
}
|
||||
|
||||
private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL;
|
||||
|
||||
public static PizzaDeliverySystemConfiguration getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public PizzaDeliveryStrategy getDeliveryStrategy() {
|
||||
return deliveryStrategy;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.generics;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Building {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
|
||||
|
||||
public void paint() {
|
||||
LOGGER.info("Painting Building");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.generics;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Generics {
|
||||
|
||||
// definition of a generic method
|
||||
public static <T> List<T> fromArrayToList(T[] a) {
|
||||
return Arrays.stream(a).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// definition of a generic method
|
||||
public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
|
||||
return Arrays.stream(a).map(mapperFunction).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// example of a generic method that has Number as an upper bound for T
|
||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||
return Arrays.stream(a).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// example of a generic method with a wild card, this method can be used
|
||||
// with a list of any subtype of Building
|
||||
public static void paintAllBuildings(List<? extends Building> buildings) {
|
||||
buildings.forEach(Building::paint);
|
||||
}
|
||||
|
||||
public static List<Integer> createList(int a) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(a);
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.generics;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class House extends Building {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(House.class);
|
||||
|
||||
public void paint() {
|
||||
LOGGER.info("Painting House");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.initializationguide;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class User implements Serializable, Cloneable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
static String forum;
|
||||
private String name;
|
||||
private int id;
|
||||
|
||||
{
|
||||
id = 0;
|
||||
System.out.println("Instance Initializer");
|
||||
}
|
||||
|
||||
static {
|
||||
forum = "Java";
|
||||
System.out.println("Static Initializer");
|
||||
}
|
||||
|
||||
public User(String name, int id) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public User() {
|
||||
System.out.println("Constructor");
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.keyword;
|
||||
|
||||
public class Circle extends Round implements Shape {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.keyword;
|
||||
|
||||
public class Ring extends Round {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.keyword;
|
||||
|
||||
public class Round {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.keyword;
|
||||
|
||||
public interface Shape {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.keyword;
|
||||
|
||||
public class Triangle implements Shape {
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.loops;
|
||||
|
||||
public class InfiniteLoops {
|
||||
|
||||
public void infiniteLoopUsingWhile() {
|
||||
while (true) {
|
||||
System.out.println("Infinite loop using while");
|
||||
}
|
||||
}
|
||||
|
||||
public void infiniteLoopUsingFor() {
|
||||
for (;;) {
|
||||
System.out.println("Infinite loop using for");
|
||||
}
|
||||
}
|
||||
|
||||
public void infiniteLoopUsingDoWhile() {
|
||||
do {
|
||||
System.out.println("Infinite loop using do-while");
|
||||
} while (true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.loops;
|
||||
|
||||
public class LoopsInJava {
|
||||
|
||||
public int[] simple_for_loop() {
|
||||
int[] arr = new int[5];
|
||||
for (int i = 0; i < 5; i++) {
|
||||
arr[i] = i;
|
||||
System.out.println("Simple for loop: i - " + i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public int[] enhanced_for_each_loop() {
|
||||
int[] intArr = { 0, 1, 2, 3, 4 };
|
||||
int[] arr = new int[5];
|
||||
for (int num : intArr) {
|
||||
arr[num] = num;
|
||||
System.out.println("Enhanced for-each loop: i - " + num);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public int[] while_loop() {
|
||||
int i = 0;
|
||||
int[] arr = new int[5];
|
||||
while (i < 5) {
|
||||
arr[i] = i;
|
||||
System.out.println("While loop: i - " + i++);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public int[] do_while_loop() {
|
||||
int i = 0;
|
||||
int[] arr = new int[5];
|
||||
do {
|
||||
arr[i] = i;
|
||||
System.out.println("Do-While loop: i - " + i++);
|
||||
} while (i < 5);
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.switchstatement;
|
||||
|
||||
public class SwitchStatement {
|
||||
|
||||
public String exampleOfIF(String animal) {
|
||||
|
||||
String result;
|
||||
|
||||
if (animal.equals("DOG") || animal.equals("CAT")) {
|
||||
result = "domestic animal";
|
||||
} else if (animal.equals("TIGER")) {
|
||||
result = "wild animal";
|
||||
} else {
|
||||
result = "unknown animal";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String exampleOfSwitch(String animal) {
|
||||
|
||||
String result;
|
||||
|
||||
switch (animal) {
|
||||
case "DOG":
|
||||
case "CAT":
|
||||
result = "domestic animal";
|
||||
break;
|
||||
case "TIGER":
|
||||
result = "wild animal";
|
||||
break;
|
||||
default:
|
||||
result = "unknown animal";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String forgetBreakInSwitch(String animal) {
|
||||
|
||||
String result;
|
||||
|
||||
switch (animal) {
|
||||
|
||||
case "DOG":
|
||||
System.out.println("domestic animal");
|
||||
result = "domestic animal";
|
||||
|
||||
default:
|
||||
System.out.println("unknown animal");
|
||||
result = "unknown animal";
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String constantCaseValue(String animal) {
|
||||
|
||||
String result = "";
|
||||
|
||||
final String dog = "DOG";
|
||||
|
||||
switch (animal) {
|
||||
|
||||
case dog:
|
||||
result = "domestic animal";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' gc() which suggests
|
||||
* JVM for garbage collection. But the usage below demonstrates how the
|
||||
* method can be used.
|
||||
*/
|
||||
public class ChatWindow {
|
||||
public void windowStateChanged(WindowEvent event) {
|
||||
if (event.getNewState() == WindowEvent.WINDOW_DEACTIVATED ) {
|
||||
System.gc(); // if it ends up running, great!
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class DateTimeService {
|
||||
|
||||
// One hour from now
|
||||
public long nowPlusOneHour() {
|
||||
return System.currentTimeMillis() + 3600 * 1000L;
|
||||
}
|
||||
|
||||
// Human-readable format
|
||||
public String nowPrettyPrinted() {
|
||||
return new Date(System.currentTimeMillis()).toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
public class EnvironmentVariables {
|
||||
public String getPath() {
|
||||
return System.getenv("PATH");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' standard error stream
|
||||
* methods to show output on screen. Also unit-tests in CI environments
|
||||
* don't have console output for user to see messages. But the usage below
|
||||
* demonstrates how the methods can be used.
|
||||
*/
|
||||
public class SystemErrDemo {
|
||||
public static void main(String[] args) {
|
||||
// Print without 'hitting' return
|
||||
System.err.print("some inline error message");
|
||||
|
||||
// Print and then 'hit' return
|
||||
System.err.println("an error message having new line at the end");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' exit() which will
|
||||
* exit the JVM. Also unit-tests in CI environments are not meant to
|
||||
* exit unit tests like that. But the usage below demonstrates how the
|
||||
* method can be used.
|
||||
*/
|
||||
public class SystemExitDemo {
|
||||
public static void main(String[] args) {
|
||||
boolean error = false;
|
||||
|
||||
// do something and set error value
|
||||
|
||||
if (error) {
|
||||
System.exit(1); // error case exit
|
||||
} else {
|
||||
System.exit(0); // normal case exit
|
||||
}
|
||||
|
||||
// Will not do anything after exit()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' standard output stream
|
||||
* methods to show output on screen. Also unit-tests in CI environments
|
||||
* don't have console output for user to see messages. But the usage below
|
||||
* demonstrates how the methods can be used.
|
||||
*/
|
||||
public class SystemOutDemo {
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
// Print without 'hitting' return
|
||||
System.out.print("some inline message");
|
||||
|
||||
// Print and then 'hit' return
|
||||
System.out.println("a message having new line at the end");
|
||||
|
||||
// Changes output stream to send messages to file.
|
||||
System.setOut(new PrintStream("file.txt"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Console;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' standard input stream
|
||||
* methods to read text from user. Also unit-tests in CI environments
|
||||
* don't have console input for user to type in text. But the usage below
|
||||
* demonstrates how the methods can be used.
|
||||
*/
|
||||
public class UserCredentials {
|
||||
|
||||
public String readUsername(int length) throws IOException {
|
||||
byte[] name = new byte[length];
|
||||
System.in.read(name, 0, length); // by default, from the console
|
||||
return new String(name);
|
||||
}
|
||||
|
||||
public String readUsername() throws IOException {
|
||||
BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(System.in));
|
||||
return reader.readLine();
|
||||
}
|
||||
|
||||
public String readUsernameWithPrompt() {
|
||||
Console console = System.console();
|
||||
|
||||
return console == null ? null : // Console not available
|
||||
console.readLine("%s", "Enter your name: ");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user