Merge branch 'eugenp:master' into master

This commit is contained in:
sebx59 2022-05-16 16:25:29 +02:00 committed by GitHub
commit dcb4d1032c
538 changed files with 6231 additions and 3223 deletions

View File

@ -31,6 +31,6 @@ public class BalancedBracketsUsingDeque {
}
}
return true;
return deque.isEmpty();
}
}

View File

@ -49,6 +49,12 @@ public class BalancedBracketsUsingDequeUnitTest {
assertThat(result).isFalse();
}
@Test
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{}(");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingDeque.isBalanced("{[()]}");

View File

@ -49,6 +49,12 @@ public class BalancedBracketsUsingStringUnitTest {
assertThat(result).isFalse();
}
@Test
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("{{}(");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingString.isBalanced("{[()]}");

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-poi-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
@ -25,5 +25,4 @@
<poi.version>5.2.0</poi.version>
</properties>
</project>
</project>

View File

@ -9,3 +9,4 @@ This module contains articles about Apache Spark
- [Machine Learning with Spark MLlib](https://www.baeldung.com/spark-mlib-machine-learning)
- [Introduction to Spark Graph Processing with GraphFrames](https://www.baeldung.com/spark-graph-graphframes)
- [Apache Spark: Differences between Dataframes, Datasets and RDDs](https://www.baeldung.com/java-spark-dataframe-dataset-rdd)
- [Spark DataFrame](https://www.baeldung.com/spark-dataframes)

View File

@ -0,0 +1,9 @@
id,zoneId,FIRST_NAME,MIDDLE_NAME,LAST_NAME,CITY,gender,transaction_date,transaction_amount
1,EU11,Ana1,A,MN, London,Female,1/2/2018,5200
2,EU12,Ana2,A,MN, London,Female,29/3/2018,1000
3,EU13,Jack1,Josh,MN, London,Male,19/6/2018,9600
4,EU14,Jack2,Josh,MN, London,Male,9/9/2018,1000
5,EU15,Nick1,H,Dee,London,Male,9/6/2021,3000
6,EU16,Nick2,H,Dee,London,Male,27/9/2021,500
7,EU17,Nick3,H,Dee,London,Male,8/12/2021,500
8,EU18,Sara1,H,Dee,London,Female,7/4/2021,2000
1 id zoneId FIRST_NAME MIDDLE_NAME LAST_NAME CITY gender transaction_date transaction_amount
2 1 EU11 Ana1 A MN London Female 1/2/2018 5200
3 2 EU12 Ana2 A MN London Female 29/3/2018 1000
4 3 EU13 Jack1 Josh MN London Male 19/6/2018 9600
5 4 EU14 Jack2 Josh MN London Male 9/9/2018 1000
6 5 EU15 Nick1 H Dee London Male 9/6/2021 3000
7 6 EU16 Nick2 H Dee London Male 27/9/2021 500
8 7 EU17 Nick3 H Dee London Male 8/12/2021 500
9 8 EU18 Sara1 H Dee London Female 7/4/2021 2000

View File

@ -0,0 +1,114 @@
[
{
"zoneId": "LONDON",
"customerId": 1,
"gender": "Female",
"name": "jane austin",
"contact": {
"street_Address": "XYZ Road",
"address2": "house 47",
"zipcode": "MK1110",
"county": "DCounty",
"phone_number": "(919) 403-0025",
"customer_city": "CityA"
},
"transaction_date": "2021-02-05",
"transaction_amount": 15000
},
{
"zoneId": "LONDON",
"customerId": 2,
"gender": "Male",
"name": "Jack Trevor",
"contact": {
"street_Address": " kingfisher road",
"address2": "SUITE 110",
"zipcode": "HM1190",
"county": "CCounty",
"phone_number": "(919) 403-0025",
"customer_city": "CityB"
},
"transaction_date": "2021-02-05",
"transaction_amount": 12000
},
{
"zoneId": "LONDON",
"customerId": 11,
"gender": "Female",
"name": "jane1 austin",
"contact": {
"street_Address": "B Road",
"address2": "house 47",
"zipcode": "MK1110",
"county": "BCounty",
"phone_number": "(919) 403-0025",
"customer_city": "CityA"
},
"transaction_date": "2021-02-05",
"transaction_amount": 1000
},
{
"zoneId": "LONDON",
"customerId": 21,
"gender": "Male",
"name": "Jack1 Trevor",
"contact": {
"street_Address": " A road",
"address2": "SUITE 777",
"zipcode": "AZ890",
"county": "ACounty",
"phone_number": "(919) 403-0025",
"customer_city": "CityB"
},
"transaction_date": "2021-02-05",
"transaction_amount": 1000
},
{
"zoneId": "Wales",
"customerId": 3,
"gender": "Male",
"name": "John Jack",
"contact": {
"street_Address": "sunny croft",
"address2": "SUITE 1",
"zipcode": "SN1030",
"county": "bucks",
"phone_number": "(919) 403-0025",
"customer_city": "Cardiff"
},
"transaction_date": "2018-02-05",
"transaction_amount": 5000
},
{
"zoneId": "LONDON",
"customerId": 4,
"gender": "Male",
"name": "Jack Trevor",
"contact": {
"street_Address": " kingfisher road",
"address2": "SUITE 110",
"zipcode": "HM1190",
"county": "Hampshire",
"phone_number": "(919) 403-0025",
"customer_city": "CityB"
},
"transaction_date": "2021-02-05",
"transaction_amount": 500
},
{
"zoneId": "Wales",
"customerId": 5,
"gender": "Male",
"name": "John Jack",
"contact": {
"street_Address": "sunny croft",
"address2": "SUITE 1",
"zipcode": "SN1030",
"county": "ECounty",
"phone_number": "(919) 403-0025",
"customer_city": "Cardiff"
},
"transaction_date": "2018-01-25",
"transaction_amount": 500
}
]

View File

@ -0,0 +1,20 @@
[
{
"id": "1",
"gender": "Female",
"name": "Jo",
"transaction_amount": 200
},
{
"id":"2",
"gender": "Male",
"name": "Mike",
"transaction_amount": 500
},
{
"id": "3",
"gender": "Male",
"name": "Dave",
"transaction_amount": 5000
}
]

View File

@ -0,0 +1,33 @@
version: "3"
services:
postgres:
image: postgres:12.3-alpine
restart: always
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
expose:
- 5432
ports:
- 5432:5432
command: -p 5432
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
# - postgres:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4:4.23
environment:
PGADMIN_DEFAULT_EMAIL: admin@pgadmin.com
PGADMIN_DEFAULT_PASSWORD: password
PGADMIN_LISTEN_PORT: 80
ports:
- 15432:80
volumes:
- pgadmin:/var/lib/pgadmin
depends_on:
- postgres
volumes:
pgadmin:

1
apache-spark/docker/init.sql Executable file
View File

@ -0,0 +1 @@
CREATE DATABASE customerdb;

View File

@ -20,37 +20,31 @@
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${org.apache.spark.spark-core.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>${org.apache.spark.spark-sql.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.11</artifactId>
<version>${org.apache.spark.spark-graphx.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>graphframes</groupId>
<artifactId>graphframes</artifactId>
<version>${graphframes.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>${org.apache.spark.spark-streaming.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>${org.apache.spark.spark-mllib.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
@ -67,6 +61,11 @@
<artifactId>spark-cassandra-connector-java_2.11</artifactId>
<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.version}</version>
</dependency>
</dependencies>
<build>
@ -108,6 +107,7 @@
<org.apache.spark.spark-streaming-kafka.version>2.4.8</org.apache.spark.spark-streaming-kafka.version>
<com.datastax.spark.spark-cassandra-connector.version>2.5.2</com.datastax.spark.spark-cassandra-connector.version>
<com.datastax.spark.spark-cassandra-connector-java.version>1.6.0-M1</com.datastax.spark.spark-cassandra-connector-java.version>
<postgres.version>42.3.3</postgres.version>
</properties>
</project>

View File

@ -0,0 +1,52 @@
package com.baeldung.dataframes;
public class Customer {
String id;
String name;
String gender;
int transaction_amount;
public Customer() {
}
public Customer(String id, String name, String gender, int transaction_amount) {
this.id = id;
this.name = name;
this.gender = gender;
this.transaction_amount = transaction_amount;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getTransaction_amount() {
return transaction_amount;
}
public void setTransaction_amount(int transaction_amount) {
this.transaction_amount = transaction_amount;
}
}

View File

@ -0,0 +1,129 @@
package com.baeldung.dataframes;
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.column;
import static org.apache.spark.sql.functions.concat;
import static org.apache.spark.sql.functions.lit;
import java.util.Properties;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SaveMode;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.functions;
public class CustomerDataAggregationPipeline {
private static final SparkSession SPARK_SESSION = SparkDriver.getSparkSession();
private final Properties dbProperties;
public CustomerDataAggregationPipeline(Properties properties) {
dbProperties = properties;
}
public static void main(String[] args) {
// replace with actual DB properties
Properties dbProps = new Properties();
dbProps.setProperty("connectionURL", "jdbc:postgresql://localhost:5432/customerdb");
dbProps.setProperty("driver", "org.postgresql.Driver");
dbProps.setProperty("user", "postgres");
dbProps.setProperty("password", "postgres");
new CustomerDataAggregationPipeline(dbProps).run();
}
public void run() {
Dataset<Row> ebayDFRaw = ingestCustomerDataFromEbay();
Dataset<Row> ebayDf = normalizeCustomerDataFromEbay(ebayDFRaw);
Dataset<Row> amazonDFRaw = ingestCustomerDataFromAmazon();
Dataset<Row> amazonDf = normalizeCustomerDataFromAmazon(amazonDFRaw);
Dataset<Row> combineDataframes = combineDataframes(ebayDf, amazonDf);
Dataset<Row> rowDataset = aggregateYearlySalesByGender(combineDataframes);
exportData(rowDataset);
}
private static Dataset<Row> ingestCustomerDataFromAmazon() {
return SPARK_SESSION.read()
.format("csv")
.option("header", "true")
.schema(SchemaFactory.customerSchema())
.option("dateFormat", "m/d/YYYY")
.load("data/customerData.csv");
}
private static Dataset<Row> ingestCustomerDataFromEbay() {
return SPARK_SESSION.read()
.format("org.apache.spark.sql.execution.datasources.json.JsonFileFormat")
.option("multiline", true)
.load("data/customerData.json");
}
private static Dataset<Row> combineDataframes(Dataset<Row> df1, Dataset<Row> df2) {
return df1.unionByName(df2);
}
private static Dataset<Row> normalizeCustomerDataFromEbay(Dataset<Row> rawDataset) {
Dataset<Row> transformedDF = rawDataset.withColumn("id", concat(rawDataset.col("zoneId"), lit("-"), rawDataset.col("customerId")))
.drop(column("customerId"))
.withColumn("source", lit("ebay"))
.withColumn("city", rawDataset.col("contact.customer_city"))
.drop(column("contact"))
.drop(column("zoneId"))
.withColumn("year", functions.year(col("transaction_date")))
.drop("transaction_date")
.withColumn("firstName", functions.split(column("name"), " ")
.getItem(0))
.withColumn("lastName", functions.split(column("name"), " ")
.getItem(1))
.drop(column("name"));
print(transformedDF);
return transformedDF;
}
private static Dataset<Row> normalizeCustomerDataFromAmazon(Dataset<Row> rawDataset) {
Dataset<Row> transformedDF = rawDataset.withColumn("id", concat(rawDataset.col("zoneId"), lit("-"), rawDataset.col("id")))
.withColumn("source", lit("amazon"))
.withColumnRenamed("CITY", "city")
.withColumnRenamed("PHONE_NO", "contactNo")
.withColumnRenamed("POSTCODE", "postCode")
.withColumnRenamed("FIRST_NAME", "firstName")
.drop(column("MIDDLE_NAME"))
.drop(column("zoneId"))
.withColumnRenamed("LAST_NAME", "lastName")
.withColumn("year", functions.year(col("transaction_date")))
.drop("transaction_date");
print(transformedDF);
return transformedDF;
}
private static Dataset<Row> aggregateYearlySalesByGender(Dataset<Row> dataset) {
Dataset<Row> aggDF = dataset.groupBy(column("year"), column("source"), column("gender"))
.sum("transaction_amount")
.withColumnRenamed("sum(transaction_amount)", "annual_spending")
.orderBy(col("year").asc(), col("annual_spending").desc());
print(aggDF);
return aggDF;
}
private static void print(Dataset<Row> aggDs) {
aggDs.show();
aggDs.printSchema();
}
private void exportData(Dataset<Row> dataset) {
String connectionURL = dbProperties.getProperty("connectionURL");
dataset.write()
.mode(SaveMode.Overwrite)
.jdbc(connectionURL, "customer", dbProperties);
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.dataframes;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
public class CustomerToDataFrameConverterApp {
private static final List<Customer> CUSTOMERS = Arrays.asList(
aCustomerWith("01", "jo", "Female", 2000),
aCustomerWith("02", "jack", "Male", 1200)
);
public static void main(String[] args) {
Dataset<Row> dataFrame = convertAfterMappingRows(CUSTOMERS);
print(dataFrame);
Dataset<Row> customerDF = convertToDataFrameWithNoChange();
print(customerDF);
}
public static Dataset<Row> convertToDataFrameWithNoChange() {
return SparkDriver.getSparkSession().createDataFrame(CUSTOMERS, Customer.class);
}
public static Dataset<Row> convertAfterMappingRows(List<Customer> customer) {
List<Row> rows = customer.stream()
.map(c -> new CustomerToRowMapper().call(c))
.collect(Collectors.toList());
return SparkDriver.getSparkSession()
.createDataFrame(rows, SchemaFactory.minimumCustomerDataSchema());
}
private static Customer aCustomerWith(String id, String name, String gender, int amount) {
return new Customer(id, name, gender, amount);
}
private static void print(Dataset<Row> dataFrame) {
dataFrame.printSchema();
dataFrame.show();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.dataframes;
import org.apache.commons.lang3.StringUtils;
import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
public class CustomerToRowMapper implements MapFunction<Customer, Row> {
@Override
public Row call(Customer customer) {
Row row = RowFactory.create(
customer.getId(), customer.getName().toUpperCase(),
StringUtils.substring(customer.getGender(),0, 1),
customer.getTransaction_amount());
return row;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.dataframes;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.Row;
public class DataFrameToCustomerConverterApp {
public static void main(String[] args) {
Dataset<Row> df = SparkDriver.getSparkSession()
.read()
.format("org.apache.spark.sql.execution.datasources.json.JsonFileFormat")
.option("multiline", true)
.load("data/minCustomerData.json");
df.show();
df.printSchema();
Dataset<Customer> customerDS = df.map(new RowToCustomerMapper(), Encoders.bean(Customer. class));
customerDS.show();
customerDS.printSchema();
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.dataframes;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
public class DataSetToDataFrameConverterApp {
private static final SparkSession SPARK_SESSION = SparkDriver.getSparkSession();
public static void main(String[] args) {
Dataset<Customer> customerDataset = convertToDataSetFromPOJO();
Dataset<Row> customerDataFrame = customerDataset.toDF();
print(customerDataFrame);
List<String> names = getNames();
Dataset<String> namesDataset = convertToDataSetFromStrings(names);
Dataset<Row> namesDataFrame = namesDataset.toDF();
print(namesDataFrame);
}
private static Dataset<String> convertToDataSetFromStrings(List<String> names) {
return SPARK_SESSION.createDataset(names, Encoders.STRING());
}
private static Dataset<Customer> convertToDataSetFromPOJO() {
return SPARK_SESSION.createDataset(CUSTOMERS, Encoders.bean(Customer.class));
}
private static final List<Customer> CUSTOMERS = Arrays.asList(
aCustomerWith("01", "jo", "Female", 2000),
aCustomerWith("02", "jack", "Female", 1200),
aCustomerWith("03", "ash", "male", 2000),
aCustomerWith("04", "emma", "Female", 2000)
);
private static List<String> getNames() {
return CUSTOMERS.stream()
.map(Customer::getName)
.collect(Collectors.toList());
}
private static void print(Dataset<Row> df) {
df.show();
df.printSchema();
}
private static Customer aCustomerWith(String id, String name, String gender, int amount) {
return new Customer(id, name, gender, amount);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.dataframes;
import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.Row;
class RowToCustomerMapper implements MapFunction<Row, Customer> {
@Override
public Customer call(Row row) {
Customer customer = new Customer();
customer.setId(row.getAs("id"));
customer.setName(row.getAs("name"));
customer.setGender(row.getAs("gender"));
customer.setTransaction_amount(Math.toIntExact(row.getAs("transaction_amount")));
return customer;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.dataframes;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
public class SchemaFactory {
public static StructType customerSchema() {
return DataTypes.createStructType(
new StructField[] { DataTypes.createStructField("id", DataTypes.IntegerType, false),
DataTypes.createStructField("zoneId", DataTypes.StringType, false),
DataTypes.createStructField("FIRST_NAME", DataTypes.StringType, false),
DataTypes.createStructField("MIDDLE_NAME", DataTypes.StringType, false),
DataTypes.createStructField("LAST_NAME", DataTypes.StringType, false),
DataTypes.createStructField("CITY", DataTypes.StringType, false),
DataTypes.createStructField("gender", DataTypes.StringType, false),
DataTypes.createStructField("transaction_date", DataTypes.DateType, false),
DataTypes.createStructField("transaction_amount", DataTypes.IntegerType, false)
});
}
public static StructType minimumCustomerDataSchema() {
return DataTypes.createStructType(new StructField[] {
DataTypes.createStructField("id", DataTypes.StringType, true),
DataTypes.createStructField("name", DataTypes.StringType, true),
DataTypes.createStructField("gender", DataTypes.StringType, true),
DataTypes.createStructField("transaction_amount", DataTypes.IntegerType, true)
});
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.dataframes;
import java.io.Serializable;
import org.apache.spark.sql.SparkSession;
public class SparkDriver implements Serializable {
public static SparkSession getSparkSession() {
return SparkSession.builder()
.appName("Customer Aggregation pipeline")
.master("local")
.getOrCreate();
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.dataframes;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class CustomerDataAggregationPipelineLiveTest {
private static Connection conn;
@BeforeAll
static void beforeAll() throws SQLException {
DriverManager.registerDriver(new org.postgresql.Driver());
String dbURL1 = "jdbc:postgresql://localhost:5432/customerdb";
conn = DriverManager.getConnection(dbURL1, "postgres", "postgres");
String sql = "drop table if exists customer";
PreparedStatement statement = conn.prepareStatement(sql);
statement.executeUpdate();
}
@Test
void givenCSVAndJSON_whenRun_thenStoresAggregatedDataFrameInDB() throws Exception {
Properties dbProps = new Properties();
dbProps.setProperty("connectionURL", "jdbc:postgresql://localhost:5432/customerdb");
dbProps.setProperty("driver", "org.postgresql.Driver");
dbProps.setProperty("user", "postgres");
dbProps.setProperty("password", "postgres");
CustomerDataAggregationPipeline pipeline = new CustomerDataAggregationPipeline(dbProps);
pipeline.run();
String allCustomersSql = "Select count(*) from customer";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(allCustomersSql);
resultSet.next();
int count = resultSet.getInt(1);
assertEquals(7, count);
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.dataframes;
import static com.baeldung.dataframes.CustomerToDataFrameConverterApp.convertAfterMappingRows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.junit.jupiter.api.Test;
class CustomerToDataFrameConverterAppUnitTest {
@Test
void givenCustomers_whenConvertAfterMappingRows_thenConvertsToDataSet() {
List<Customer> customers = Arrays.asList(
new Customer("01", "jo", "Female", 2000),
new Customer("02", "jack", "Male", 1200)
);
Dataset<Row> customerDF = convertAfterMappingRows(customers);
List<Row> rows = customerDF.collectAsList();
Row row1 = rows.get(0);
Row row2 = rows.get(1);
assertEquals("01", row1.get(0));
assertEquals( "JO", row1.get(1));
assertEquals( "F", row1.get(2));
assertEquals( 2000, row1.get(3));
assertEquals("02", row2.get(0));
assertEquals( "JACK", row2.get(1));
assertEquals( "M", row2.get(2));
assertEquals( 1200, row2.get(3));
}
@Test
void givenCustomers_whenConvertWithNoChange_thenConvertsToDataSet() {
List<Customer> customers = Arrays.asList(
new Customer("01", "jo", "Female", 2000),
new Customer("02", "jack", "Male", 1200)
);
Dataset<Row> customerDF = CustomerToDataFrameConverterApp.convertToDataFrameWithNoChange();
List<Row> rows = customerDF.collectAsList();
Row row1 = rows.get(0);
Row row2 = rows.get(1);
assertEquals("01", row1.getAs("id"));
assertEquals( "jo", row1.getAs("name"));
assertEquals( "Female", row1.getAs("gender"));
assertEquals( 2000, (int)row1.getAs("transaction_amount"));
assertEquals("02", row2.getAs("id"));
assertEquals( "jack", row2.getAs("name"));
assertEquals( "Male", row2.getAs("gender"));
assertEquals( 1200, (int)row2.getAs("transaction_amount"));
}
}

View File

@ -12,21 +12,29 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Tuple2;
public class ActionsUnitTest {
public static final Logger LOG = LoggerFactory.getLogger(ActionsUnitTest.class);
private static JavaRDD<String> tourists;
private static JavaSparkContext sc;
public static final String COMMA_DELIMITER = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
@BeforeClass
public static void init() {
SparkConf conf = new SparkConf().setAppName("reduce")
.setMaster("local[*]");
SparkConf conf = new SparkConf()
.setAppName("reduce")
.setMaster("local[*]")
.set("spark.driver.allowMultipleContexts", "true");
sc = new JavaSparkContext(conf);
tourists = sc.textFile("data/Tourist.csv").filter(line -> !line.startsWith("Region"));
}
@AfterClass
public static void cleanup() {
sc.close();
@ -40,11 +48,11 @@ public class ActionsUnitTest {
})
.distinct();
Long numberOfCountries = countries.count();
System.out.println("Count: " + numberOfCountries);
LOG.debug("Count: {}", numberOfCountries);
assertEquals(Long.valueOf(220), numberOfCountries);
}
@Test
public void whenReduceByKeySum_thenTotalValuePerKey() {
JavaRDD<String> touristsExpenditure = tourists.filter(line -> line.split(COMMA_DELIMITER)[3].contains("expenditure"));
@ -53,10 +61,12 @@ public class ActionsUnitTest {
String[] columns = line.split(COMMA_DELIMITER);
return new Tuple2<>(columns[1], Double.valueOf(columns[6]));
});
List<Tuple2<String, Double>> totalByCountry = expenditurePairRdd.reduceByKey((x, y) -> x + y)
.collect();
System.out.println("Total per Country: " + totalByCountry);
List<Tuple2<String, Double>> totalByCountry = expenditurePairRdd
.reduceByKey(Double::sum)
.collect();
LOG.debug("Total per Country: {}", totalByCountry);
for(Tuple2<String, Double> tuple : totalByCountry) {
if (tuple._1.equals("Mexico")) {
assertEquals(Double.valueOf(99164), tuple._2);

View File

@ -39,8 +39,10 @@ public class DataFrameUnitTest {
@Test
public void whenSelectSpecificColumns_thenColumnsFiltered() {
Dataset<Row> selectedData = data.select(col("country"), col("year"), col("value"));
selectedData.show();
// uncomment to see table
// selectedData.show();
List<String> resultList = Arrays.asList(selectedData.columns());
assertTrue(resultList.contains("country"));
assertTrue(resultList.contains("year"));
@ -52,22 +54,26 @@ public class DataFrameUnitTest {
@Test
public void whenFilteringByCountry_thenCountryRecordsSelected() {
Dataset<Row> filteredData = data.filter(col("country").equalTo("Mexico"));
filteredData.show();
// uncomment to see table
// filteredData.show();
filteredData.foreach(record -> {
assertEquals("Mexico", record.get(1));
});
}
@Test
public void whenGroupCountByCountry_thenContryTotalRecords() {
Dataset<Row> recordsPerCountry = data.groupBy(col("country"))
.count();
recordsPerCountry.show();
// uncomment to see table
// recordsPerCountry.show();
Dataset<Row> filteredData = recordsPerCountry.filter(col("country").equalTo("Sweden"));
assertEquals(new Long(12), filteredData.first()
assertEquals(12L, filteredData.first()
.get(1));
}

View File

@ -3,6 +3,7 @@ package com.baeldung.differences.rdd;
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.sum;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.apache.spark.api.java.function.FilterFunction;
import org.apache.spark.sql.DataFrameReader;
@ -29,8 +30,8 @@ public class DatasetUnitTest {
DataFrameReader dataFrameReader = session.read();
Dataset<Row> data = dataFrameReader.option("header", "true")
.csv("data/Tourist.csv");
Dataset<Row> responseWithSelectedColumns = data.select(col("region"),
col("country"), col("year"), col("series"), col("value").cast("double"),
Dataset<Row> responseWithSelectedColumns = data.select(col("region"),
col("country"), col("year"), col("series"), col("value").cast("double"),
col("footnotes"), col("source"));
typedDataset = responseWithSelectedColumns.as(Encoders.bean(TouristData.class));
}
@ -45,7 +46,9 @@ public class DatasetUnitTest {
Dataset<TouristData> selectedData = typedDataset
.filter((FilterFunction<TouristData>) record -> record.getCountry()
.equals("Norway"));
selectedData.show();
// uncomment to see output
// selectedData.show();
selectedData.foreach(record -> {
assertEquals("Norway", record.getCountry());
@ -56,28 +59,41 @@ public class DatasetUnitTest {
public void whenGroupCountByCountry_thenContryTotalRecords() {
Dataset<Row> countriesCount = typedDataset.groupBy(typedDataset.col("country"))
.count();
countriesCount.show();
assertEquals(Long.valueOf(220), Long.valueOf(countriesCount.count()));
// uncomment to see output
// countriesCount.show();
assertEquals(220, countriesCount.count());
}
@Test
public void whenFilteredByPropertyRange_thenRetreiveValidRecords() {
// Filter records with existing data for years between 2010 and 2017
typedDataset.filter((FilterFunction<TouristData>) record -> record.getYear() != null
&& (Long.valueOf(record.getYear()) > 2010 && Long.valueOf(record.getYear()) < 2017))
.show();
Dataset<TouristData> filteredData = typedDataset.filter(
(FilterFunction<TouristData>) record -> record.getYear() != null
&& (Long.parseLong(record.getYear()) > 2010 && Long.parseLong(record.getYear()) < 2017));
// uncomment to see output
// filteredData.show();
assertEquals(394, filteredData.count());
filteredData.foreach(record -> {
assertTrue(Integer.parseInt(record.getYear()) > 2010 && Integer.parseInt(record.getYear()) < 2017);
});
}
@Test
public void whenSumValue_thenRetreiveTotalValue() {
// Total tourist expenditure by country
typedDataset.filter((FilterFunction<TouristData>) record -> record.getValue() != null
&& record.getSeries()
.contains("expenditure"))
.groupBy("country")
.agg(sum("value"))
.show();
Dataset<Row> filteredData = typedDataset.filter((FilterFunction<TouristData>) record -> record.getValue() != null
&& record.getSeries().contains("expenditure"))
.groupBy("country")
.agg(sum("value"));
// uncomment to see output
// filteredData.show();
assertEquals(212, filteredData.count());
}
}

View File

@ -23,8 +23,11 @@ public class TransformationsUnitTest {
@BeforeClass
public static void init() {
SparkConf conf = new SparkConf().setAppName("uppercaseCountries")
.setMaster("local[*]");
SparkConf conf = new SparkConf()
.setAppName("uppercaseCountries")
.setMaster("local[*]")
.set("spark.driver.allowMultipleContexts", "true");
sc = new JavaSparkContext(conf);
tourists = sc.textFile("data/Tourist.csv")
.filter(line -> !line.startsWith("Region")); //filter header row

View File

@ -78,6 +78,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-plugins-version}</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope></includeScope>
<includeTypes>so,dll,dylib</includeTypes>
<outputDirectory>native-libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
@ -99,6 +118,7 @@
<commons-codec-version>1.10.L001</commons-codec-version>
<jets3t-version>0.9.4.0006L</jets3t-version>
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
<maven-plugins-version>3.1.1</maven-plugins-version>
</properties>
</project>

View File

@ -20,7 +20,6 @@
<artifactId>aws-java-sdk</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@ -58,7 +57,7 @@
</executions>
</plugin>
</plugins>
</build>
</build>
<properties>
<aws-java-sdk.version>1.11.290</aws-java-sdk.version>

View File

@ -19,6 +19,6 @@
<module>aws-miscellaneous</module>
<module>aws-reactive</module>
<module>aws-s3</module>
</modules>
</modules>
</project>

View File

@ -4,6 +4,6 @@ This module contains modules about core Java
## Relevant articles:
- [Multi-Module Maven Application with Java Modules](https://www.baeldung.com/maven-multi-module-project-java-jpms)
- [Understanding the NumberFormatException in Java](https://www.baeldung.com/java-number-format-exception)
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)

View File

@ -11,3 +11,5 @@ This module contains articles about Java 11 core features
- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service)
- [Java HTTPS Client Certificate Authentication](https://www.baeldung.com/java-https-client-certificate-authentication)
- [Call Methods at Runtime Using Java Reflection](https://www.baeldung.com/java-method-reflection)
- [Java HttpClient Basic Authentication](https://www.baeldung.com/java-httpclient-basic-auth)
- [Java HttpClient With SSL](https://www.baeldung.com/java-httpclient-ssl)

View File

@ -0,0 +1,32 @@
package com.baeldung.httpclient.ssl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Properties;
public class HttpClientSSLBypassUnitTest {
@Test
public void whenHttpsRequest_thenCorrect() throws IOException, InterruptedException {
final Properties props = System.getProperties();
props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.TRUE.toString());
HttpClient httpClient = HttpClient.newBuilder()
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.testingmcafeesites.com/"))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.FALSE.toString());
Assertions.assertEquals(200, response.statusCode());
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.httpclient.ssl;
import org.junit.Test;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import static org.junit.Assert.assertEquals;
public class HttpClientSSLUnitTest {
@Test
public void whenValidHttpsRequest_thenCorrect() throws URISyntaxException, IOException, InterruptedException {
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://www.google.com/"))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
}
@Test(expected = SSLHandshakeException.class)
public void whenInvalidHttpsRequest_thenInCorrect() throws IOException, InterruptedException {
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://expired.badssl.com/"))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
}
}

View File

@ -12,3 +12,4 @@
- [Sorting in Java](https://www.baeldung.com/java-sorting)
- [Getting the Size of an Iterable in Java](https://www.baeldung.com/java-iterable-size)
- [Java Null-Safe Streams from Collections](https://www.baeldung.com/java-null-safe-streams-from-collections)
- [Differences Between Iterator and Iterable and How to Use Them?](https://www.baeldung.com/java-iterator-vs-iterable)

View File

@ -0,0 +1,17 @@
package com.baeldung.collections.iterable;
class CustomIterableClient {
public static void main(String[] args) {
ShoppingCart<Product> shoppingCart = new ShoppingCart<>();
shoppingCart.add(new Product("Tuna", 42));
shoppingCart.add(new Product("Eggplant", 65));
shoppingCart.add(new Product("Salad", 45));
shoppingCart.add(new Product("Banana", 29));
for (Product product : shoppingCart) {
System.out.println(product.getName());
}
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.collections.iterable;
import java.util.Iterator;
import java.util.List;
public class IterableExample {
public void iterateUsingIterator(List<Integer> numbers) {
Iterator<Integer> iterator = numbers.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
public void iterateUsingEnhancedForLoop(List<Integer> numbers) {
for (Integer number : numbers) {
System.out.println(number);
}
}
public void iterateUsingForEachLoop(List<Integer> numbers) {
numbers.forEach(System.out::println);
}
public void removeElementsUsingIterator(List<Integer> numbers) {
Iterator<Integer> iterator = numbers.iterator();
while (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.collections.iterable;
class Product {
private String name;
private double price;
public Product(String code, double price) {
this.name = code;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.collections.iterable;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ShoppingCart<E> implements Iterable<E> {
private E[] elementData;
private int size;
public ShoppingCart() {
this.elementData = (E[]) new Object[]{};
}
public void add(E element) {
ensureCapacity(size + 1);
elementData[size++] = element;
}
private void ensureCapacity(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
elementData = Arrays.copyOf(elementData, newCapacity);
}
@Override
public Iterator<E> iterator() {
return new ShoppingCartIterator();
}
public class ShoppingCartIterator implements Iterator<E> {
int cursor;
int lastReturned = -1;
public boolean hasNext() {
return cursor != size;
}
public E next() {
return getNextElement();
}
private E getNextElement() {
int current = cursor;
exist(current);
E[] elements = ShoppingCart.this.elementData;
validate(elements, current);
cursor = current + 1;
lastReturned = current;
return elements[lastReturned];
}
private void exist(int current) {
if (current >= size) {
throw new NoSuchElementException();
}
}
private void validate(E[] elements, int current) {
if (current >= elements.length) {
throw new ConcurrentModificationException();
}
}
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.collections.iterator;
import java.util.Iterator;
class CustomIteratorClient {
public static void main(String[] args) {
Iterator<Integer> iterator = Numbers.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

View File

@ -0,0 +1,60 @@
package com.baeldung.collections.iterator;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
class Numbers {
private static final List<Integer> NUMBER_LIST = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
private Numbers() {
}
public static Iterator<Integer> iterator() {
return new PrimeIterator();
}
private static class PrimeIterator implements Iterator<Integer> {
private int cursor;
@Override
public Integer next() {
exist(cursor);
return NUMBER_LIST.get(cursor++);
}
private void exist(int current) {
if (current >= NUMBER_LIST.size()) {
throw new NoSuchElementException();
}
}
@Override
public boolean hasNext() {
if (cursor > NUMBER_LIST.size()) {
return false;
}
for (int i = cursor; i < NUMBER_LIST.size(); i++) {
if (isPrime(NUMBER_LIST.get(i))) {
cursor = i;
return true;
}
}
return false;
}
private boolean isPrime(int number) {
for (int i = 2; i <= number / 2; ++i) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.collections.iterable;
import com.baeldung.collections.iterable.IterableExample;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class IterableUnitTest {
private static List<Integer> getNumbers() {
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
return numbers;
}
@Test
void givenNumbers_whenUsingIterator_thenCorrectSize() {
List<Integer> numbers = getNumbers();
IterableExample iterableExample = new IterableExample();
iterableExample.iterateUsingIterator(numbers);
assertEquals(4, numbers.size());
}
@Test
void givenNumbers_whenRemoveElements_thenEmptyList() {
List<Integer> numbers = getNumbers();
IterableExample iterableExample = new IterableExample();
iterableExample.removeElementsUsingIterator(numbers);
assertEquals(0, numbers.size());
}
@Test
void givenNumbers_whenIterateUsingEnhancedForLoop_thenCorrectSize() {
List<Integer> numbers = getNumbers();
IterableExample iterableExample = new IterableExample();
iterableExample.iterateUsingEnhancedForLoop(numbers);
assertEquals(4, numbers.size());
}
@Test
void givenNumbers_whenIterateUsingForEachLoop_thenCorrectSize() {
List<Integer> numbers = getNumbers();
IterableExample iterableExample = new IterableExample();
iterableExample.iterateUsingForEachLoop(numbers);
assertEquals(4, numbers.size());
}
}

View File

@ -14,4 +14,16 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
</dependencies>
<properties>
<commons-lang.version>2.2</commons-lang.version>
</properties>
</project>

View File

@ -0,0 +1,72 @@
package com.baeldung.collections.sorting;
import java.util.Date;
public class Employee implements Comparable<Employee>{
private String name;
private int age;
private double salary;
private Date joiningDate;
public Employee(String name, int age, double salary, Date joiningDate) {
this.name = name;
this.age = age;
this.salary = salary;
this.joiningDate = joiningDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getJoiningDate() {
return joiningDate;
}
public void setJoiningDate(Date joiningDate) {
this.joiningDate = joiningDate;
}
@Override
public boolean equals(Object obj) {
return ((Employee) obj).getName()
.equals(getName());
}
@Override
public String toString() {
return new StringBuffer().append("(")
.append(getName()).append(",")
.append(getAge())
.append(",")
.append(getSalary()).append(",").append(getJoiningDate())
.append(")")
.toString();
}
@Override
public int compareTo(Employee employee) {
return getJoiningDate().compareTo(employee.getJoiningDate());
}
}

View File

@ -0,0 +1,146 @@
package com.baeldung.collections.sorting;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Before;
import org.junit.Test;
public class EmployeeSortingByDateUnitTest {
private List<Employee> employees = new ArrayList<>();
private List<Employee> employeesSortedByDateAsc = new ArrayList<>();
private List<Employee> employeesSortedByDateDesc = new ArrayList<>();
@Before
public void initVariables() {
Collections.addAll(employees,
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
new Employee("John", 23, 5000, new Date())
);
Collections.addAll(employeesSortedByDateDesc,
new Employee("John", 23, 5000, new Date()),
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1))
);
Collections.addAll(employeesSortedByDateAsc,
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
new Employee("John", 23, 5000, new Date())
);
}
@Test
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
Collections.sort(employees);
assertEquals(employees, employeesSortedByDateAsc);
}
@Test
public void givenEmpList_SortEmpList_thenCheckSortedList() {
Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee o1, Employee o2) {
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
}
});
assertEquals(employees, employeesSortedByDateAsc);
}
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) {
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
return 0;
return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
}
});
assertEquals(employees, employeesSortedByDateAsc);
}
@Test
public void givenEmpList_SortEmpList_thenSortedListinAscOrder() {
Collections.sort(employees, Collections.reverseOrder());
assertEquals(employees, employeesSortedByDateDesc);
}
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) {
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
}
});
assertEquals(employees, employeesSortedByDateDesc);
}
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() {
Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) {
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
return 0;
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
}
});
assertEquals(employees, employeesSortedByDateDesc);
}
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
Collections.sort(employees,
(emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
assertEquals(employees, employeesSortedByDateDesc);
}
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
Collections.sort(employees, (emp1, emp2) -> {
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
return 0;
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
});
assertEquals(employees, employeesSortedByDateDesc);
}
@Test
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
Collections.sort(employees,
Comparator.comparing(Employee::getJoiningDate));
assertEquals(employees, employeesSortedByDateAsc);
}
}

View File

@ -12,4 +12,6 @@ This module contains articles about the Java List collection
- [How to Count Duplicate Elements in Arraylist](https://www.baeldung.com/java-count-duplicate-elements-arraylist)
- [Finding the Differences Between Two Lists in Java](https://www.baeldung.com/java-lists-difference)
- [List vs. ArrayList in Java](https://www.baeldung.com/java-list-vs-arraylist)
- [How to Store HashMap<String, ArrayList> Inside a List](https://www.baeldung.com/java-hashmap-inside-list)
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
- [[<-- Prev]](/core-java-modules/core-java-collections-list-2)

View File

@ -0,0 +1,7 @@
## Core Java Collections List (Part 4)
This module contains articles about the Java List collection
### Relevant Articles:
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
- [[<-- Prev]](/core-java-modules/core-java-collections-list-3)

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-list-4</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-list-4</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>${trove4j.version}</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>${fastutil.version}</version>
</dependency>
<dependency>
<groupId>colt</groupId>
<artifactId>colt</artifactId>
<version>${colt.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
</dependencies>
<properties>
<trove4j.version>3.0.2</trove4j.version>
<fastutil.version>8.1.0</fastutil.version>
<colt.version>1.2.0</colt.version>
</properties>
</project>

View File

@ -0,0 +1,74 @@
package com.baeldung.list.listoflists;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
public class ListOfListsUnitTest {
private List<List<String>> getListOfListsFromCsv() throws URISyntaxException, IOException {
List<String> lines = Files.readAllLines(Paths.get(getClass().getResource("/listoflists/example.csv")
.toURI()));
List<List<String>> listOfLists = new ArrayList<>();
lines.forEach(line -> {
List<String> innerList = new ArrayList<>(Arrays.asList(line.split(", ")));
listOfLists.add(innerList);
});
return listOfLists;
}
private void printListOfLists(List<List<String>> listOfLists) {
System.out.println("\n List of Lists ");
System.out.println("-------------------------------------");
listOfLists.forEach(innerList -> {
String line = String.join(", ", innerList);
System.out.println(line);
});
}
@Test
void givenCsv_whenInitListOfLists_thenGetExpectedResults() throws URISyntaxException, IOException {
List<List<String>> listOfLists = getListOfListsFromCsv();
assertThat(listOfLists).hasSize(3);
assertThat(listOfLists.stream()
.map(List::size)
.collect(Collectors.toSet())).hasSize(1)
.containsExactly(4);
printListOfLists(listOfLists);
}
@Test
void givenListOfLists_whenRemoveElementFromInnerLists_thenGetExpectedResults() throws URISyntaxException, IOException {
List<List<String>> listOfLists = getListOfListsFromCsv();
listOfLists.forEach(innerList -> innerList.remove("Delete Me"));
assertThat(listOfLists.stream()
.map(List::size)
.collect(Collectors.toSet())).hasSize(1)
.containsExactly(3);
printListOfLists(listOfLists);
}
@Test
void givenListOfLists_whenAddNewInnerList_thenGetExpectedResults() throws URISyntaxException, IOException {
List<List<String>> listOfLists = getListOfListsFromCsv();
List<String> newList = new ArrayList<>(Arrays.asList("Slack", "Zoom", "Microsoft Teams", "Telegram"));
listOfLists.add(2, newList);
assertThat(listOfLists).hasSize(4);
assertThat(listOfLists.get(2)).containsExactly("Slack", "Zoom", "Microsoft Teams", "Telegram");
printListOfLists(listOfLists);
}
}

View File

@ -0,0 +1,3 @@
Linux, Microsoft Windows, Mac OS, Delete Me
Kotlin, Delete Me, Java, Python
Delete Me, Mercurial, Git, Subversion
1 Linux Microsoft Windows Mac OS Delete Me
2 Kotlin Delete Me Java Python
3 Delete Me Mercurial Git Subversion

View File

@ -6,3 +6,4 @@
- [Volatile Variables and Thread Safety](https://www.baeldung.com/java-volatile-variables-thread-safety)
- [Producer-Consumer Problem With Example in Java](https://www.baeldung.com/java-producer-consumer-problem)
- [Acquire a Lock by a Key in Java](https://www.baeldung.com/java-acquire-lock-by-key)
- [Differences Between set() and lazySet() in Java Atomic Variables](https://www.baeldung.com/java-atomic-set-vs-lazyset)

View File

@ -0,0 +1,35 @@
package com.baeldung.setvslazyset;
import java.util.concurrent.atomic.AtomicInteger;
public class Application {
AtomicInteger atomic = new AtomicInteger(0);
public static void main(String[] args) {
Application app = new Application();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
//app.atomic.set(i);
app.atomic.lazySet(i);
System.out.println("Set: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
synchronized (app.atomic) {
int counter = app.atomic.get();
System.out.println("Get: " + counter);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}).start();
}
}

View File

@ -32,8 +32,8 @@
<version>3.8.1</version>
<configuration>
<compilerArgs>
<!-- Comment the arg below to print complete stack trace information -->
<!-- <arg>-g:none</arg>-->
<!-- Comment the arg below to print complete stack trace information -->
<!-- <arg>-g:none</arg> -->
</compilerArgs>
</configuration>
</plugin>

View File

@ -32,8 +32,8 @@
<version>3.8.1</version>
<configuration>
<compilerArgs>
<!-- Comment the arg below to print complete stack trace information -->
<!-- <arg>-g:none</arg>-->
<!-- Comment the arg below to print complete stack trace information -->
<!-- <arg>-g:none</arg> -->
</compilerArgs>
</configuration>
</plugin>

View File

@ -13,4 +13,5 @@ This module contains articles about core java exceptions
- [The StackOverflowError in Java](https://www.baeldung.com/java-stack-overflow-error)
- [Checked and Unchecked Exceptions in Java](https://www.baeldung.com/java-checked-unchecked-exceptions)
- [Common Java Exceptions](https://www.baeldung.com/java-common-exceptions)
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
- [[Next -->]](/core-java-modules/core-java-exceptions-2)

View File

@ -0,0 +1,6 @@
## Java HttpClient
This module contains articles about Java HttpClient
### Relevant articles
- TODO

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-httpclient</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-httpclient</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>${mockserver.version}</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>${mockserver.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>11</maven.compiler.source.version>
<maven.compiler.target.version>11</maven.compiler.target.version>
<assertj.version>3.22.0</assertj.version>
<mockserver.version>5.11.2</mockserver.version>
</properties>
</project>

View File

@ -0,0 +1,162 @@
package com.baeldung.httpclient;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class HttpClientPost {
public static HttpResponse<String> sendSynchronousPost(String serviceUrl) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
public static CompletableFuture<HttpResponse<String>> sendAsynchronousPost(String serviceUrl) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
CompletableFuture<HttpResponse<String>> futureResponse = client
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
return futureResponse;
}
public static List<CompletableFuture<HttpResponse<String>>> sendConcurrentPost(List<String> serviceUrls) {
HttpClient client = HttpClient.newHttpClient();
List<CompletableFuture<HttpResponse<String>>> completableFutures = serviceUrls.stream()
.map(URI::create)
.map(HttpRequest::newBuilder)
.map(builder -> builder.POST(HttpRequest.BodyPublishers.noBody()))
.map(HttpRequest.Builder::build)
.map(request -> client.sendAsync(request, HttpResponse.BodyHandlers.ofString()))
.collect(Collectors.toList());
return completableFutures;
}
public static HttpResponse<String> sendPostWithAuthHeader(String serviceUrl) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.POST(HttpRequest.BodyPublishers.noBody())
.header("Authorization", "Basic " + Base64.getEncoder()
.encodeToString(("baeldung:123456").getBytes()))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
public static HttpResponse<String> sendPostWithAuthClient(String serviceUrl) throws IOException, InterruptedException {
HttpClient client = HttpClient.newBuilder()
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"baeldung",
"123456".toCharArray());
}
})
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
public static HttpResponse<String> sendPostWithJsonBody(String serviceUrl) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.POST(HttpRequest.BodyPublishers.ofString("{\"action\":\"hello\"}"))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
public static HttpResponse<String> sendPostWithFormData(String serviceUrl) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
Map<String, String> formData = new HashMap<>();
formData.put("username", "baeldung");
formData.put("message", "hello");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.POST(HttpRequest.BodyPublishers.ofString(getFormDataAsString(formData)))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
public static HttpResponse<String> sendPostWithFileData(String serviceUrl, Path file) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serviceUrl))
.POST(HttpRequest.BodyPublishers.ofFile(file))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
private static String getFormDataAsString(Map<String, String> formData) {
StringBuilder formBodyBuilder = new StringBuilder();
for (Map.Entry<String, String> singleEntry : formData.entrySet()) {
if (formBodyBuilder.length() > 0) {
formBodyBuilder.append("&");
}
formBodyBuilder.append(URLEncoder.encode(singleEntry.getKey(), StandardCharsets.UTF_8));
formBodyBuilder.append("=");
formBodyBuilder.append(URLEncoder.encode(singleEntry.getValue(), StandardCharsets.UTF_8));
}
return formBodyBuilder.toString();
}
}

View File

@ -0,0 +1,99 @@
package com.baeldung.httpclient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.*;
class HttpClientPostUnitTest extends PostRequestMockServer {
@Test
void givenSyncPostRequest_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException {
HttpResponse<String> response = HttpClientPost.sendSynchronousPost(serviceUrl);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
}
@Test
void givenAsyncPostRequest_whenServerIsAvailable_thenOkStatusIsReceived() throws ExecutionException, InterruptedException {
CompletableFuture<HttpResponse<String>> futureResponse = HttpClientPost.sendAsynchronousPost(serviceUrl);
HttpResponse<String> response = futureResponse.get();
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
}
@Test
void givenConcurrentPostRequests_whenServerIsAvailable_thenOkStatusIsReceived() throws ExecutionException, InterruptedException {
List<CompletableFuture<HttpResponse<String>>> completableFutures = HttpClientPost
.sendConcurrentPost(List.of(serviceUrl, serviceUrl));
CompletableFuture<List<HttpResponse<String>>> combinedFutures = CompletableFuture
.allOf(completableFutures.toArray(new CompletableFuture[0]))
.thenApply(future ->
completableFutures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()));
List<HttpResponse<String>> responses = combinedFutures.get();
responses.forEach((response) -> {
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
});
}
@Test
void givenPostRequestWithAuthClient_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException {
HttpResponse<String> response = HttpClientPost.sendPostWithAuthClient(serviceUrl);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
}
@Test
void givenPostRequestWithAuthHeader_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException {
HttpResponse<String> response = HttpClientPost.sendPostWithAuthHeader(serviceUrl);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
}
@Test
void givenPostRequestWithJsonBody_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException {
HttpResponse<String> response = HttpClientPost.sendPostWithJsonBody(serviceUrl);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
}
@Test
void givenPostRequestWithFormData_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException {
HttpResponse<String> response = HttpClientPost.sendPostWithFormData(serviceUrl);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
}
@Test
void givenPostRequestWithFileData_whenServerIsAvailable_thenOkStatusIsReceived(@TempDir Path tempDir) throws IOException, InterruptedException {
Path file = tempDir.resolve("temp.txt");
List<String> lines = Arrays.asList("1", "2", "3");
Files.write(file, lines);
HttpResponse<String> response = HttpClientPost.sendPostWithFileData(serviceUrl, file);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}");
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.httpclient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpStatusCode;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URISyntaxException;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
public abstract class PostRequestMockServer {
public static ClientAndServer mockServer;
public static String serviceUrl;
private static int serverPort;
public static final String SERVER_ADDRESS = "127.0.0.1";
public static final String PATH = "/test1";
public static final String METHOD = "POST";
@BeforeAll
static void startServer() throws IOException, URISyntaxException {
serverPort = getFreePort();
serviceUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH;
mockServer = startClientAndServer(serverPort);
mockBasicPostRequest();
}
@AfterAll
static void stopServer() {
mockServer.stop();
}
private static void mockBasicPostRequest() {
new MockServerClient(SERVER_ADDRESS, serverPort)
.when(
request()
.withPath(PATH)
.withMethod(METHOD)
)
.respond(
response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody("{\"message\":\"ok\"}")
);
}
private static int getFreePort () throws IOException {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
}
}

View File

@ -9,4 +9,5 @@ This module contains articles about core Java input and output (IO)
- [SequenceInputStream Class in Java](https://www.baeldung.com/java-sequenceinputstream)
- [Read a File Into a Map in Java](https://www.baeldung.com/java-read-file-into-map)
- [Read User Input Until a Condition is Met](https://www.baeldung.com/java-read-input-until-condition)
- [Java Scanner.skip method with examples](https://www.baeldung.com/java-scanner-skip)
- [[<-- Prev]](/core-java-modules/core-java-io-3)

View File

@ -0,0 +1,35 @@
package com.baeldung.scanner;
import org.junit.Test;
import java.util.*;
import java.util.regex.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScannerUnitTest {
@Test public void givenScannerWithPattern_thenSkipUsingPattern() {
String str = "Java scanner skip tutorial";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using skip(Pattern) method is to skip that meets the given pattern
sc.skip(Pattern.compile(".ava"));
assertEquals(" scanner skip tutorial", sc.nextLine());
// Scanner closed
sc.close();
}
@Test public void givenScannerWithString_thenSkipUsingStringPattern() {
String str = "Java scanner skip tutorial";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using skip(String) method is to skip that meets the given
// pattern constructed from the given String
sc.skip("Java");
assertEquals(" scanner skip tutorial", sc.nextLine());
// Scanner closed
sc.close();
}
}

View File

@ -6,4 +6,5 @@
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
- [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions)
- [Method References in Java](https://www.baeldung.com/java-method-references)
- [The Double Colon Operator in Java 8](https://www.baeldung.com/java-8-double-colon-operator)
- [The Double Colon Operator in Java 8](https://www.baeldung.com/java-8-double-colon-operator)
- [Serialize a Lambda in Java](https://www.baeldung.com/java-serialize-lambda)

View File

@ -0,0 +1,8 @@
package com.baeldung.java8.lambda.serialization;
public class NotSerializableLambdaExpression {
public static Object getLambdaExpressionObject() {
Runnable r = () -> System.out.println("please serialize this message");
return r;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.java8.lambda.serialization;
import java.io.Serializable;
public class SerializableLambdaExpression {
public static Object getLambdaExpressionObject() {
Runnable r = (Runnable & Serializable) () -> System.out.println("please serialize this message");
return r;
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.java8.lambda.serialization;
import org.junit.Test;
import java.io.*;
import java.nio.file.Files;
import java.util.function.Consumer;
import java.util.function.Function;
import static org.junit.Assert.assertTrue;
public class LambdaSerializationUnitTest {
@Test(expected = NotSerializableException.class)
public void givenRunnable_whenNoCapturing_thenSerializationFailed() throws IOException, ClassNotFoundException {
Object obj = NotSerializableLambdaExpression.getLambdaExpressionObject();
writeAndReadObject(obj, Runnable.class);
}
@Test
public void givenIntersectionType_whenNoCapturing_thenSerializationSuccess() throws IOException, ClassNotFoundException {
Object obj = SerializableLambdaExpression.getLambdaExpressionObject();
writeAndReadObject(obj, Runnable.class);
}
@Test
public void givenSerializableRunnable_whenNoCapturing_thenSerializationSuccess() throws IOException, ClassNotFoundException {
SerializableRunnable obj = () -> System.out.println("please serialize this message");
writeAndReadObject(obj, SerializableRunnable.class);
}
@Test
public void givenSerializableFunction_whenNoCapturing_thenSerializationSuccess() throws IOException, ClassNotFoundException {
SerializableFunction<String, String> obj = message -> String.format("Hello %s", message);
writeAndReadObject(obj, SerializableFunction.class);
}
@Test
public void givenSerializableConsumer_whenNoCapturing_thenSerializationSuccess() throws IOException, ClassNotFoundException {
SerializableConsumer<String> obj = message -> System.out.println(message);
writeAndReadObject(obj, SerializableConsumer.class);
}
@Test(expected = NotSerializableException.class)
public void givenSerializableConsumer_whenCapturingNotSerializable_thenSerializationFailed() throws IOException, ClassNotFoundException {
SerializableConsumer<String> obj = System.out::println;
writeAndReadObject(obj, SerializableConsumer.class);
}
private <T> void writeAndReadObject(Object obj, Class<T> clazz) throws IOException, ClassNotFoundException {
File file = Files.createTempFile("lambda", "ser").toFile();
try (
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos)
) {
oos.writeObject(obj);
}
try (
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis)
) {
Object newObj = ois.readObject();
boolean isInstance = clazz.isInstance(newObj);
assertTrue(isInstance);
}
}
interface SerializableRunnable extends Runnable, Serializable {
}
interface SerializableFunction<T, R> extends Function<T, R>, Serializable {
}
interface SerializableConsumer<T> extends Consumer<T>, Serializable {
}
}

View File

@ -28,9 +28,15 @@ public class EmployeeVO {
@Override
public boolean equals(Object obj) {
return Objects.equals(firstName, this.firstName)
&& Objects.equals(lastName, this.lastName)
&& Objects.equals(startDate, this.startDate);
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
EmployeeVO emp = (EmployeeVO) obj;
return Objects.equals(firstName, emp.firstName)
&& Objects.equals(lastName, emp.lastName)
&& Objects.equals(startDate, emp.startDate);
}
@Override

View File

@ -0,0 +1,7 @@
## Core Java Lang (Part 5)
This module contains articles about core features in the Java language
### Relevant Articles:
- [Difference Between == and equals() in Java](https://www.baeldung.com/java-equals-method-operator-difference)

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-lang-5</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-5</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<finalName>core-java-lang-5</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,125 @@
package com.baeldung.nullchecks;
import org.junit.Test;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.*;
public class NullChecksUnitTest {
@Test
public void whenReferenceEqualityOnPrimitives_thenCompareValues() {
int a = 10;
int b = 15;
int c = 10;
int d = a;
// different values check
assertFalse(a == b);
// same values check
assertTrue(a == c);
// same references check
assertTrue(a == d);
}
@Test
public void whenReferenceEqualityOnObjects_thenCompareReferences() {
Person a = new Person("Bob", 20);
Person b = new Person("Mike", 40);
Person c = new Person("Bob", 20);
Person d = a;
Person e = null;
// different values check
assertFalse(a == b);
// same values check
assertFalse(a == c);
// same references check
assertTrue(a == d);
// same references check - for nulls
assertTrue(e == null);
}
@Test
public void whenValueEqualityOnPrimitives_thenCompareValues() {
int a = 10;
Integer b = a;
assertTrue(b.equals(10));
}
@Test
public void whenValueEqualityOnObjects_thenCompareValues() {
Person a = new Person("Bob", 20);
Person b = new Person("Mike", 40);
Person c = new Person("Bob", 20);
Person d = a;
Person e = null;
// different values check
assertFalse(a.equals(b));
// same values check
assertTrue(a.equals(c));
// same references check
assertTrue(a.equals(d));
// null checks
assertFalse(a.equals(e));
assertThrows(NullPointerException.class, () -> e.equals(a));
// null checks fixed
assertFalse(e != null && e.equals(a));
// using Objects.equals
assertFalse(Objects.equals(e, a));
assertTrue(Objects.equals(null, e));
}
private class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
}

View File

@ -5,4 +5,5 @@
### Relevant articles:
- [Evaluating a Math Expression in Java](https://www.baeldung.com/java-evaluate-math-expression-string)
- [Swap Two Variables in Java](https://www.baeldung.com/java-swap-two-variables)
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2)

View File

@ -0,0 +1,22 @@
package com.baeldung.enums.randomenum;
import java.util.Random;
/**
* Represents directions.
*/
public enum Direction {
EAST, WEST, SOUTH, NORTH;
private static final Random PRNG = new Random();
/**
* Generate a random direction.
*
* @return a random direction
*/
public static Direction randomDirection() {
Direction[] directions = values();
return directions[PRNG.nextInt(directions.length)];
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.enums.randomenum;
import java.util.Random;
public class RandomEnumGenerator<T extends Enum<T>> {
private static final Random PRNG = new Random();
private final T[] values;
public RandomEnumGenerator(Class<T> e) {
values = e.getEnumConstants();
}
public T randomEnum() {
return values[PRNG.nextInt(values.length)];
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.enums.randomenum;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RandomEnumUnitTest {
@Test
public void givenEnumType_whenUsingStaticMethod_valueIsRandomlyGenerated() {
Direction direction = Direction.randomDirection();
assertThat(direction).isNotNull();
assertThat(direction instanceof Direction);
}
@Test
public void givenEnumType_whenGeneratingRandomValue_valueIsOfClassAndNotNull() {
RandomEnumGenerator reg = new RandomEnumGenerator(Direction.class);
Object direction = reg.randomEnum();
assertThat(direction).isNotNull();
assertThat(direction instanceof Direction);
}
}

View File

@ -19,7 +19,7 @@ public class FileDownloadIntegrationTest {
static String FILE_URL = "https://s3.amazonaws.com/baeldung.com/Do+JSON+with+Jackson+by+Baeldung.pdf";
static String FILE_NAME = "file.dat";
static String FILE_MD5_HASH = "c959feb066b37f5c4f0e0f45bbbb4f86";
static String FILE_MD5_HASH = "753197aa27f162faa3e3c2e48ee5eb07";
@Test
public void givenJavaIO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException {

View File

@ -10,4 +10,5 @@ This module contains articles about networking in Java
- [Find Whether an IP Address Is in the Specified Range or Not in Java](https://www.baeldung.com/java-check-ip-address-range)
- [Find the IP Address of a Client Connected to a Server](https://www.baeldung.com/java-client-get-ip-address)
- [Unix Domain Socket in Java 16](https://www.baeldung.com/java-unix-domain-socket)
- [Get the IP Address of the Current Machine Using Java](https://www.baeldung.com/java-get-ip-address)
- [[<-- Prev]](/core-java-modules/core-java-networking-2)

View File

@ -0,0 +1,50 @@
package com.baeldung.iplookup;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
public class IPAddressLookup {
public static void main(String[] args) {
System.out.println("UDP connection IP lookup: " + getLocalIpAddressUdp());
System.out.println("Socket connection IP lookup: " + getLocalIpAddressSocket());
System.out.println("AWS connection IP lookup: " + getPublicIpAddressAws());
}
public static String getLocalIpAddressUdp() {
try (final DatagramSocket datagramSocket = new DatagramSocket()) {
datagramSocket.connect(InetAddress.getByName("8.8.8.8"), 12345);
return datagramSocket.getLocalAddress().getHostAddress();
} catch (SocketException | UnknownHostException exception) {
throw new RuntimeException(exception);
}
}
public static String getLocalIpAddressSocket() {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("google.com", 80));
return socket.getLocalAddress().getHostAddress();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getPublicIpAddressAws() {
try {
String urlString = "http://checkip.amazonaws.com/";
URL url = new URL(urlString);
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
return br.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -4,4 +4,5 @@
- [Lookahead and Lookbehind in Java Regex](https://www.baeldung.com/java-regex-lookahead-lookbehind)
- [Converting Camel Case and Title Case to Words in Java](https://www.baeldung.com/java-camel-case-title-case-to-words)
- [How to Use Regular Expressions to Replace Tokens in Strings in Java](https://www.baeldung.com/java-regex-token-replacement)
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)
- [Creating a Java Array from Regular Expression Matches](https://www.baeldung.com/java-array-regex-matches)
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)

View File

@ -35,7 +35,6 @@
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -1,9 +1,11 @@
## Core Java Cookbooks and Examples
### Relevant Articles:
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
- [Introduction to Javadoc](http://www.baeldung.com/javadoc)
- [A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle)
- [Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties)
- [Illegal Character Compilation Error](https://www.baeldung.com/java-illegal-character-error)

View File

@ -59,6 +59,16 @@
<artifactId>spring-core</artifactId>
<version>${spring.core.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>${gdata.version}</version>
</dependency>
</dependencies>
<build>
@ -183,6 +193,7 @@
<source.version>1.8</source.version>
<target.version>1.8</target.version>
<spring.core.version>4.3.20.RELEASE</spring.core.version>
<gdata.version>1.47.1</gdata.version>
</properties>
</project>

View File

@ -0,0 +1,79 @@
package com.baeldung.illegalcharacter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Objects;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import org.junit.Test;
import com.google.gdata.util.io.base.UnicodeReader;
public class IllegalCharacterUnitTest {
final String RESOURCE_FILE_NAME = "bom-file.txt";
final InputStream ioStream = this.getClass()
.getClassLoader()
.getResourceAsStream(RESOURCE_FILE_NAME);
final String expected = "Hello world with BOM.";
@Test
public void whenInputFileHasBOM_thenUseInputStream() throws IOException {
String line;
String actual = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(ioStream))) {
while ((line = br.readLine()) != null) {
actual += line;
}
}
assertNotEquals(expected, actual);
}
@Test
public void whenInputFileHasBOM_thenUseInputStreamWithReplace() throws IOException {
String line;
String actual = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(ioStream)))) {
while ((line = br.readLine()) != null) {
actual += line.replace("\uFEFF", "");
}
}
assertEquals(expected, actual);
}
@Test
public void whenInputFileHasBOM_thenUseBOMInputStream() throws IOException {
String line;
String actual = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(new BOMInputStream(ioStream, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE)))) {
while ((line = br.readLine()) != null) {
actual += line;
}
}
assertEquals(expected, actual);
}
@Test
public void whenInputFileHasBOM_thenUseGoogleGdata() throws IOException {
char[] actual = new char[21];
try (Reader r = new UnicodeReader(ioStream, null)) {
r.read(actual);
}
assertEquals(expected, String.valueOf(actual));
}
}

View File

@ -0,0 +1 @@
Hello world with BOM.

View File

@ -9,10 +9,9 @@
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
@ -29,7 +28,7 @@
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.3</version>
<version>${vavr.version}</version>
</dependency>
</dependencies>
@ -43,4 +42,8 @@
</resources>
</build>
<properties>
<vavr.version>0.10.3</vavr.version>
</properties>
</project>

View File

@ -9,10 +9,9 @@
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>

Some files were not shown because too many files have changed in this diff Show More