Merge branch 'master' into core-java-move-1

This commit is contained in:
Sam Millington
2019-09-08 18:21:13 +01:00
committed by GitHub
328 changed files with 5465 additions and 5578 deletions

View File

@@ -20,7 +20,6 @@
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
- [Java Optional orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
- [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time)

View File

@@ -134,16 +134,6 @@
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
@@ -192,7 +182,6 @@
<jmh-generator.version>1.19</jmh-generator.version>
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
<!-- plugins -->
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
</properties>
</project>

View File

@@ -0,0 +1,40 @@
package com.baeldung.java.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RemoveFromList {
public static void main(String[] args) {
List<String> sports = new ArrayList<>();
sports.add("Football");
sports.add("Basketball");
sports.add("Baseball");
sports.add("Boxing");
sports.add("Cycling");
System.out.println("List before removing: " + sports);
// Remove with index
sports.remove(1);
// Remove with an element
sports.remove("Baseball");
// Iterator remove method
Iterator<String> iterator = sports.iterator();
while (iterator.hasNext()) {
if (iterator.next().equals("Boxing")) {
iterator.remove();
break;
}
}
// ArrayList removeIf method (Java 8)
sports.removeIf(p -> p.equals("Cycling"));
System.out.println("List after removing: " + sports);
}
}

View File

@@ -10,8 +10,4 @@
- [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line)
- [Ways to Iterate Over a List in Java](https://www.baeldung.com/java-iterate-list)
- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections)
- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection)
- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal)
- [List of Primitive Integer Values in Java](https://www.baeldung.com/java-list-primitive-int)
- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance)
- [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list)
- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection)

View File

@@ -36,41 +36,11 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</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-core.version}</version>
</dependency>
</dependencies>
<properties>
<commons-collections4.version>4.1</commons-collections4.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<assertj.version>3.11.1</assertj.version>
<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,11 @@
=========
## Core Java Collections List Cookbooks and Examples
### Relevant Articles:
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal)
- [List of Primitive Integer Values in Java](https://www.baeldung.com/java-list-primitive-int)
- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance)
- [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list)

View File

