This commit is contained in:
markoprevisic 2018-10-16 00:50:50 +02:00
commit 43317f62a6
37 changed files with 285 additions and 115 deletions

View File

@ -1 +0,0 @@
/bin/

View File

@ -1,4 +1,4 @@
package findItems; package com.baeldung.findItems;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -10,24 +10,20 @@ import java.util.stream.Collectors;
import org.junit.Test; import org.junit.Test;
public class FindItemsBasedOnValues { public class FindItemsBasedOnOtherStreamUnitTest {
List<Employee> EmplList = new ArrayList<Employee>(); private List<Employee> employeeList = new ArrayList<Employee>();
List<Department> deptList = new ArrayList<Department>();
public static void main(String[] args) throws ParseException { private List<Department> departmentList = new ArrayList<Department>();
FindItemsBasedOnValues findItems = new FindItemsBasedOnValues();
findItems.givenDepartmentList_thenEmployeeListIsFilteredCorrectly();
}
@Test @Test
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() { public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
Integer expectedId = 1002; Integer expectedId = 1002;
populate(EmplList, deptList); populate(employeeList, departmentList);
List<Employee> filteredList = EmplList.stream() List<Employee> filteredList = employeeList.stream()
.filter(empl -> deptList.stream() .filter(empl -> departmentList.stream()
.anyMatch(dept -> dept.getDepartment() .anyMatch(dept -> dept.getDepartment()
.equals("sales") && empl.getEmployeeId() .equals("sales") && empl.getEmployeeId()
.equals(dept.getEmployeeId()))) .equals(dept.getEmployeeId())))
@ -53,16 +49,16 @@ public class FindItemsBasedOnValues {
} }
class Employee { class Employee {
Integer employeeId; private Integer employeeId;
String employeeName; private String employeeName;
public Employee(Integer employeeId, String employeeName) { Employee(Integer employeeId, String employeeName) {
super(); super();
this.employeeId = employeeId; this.employeeId = employeeId;
this.employeeName = employeeName; this.employeeName = employeeName;
} }
public Integer getEmployeeId() { Integer getEmployeeId() {
return employeeId; return employeeId;
} }
@ -73,20 +69,20 @@ class Employee {
} }
class Department { class Department {
Integer employeeId; private Integer employeeId;
String department; private String department;
public Department(Integer employeeId, String department) { Department(Integer employeeId, String department) {
super(); super();
this.employeeId = employeeId; this.employeeId = employeeId;
this.department = department; this.department = department;
} }
public Integer getEmployeeId() { Integer getEmployeeId() {
return employeeId; return employeeId;
} }
public String getDepartment() { String getDepartment() {
return department; return department;
} }

View File

@ -0,0 +1,54 @@
package com.baeldung.modulo;
import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.*;
public class ModuloUnitTest {
@Test
public void whenIntegerDivision_thenLosesRemainder(){
assertThat(11 / 4).isEqualTo(2);
}
@Test
public void whenDoubleDivision_thenKeepsRemainder(){
assertThat(11 / 4.0).isEqualTo(2.75);
}
@Test
public void whenModulo_thenReturnsRemainder(){
assertThat(11 % 4).isEqualTo(3);
}
@Test(expected = ArithmeticException.class)
public void whenDivisionByZero_thenArithmeticException(){
double result = 1 / 0;
}
@Test(expected = ArithmeticException.class)
public void whenModuloByZero_thenArithmeticException(){
double result = 1 % 0;
}
@Test
public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){
assertThat(3 % 2).isEqualTo(1);
}
@Test
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){
assertThat(4 % 2).isEqualTo(0);
}
@Test
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){
int QUEUE_CAPACITY= 10;
int[] circularQueue = new int[QUEUE_CAPACITY];
int itemsInserted = 0;
for (int value = 0; value < 1000; value++) {
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
circularQueue[writeIndex] = value;
}
}
}

13
guava-collections/.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -0,0 +1,20 @@
=========
## Guava and Hamcrest Cookbooks and Examples
### Relevant Articles:
- [Guava Collections Cookbook](http://www.baeldung.com/guava-collections)
- [Guava Ordering Cookbook](http://www.baeldung.com/guava-order)
- [Hamcrest Collections Cookbook](http://www.baeldung.com/hamcrest-collections-arrays)
- [Partition a List in Java](http://www.baeldung.com/java-list-split)
- [Filtering and Transforming Collections in Guava](http://www.baeldung.com/guava-filter-and-transform-a-collection)
- [Guava Join and Split Collections](http://www.baeldung.com/guava-joiner-and-splitter-tutorial)
- [Guava Lists](http://www.baeldung.com/guava-lists)
- [Guava Sets](http://www.baeldung.com/guava-sets)
- [Guava Maps](http://www.baeldung.com/guava-maps)
- [Guide to Guava Multimap](http://www.baeldung.com/guava-multimap)
- [Guide to Guava RangeSet](http://www.baeldung.com/guava-rangeset)
- [Guide to Guava RangeMap](http://www.baeldung.com/guava-rangemap)
- [Guide to Guava MinMaxPriorityQueue and EvictingQueue](http://www.baeldung.com/guava-minmax-priority-queue-and-evicting-queue)
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)

66
guava-collections/pom.xml Normal file
View File

@ -0,0 +1,66 @@
<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>
<groupId>com.baeldung</groupId>
<artifactId>guava-collections</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>guava-collections</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<!-- utils -->
<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>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>${java-hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>guava</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- util -->
<guava.version>24.0-jre</guava.version>
<commons-lang3.version>3.5</commons-lang3.version>
<commons-collections4.version>4.1</commons-collections4.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
</properties>
</project>

View File

@ -0,0 +1,19 @@
<?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>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -0,0 +1 @@
John Jane Adam Tom

View File

@ -0,0 +1 @@
Hello world

View File

@ -0,0 +1 @@
Test

View File

@ -0,0 +1,4 @@
John
Jane
Adam
Tom

View File

@ -0,0 +1 @@
Hello world

Binary file not shown.

View File

@ -4,33 +4,19 @@
### Relevant Articles: ### Relevant Articles:
- [Guava Collections Cookbook](http://www.baeldung.com/guava-collections)
- [Guava Ordering Cookbook](http://www.baeldung.com/guava-order)
- [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates) - [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates)
- [Hamcrest Collections Cookbook](http://www.baeldung.com/hamcrest-collections-arrays)
- [Partition a List in Java](http://www.baeldung.com/java-list-split)
- [Filtering and Transforming Collections in Guava](http://www.baeldung.com/guava-filter-and-transform-a-collection)
- [Guava Join and Split Collections](http://www.baeldung.com/guava-joiner-and-splitter-tutorial)
- [Guava Write to File, Read from File](http://www.baeldung.com/guava-write-to-file-read-from-file) - [Guava Write to File, Read from File](http://www.baeldung.com/guava-write-to-file-read-from-file)
- [Guava Lists](http://www.baeldung.com/guava-lists)
- [Guava Sets](http://www.baeldung.com/guava-sets)
- [Guava Maps](http://www.baeldung.com/guava-maps)
- [Guava Set + Function = Map](http://www.baeldung.com/guava-set-function-map-tutorial) - [Guava Set + Function = Map](http://www.baeldung.com/guava-set-function-map-tutorial)
- [Guide to Guavas Ordering](http://www.baeldung.com/guava-ordering) - [Guide to Guavas Ordering](http://www.baeldung.com/guava-ordering)
- [Guide to Guavas PreConditions](http://www.baeldung.com/guava-preconditions) - [Guide to Guavas PreConditions](http://www.baeldung.com/guava-preconditions)
- [Introduction to Guava CacheLoader](http://www.baeldung.com/guava-cacheloader) - [Introduction to Guava CacheLoader](http://www.baeldung.com/guava-cacheloader)
- [Introduction to Guava Memoizer](http://www.baeldung.com/guava-memoizer) - [Introduction to Guava Memoizer](http://www.baeldung.com/guava-memoizer)
- [Guide to Guavas EventBus](http://www.baeldung.com/guava-eventbus) - [Guide to Guavas EventBus](http://www.baeldung.com/guava-eventbus)
- [Guide to Guava Multimap](http://www.baeldung.com/guava-multimap)
- [Guide to Guava RangeSet](http://www.baeldung.com/guava-rangeset)
- [Guide to Guava RangeMap](http://www.baeldung.com/guava-rangemap)
- [Guide to Guava Table](http://www.baeldung.com/guava-table) - [Guide to Guava Table](http://www.baeldung.com/guava-table)
- [Guide to Guavas Reflection Utilities](http://www.baeldung.com/guava-reflection) - [Guide to Guavas Reflection Utilities](http://www.baeldung.com/guava-reflection)
- [Guide to Guava ClassToInstanceMap](http://www.baeldung.com/guava-class-to-instance-map) - [Guide to Guava ClassToInstanceMap](http://www.baeldung.com/guava-class-to-instance-map)
- [Guide to Guava MinMaxPriorityQueue and EvictingQueue](http://www.baeldung.com/guava-minmax-priority-queue-and-evicting-queue)
- [Guide to Mathematical Utilities in Guava](http://www.baeldung.com/guava-math) - [Guide to Mathematical Utilities in Guava](http://www.baeldung.com/guava-math)
- [Bloom Filter in Java using Guava](http://www.baeldung.com/guava-bloom-filter) - [Bloom Filter in Java using Guava](http://www.baeldung.com/guava-bloom-filter)
- [Using Guava CountingOutputStream](http://www.baeldung.com/guava-counting-outputstream) - [Using Guava CountingOutputStream](http://www.baeldung.com/guava-counting-outputstream)
- [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers) - [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers)
- [Quick Guide to the Guava RateLimiter](http://www.baeldung.com/guava-rate-limiter) - [Quick Guide to the Guava RateLimiter](http://www.baeldung.com/guava-rate-limiter)
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)

View File

@ -14,12 +14,6 @@
</parent> </parent>
<dependencies> <dependencies>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
@ -56,7 +50,6 @@
<!-- util --> <!-- util -->
<guava.version>24.0-jre</guava.version> <guava.version>24.0-jre</guava.version>
<commons-lang3.version>3.5</commons-lang3.version> <commons-lang3.version>3.5</commons-lang3.version>
<commons-collections4.version>4.1</commons-collections4.version>
<!-- testing --> <!-- testing -->
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>

View File

@ -9,3 +9,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Java Bean Validation Basics](http://www.baeldung.com/javax-validation) - [Java Bean Validation Basics](http://www.baeldung.com/javax-validation)
- [Validating Container Elements with Bean Validation 2.0](http://www.baeldung.com/bean-validation-container-elements) - [Validating Container Elements with Bean Validation 2.0](http://www.baeldung.com/bean-validation-container-elements)
- [Method Constraints with Bean Validation 2.0](http://www.baeldung.com/javax-validation-method-constraints) - [Method Constraints with Bean Validation 2.0](http://www.baeldung.com/javax-validation-method-constraints)
- [Difference Between @NotNull, @NotEmpty, and @NotBlank Constraints in Bean Validation](https://www.baeldung.com/java-bean-validation-not-null-empty-blank)

View File

@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId> <artifactId>spring-boot-dependencies</artifactId>
<version>1.5.15.RELEASE</version> <version>1.5.16.RELEASE</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>

View File

@ -378,6 +378,7 @@
<module>google-web-toolkit</module> <module>google-web-toolkit</module>
<module>gson</module> <module>gson</module>
<module>guava</module> <module>guava</module>
<module>guava-collections</module>
<module>guava-modules/guava-18</module> <module>guava-modules/guava-18</module>
<module>guava-modules/guava-19</module> <module>guava-modules/guava-19</module>
<module>guava-modules/guava-21</module> <module>guava-modules/guava-21</module>
@ -1285,6 +1286,7 @@
<module>google-cloud</module> <module>google-cloud</module>
<module>gson</module> <module>gson</module>
<module>guava</module> <module>guava</module>
<module>guava-collections</module>
<module>guava-modules/guava-18</module> <module>guava-modules/guava-18</module>
<module>guava-modules/guava-19</module> <module>guava-modules/guava-19</module>
<module>guava-modules/guava-21</module> <module>guava-modules/guava-21</module>