@@ -0,0 +1,76 @@
<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-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-list-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</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-core.version}</version>
</dependency>
</dependencies>
<properties>
<commons-collections4.version>4.1</commons-collections4.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<assertj.version>3.11.1</assertj.version>
<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,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -10,7 +10,5 @@
- [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards)
- [Remove the First Element from a List](http://www.baeldung.com/java-remove-first-element-from-list)
- [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java)
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
- [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max)
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
- [Remove All Occurrences of a Specific Value from a List](https://www.baeldung.com/java-remove-value-from-list)

View File

@@ -17,15 +17,9 @@ public class FileReaderExampleUnitTest {
public void givenFileReader_whenReadAllCharacters_thenReturnsContent() throws IOException {
String expectedText = "Hello, World!";
File file = new File(FILE_PATH);
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
try (FileReader fileReader = new FileReader(file)) {
String content = FileReaderExample.readAllCharactersOneByOne(fileReader);
Assert.assertEquals(expectedText, content);
} finally {
if (fileReader != null) {
fileReader.close();
}
}
}
@@ -33,15 +27,9 @@ public class FileReaderExampleUnitTest {
public void givenFileReader_whenReadMultipleCharacters_thenReturnsContent() throws IOException {
String expectedText = "Hello";
File file = new File(FILE_PATH);
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
try (FileReader fileReader = new FileReader(file)) {
String content = FileReaderExample.readMultipleCharacters(fileReader, 5);
Assert.assertEquals(expectedText, content);
} finally {
if (fileReader != null) {
fileReader.close();
}
}
}

View File

@@ -207,6 +207,21 @@
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.baeldung.resource.MyResourceLoader</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
@@ -274,6 +289,8 @@
<!-- Mime Type Libraries -->
<tika.version>1.18</tika.version>
<jmime-magic.version>0.1.5</jmime-magic.version>
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
</properties>
</project>

View File

@@ -0,0 +1,42 @@
package com.baeldung.resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
public class MyResourceLoader {
private void loadFileWithReader() throws IOException {
try (FileReader fileReader = new FileReader("src/main/resources/input.txt");
BufferedReader reader = new BufferedReader(fileReader)) {
String contents = reader.lines()
.collect(Collectors.joining(System.lineSeparator()));
System.out.println(contents);
}
}
private void loadFileAsResource() throws IOException {
try (InputStream inputStream = getClass().getResourceAsStream("/input.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String contents = reader.lines()
.collect(Collectors.joining(System.lineSeparator()));
System.out.println(contents);
}
}
public static void main(String[] args) throws IOException {
MyResourceLoader resourceLoader = new MyResourceLoader();
resourceLoader.loadFileAsResource();
resourceLoader.loadFileWithReader();
}
}

View File

@@ -0,0 +1,30 @@
package com.baeldung.copyconstructor;
import java.util.Date;
public class Employee {
protected int id;
protected String name;
protected Date startDate;
public Employee(int id, String name, Date startDate) {
this.id = id;
this.name = name;
this.startDate = startDate;
}
public Employee(Employee employee) {
this.id = employee.id;
this.name = employee.name;
this.startDate = new Date(employee.startDate.getTime());
}
Date getStartDate() {
return startDate;
}
public Employee copy() {
return new Employee(this);
}
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.copyconstructor;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class Manager extends Employee {
private List<Employee> directReports;
public Manager(int id, String name, Date startDate, List<Employee> directReports) {
super(id, name, startDate);
this.directReports = directReports;
}
public Manager(Manager manager) {
super(manager.id, manager.name, manager.startDate);
this.directReports = manager.directReports.stream()
.collect(Collectors.toList());
}
@Override
public Employee copy() {
return new Manager(this);
}
List<Employee> getDirectReport() {
return this.directReports;
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.copyconstructor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.util.Date;
import org.junit.Test;
public class EmployeeUnitTest {
@Test
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
Date d1 = new Date(123);
Employee e1 = new Employee(1, "Baeldung", d1);
Employee e2 = new Employee(e1);
assertEquals(d1, e1.getStartDate());
assertEquals(d1, e2.getStartDate());
d1.setTime(456);
assertEquals(d1, e1.getStartDate());
assertNotEquals(d1, e2.getStartDate());
}
@Test
public void givenCopyMethod_whenCopy_thenDistinct() {
Date d1 = new Date(123);
Employee e1 = new Employee(1, "Baeldung", d1);
Employee e2 = e1.copy();
assertEquals(d1, e1.getStartDate());
assertEquals(d1, e2.getStartDate());
d1.setTime(456);
assertEquals(d1, e1.getStartDate());
assertNotEquals(d1, e2.getStartDate());
}
}

View File

@@ -0,0 +1,67 @@
package com.baeldung.copyconstructor;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Test;
public class ManagerUnitTest {
@Test
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
Date startDate = new Date(123);
Employee e1 = new Employee(1, "Baeldung", startDate);
Employee e2 = new Employee(e1);
List<Employee> directReports = new ArrayList<Employee>();
directReports.add(e1);
directReports.add(e2);
Manager m1 = new Manager(1, "Baeldung Manager", startDate, directReports);
Manager m2 = new Manager(m1);
List<Employee> directReports1 = m1.getDirectReport();
List<Employee> directReports2 = m2.getDirectReport();
assertEquals(directReports1.size(), directReports2.size());
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
// clear m1's direct reports list. m2's list should not be affected
directReports.clear();
directReports1 = m1.getDirectReport();
directReports2 = m2.getDirectReport();
assertEquals(0, directReports1.size());
assertEquals(2, directReports2.size());
}
@Test
public void givenCopyMethod_whenCopy_thenDistinct() {
Date startDate = new Date(123);
Employee e1 = new Employee(1, "Baeldung", startDate);
Employee e2 = new Employee(e1);
List<Employee> directReports = new ArrayList<Employee>();
directReports.add(e1);
directReports.add(e2);
// a Manager object whose declaration type is Employee.
Employee source = new Manager(1, "Baeldung Manager", startDate, directReports);
Employee clone = source.copy();
// after copy, clone should be still a Manager object.
assertTrue(clone instanceof Manager);
List<Employee> directReports1 = ((Manager) source).getDirectReport();
List<Employee> directReports2 = ((Manager) clone).getDirectReport();
assertEquals(directReports1.size(), directReports2.size());
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
// clear source's direct reports list. clone's list should not be affected
directReports.clear();
directReports1 = ((Manager) source).getDirectReport();
directReports2 = ((Manager) clone).getDirectReport();
assertEquals(0, directReports1.size());
assertEquals(2, directReports2.size());
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.booleanoperators;
public class Car {
private boolean diesel;
private boolean manual;
public Car(boolean diesel, boolean manual) {
this.diesel = diesel;
this.manual = manual;
}
public boolean isDiesel() {
return diesel;
}
public boolean isManual() {
return manual;
}
static Car dieselAndManualCar() {
return new Car(true, true);
}
static Car dieselAndAutomaticCar() {
return new Car(true, false);
}
static Car oilAndManualCar() {
return new Car(false, true);
}
static Car oilAndAutomaticCar() {
return new Car(false, false);
}
}

View File

@@ -0,0 +1,69 @@
package com.baeldung.booleanoperators;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class XorUnitTest {
@Test
void givenDieselManualCar_whenXorOldSchool_thenFalse() {
Car car = Car.dieselAndManualCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselAutomaticCar_whenXorOldSchool_thenTrue() {
Car car = Car.dieselAndAutomaticCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselManualCar_whenXorOldSchool_thenTrue() {
Car car = Car.oilAndManualCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselAutomaticCar_whenXorOldSchool_thenFalse() {
Car car = Car.oilAndAutomaticCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselManualCar_whenXor_thenFalse() {
Car car = Car.dieselAndManualCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselAutomaticCar_whenXor_thenTrue() {
Car car = Car.dieselAndAutomaticCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselManualCar_whenXor_thenTrue() {
Car car = Car.oilAndManualCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselAutomaticCar_whenXor_thenFalse() {
Car car = Car.oilAndAutomaticCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isFalse();
}
@Test
void givenNumbersOneAndThree_whenXor_thenTwo() {
assertThat(1 ^ 3).isEqualTo(2);
}
}

View File

@@ -3,13 +3,10 @@
## Core Java Lang Cookbooks and Examples
### Relevant Articles:
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode)
- [Chained Exceptions in Java](http://www.baeldung.com/java-chained-exceptions)
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration)
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
- [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization)
- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator)
- [Comparator and Comparable in Java](http://www.baeldung.com/java-comparator-comparable)

View File

@@ -1,4 +1,9 @@
## Relevant Articles
- [Void Type in Java](https://www.baeldung.com/java-void-type)
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)

View File

@@ -23,7 +23,30 @@
</dependency>
</dependencies>
<build>
<finalName>core-java-reflection</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
</project>

View File

@@ -3,6 +3,7 @@ package com.baeldung.uuid;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.UUID;
public class UUIDGenerator {
@@ -65,9 +66,9 @@ public class UUIDGenerator {
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException nsae) {
throw new InternalError("MD5 not supported", nsae);
throw new InternalError("SHA-1 not supported", nsae);
}
byte[] bytes = md.digest(name);
byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16);
bytes[6] &= 0x0f; /* clear version */
bytes[6] |= 0x50; /* set to version 5 */
bytes[8] &= 0x3f; /* clear variant */