Merge branch 'master' into stream-foreach-ifelse-logic
This commit is contained in:
@@ -24,3 +24,7 @@
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [Find the Middle Element of a Linked List](http://www.baeldung.com/java-linked-list-middle-element)
|
||||
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
||||
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
|
||||
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
|
||||
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
|
||||
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
### Relevant articles
|
||||
|
||||
- [Introduction to Asciidoctor](http://www.baeldung.com/introduction-to-asciidoctor)
|
||||
- [Generating a Book with Asciidoctor](http://www.baeldung.com/asciidoctor-book)
|
||||
- [Introduction to Asciidoctor in Java](http://www.baeldung.com/asciidoctor)
|
||||
|
||||
+4
-3
@@ -61,9 +61,10 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<asciidoctor-maven-plugin.version>1.5.5</asciidoctor-maven-plugin.version>
|
||||
<asciidoctorj.version>1.5.4</asciidoctorj.version>
|
||||
<asciidoctorj-pdf.src.version>1.5.0-alpha.11</asciidoctorj-pdf.src.version>
|
||||
<asciidoctor-maven-plugin.version>1.5.6</asciidoctor-maven-plugin.version>
|
||||
<asciidoctorj.version>1.5.6</asciidoctorj.version>
|
||||
|
||||
<asciidoctorj-pdf.version>1.5.0-alpha.15</asciidoctorj-pdf.version>
|
||||
<asciidoctorj-pdf.plugin.version>1.5.0-alpha.15</asciidoctorj-pdf.plugin.version>
|
||||
</properties>
|
||||
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package com.baeldung.asciidoctor;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AsciidoctorDemoTest {
|
||||
public class AsciidoctorDemoIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenConverting_thenResultingHTMLCode() {
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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>flyway-cdi</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>cdi-portable-extension</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
<artifactId>cdi-api</artifactId>
|
||||
<version>2.0.SP1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
<version>5.1.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-jdbc</artifactId>
|
||||
<version>8.5.33</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
<artifactId>javax.annotation-api</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.cdi.extension;
|
||||
|
||||
import org.apache.tomcat.jdbc.pool.DataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
|
||||
import javax.annotation.sql.DataSourceDefinition;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.enterprise.inject.Any;
|
||||
import javax.enterprise.inject.Default;
|
||||
import javax.enterprise.inject.literal.InjectLiteral;
|
||||
import javax.enterprise.inject.spi.*;
|
||||
import javax.enterprise.util.AnnotationLiteral;
|
||||
|
||||
|
||||
/**
|
||||
* Flyway is now under CDI container like:
|
||||
*
|
||||
* @ApplicationScoped
|
||||
* @FlywayType public class Flyway{
|
||||
* @Inject setDataSource(DataSource dataSource){
|
||||
* //...
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
|
||||
public class FlywayExtension implements Extension {
|
||||
|
||||
DataSourceDefinition dataSourceDefinition = null;
|
||||
|
||||
public void registerFlywayType(@Observes BeforeBeanDiscovery bbdEvent) {
|
||||
bbdEvent.addAnnotatedType(Flyway.class, Flyway.class.getName());
|
||||
}
|
||||
|
||||
public void detectDataSourceDefinition(@Observes @WithAnnotations(DataSourceDefinition.class) ProcessAnnotatedType<?> patEvent) {
|
||||
AnnotatedType at = patEvent.getAnnotatedType();
|
||||
dataSourceDefinition = at.getAnnotation(DataSourceDefinition.class);
|
||||
}
|
||||
|
||||
public void processAnnotatedType(@Observes ProcessAnnotatedType<Flyway> patEvent) {
|
||||
patEvent.configureAnnotatedType()
|
||||
//Add Scope
|
||||
.add(ApplicationScoped.Literal.INSTANCE)
|
||||
//Add Qualifier
|
||||
.add(new AnnotationLiteral<FlywayType>() {
|
||||
})
|
||||
//Decorate setDataSource(DataSource dataSource){} with @Inject
|
||||
.filterMethods(annotatedMethod -> {
|
||||
return annotatedMethod.getParameters().size() == 1 &&
|
||||
annotatedMethod.getParameters().get(0).getBaseType().equals(javax.sql.DataSource.class);
|
||||
})
|
||||
.findFirst().get().add(InjectLiteral.INSTANCE);
|
||||
}
|
||||
|
||||
void afterBeanDiscovery(@Observes AfterBeanDiscovery abdEvent, BeanManager bm) {
|
||||
abdEvent.addBean()
|
||||
.types(javax.sql.DataSource.class, DataSource.class)
|
||||
.qualifiers(new AnnotationLiteral<Default>() {}, new AnnotationLiteral<Any>() {})
|
||||
.scope(ApplicationScoped.class)
|
||||
.name(DataSource.class.getName())
|
||||
.beanClass(DataSource.class)
|
||||
.createWith(creationalContext -> {
|
||||
DataSource instance = new DataSource();
|
||||
instance.setUrl(dataSourceDefinition.url());
|
||||
instance.setDriverClassName(dataSourceDefinition.className());
|
||||
return instance;
|
||||
});
|
||||
}
|
||||
|
||||
void runFlywayMigration(@Observes AfterDeploymentValidation adv, BeanManager manager) {
|
||||
Flyway flyway = manager.createInstance().select(Flyway.class, new AnnotationLiteral<FlywayType>() {}).get();
|
||||
flyway.migrate();
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.cdi.extension;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({FIELD, METHOD, PARAMETER, TYPE})
|
||||
@Qualifier
|
||||
public @interface FlywayType {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<beans version="2.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
|
||||
bean-discovery-mode="annotated">
|
||||
</beans>
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
com.baeldung.cdi.extension.FlywayExtension
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
<?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>main-app</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>cdi-portable-extension</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
<artifactId>cdi-api</artifactId>
|
||||
<version>2.0.SP1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jboss.weld.se</groupId>
|
||||
<artifactId>weld-se-core</artifactId>
|
||||
<version>3.0.5.Final</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>flyway-cdi</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.197</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
<artifactId>javax.annotation-api</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.cdi.extension;
|
||||
|
||||
import javax.annotation.sql.DataSourceDefinition;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.se.SeContainer;
|
||||
import javax.enterprise.inject.se.SeContainerInitializer;
|
||||
|
||||
@ApplicationScoped
|
||||
@DataSourceDefinition(name = "ds", className = "org.h2.Driver", url = "jdbc:h2:mem:testdb")
|
||||
public class MainApp {
|
||||
public static void main(String[] args) {
|
||||
SeContainerInitializer initializer = SeContainerInitializer.newInstance();
|
||||
try (SeContainer container = initializer.initialize()) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<beans version="2.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
|
||||
bean-discovery-mode="annotated">
|
||||
</beans>
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
create table PERSON (
|
||||
ID int not null,
|
||||
NAME varchar(100) not null
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
insert into PERSON (ID, NAME) values (1, 'Axel');
|
||||
insert into PERSON (ID, NAME) values (2, 'Mr. Foo');
|
||||
insert into PERSON (ID, NAME) values (3, 'Ms. Bar');
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>cdi-portable-extension</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>main-app</module>
|
||||
<module>flyway-cdi</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
<artifactId>cdi-api</artifactId>
|
||||
<version>2.0.SP1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -48,4 +48,6 @@
|
||||
- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe)
|
||||
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
||||
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
||||
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
|
||||
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
|
||||
|
||||
@@ -63,6 +63,12 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
|
||||
public class CombiningArrays {
|
||||
|
||||
public static Object[] usingNativeJava(Object[] first, Object[] second) {
|
||||
Object[] combined = new Object[first.length + second.length];
|
||||
System.arraycopy(first, 0, combined, 0, first.length);
|
||||
System.arraycopy(second, 0, combined, first.length, second.length);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingJava8ObjectStream(Object[] first, Object[] second) {
|
||||
Object[] combined = Stream.concat(Arrays.stream(first), Arrays.stream(second)).toArray();
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingJava8FlatMaps(Object[] first, Object[] second) {
|
||||
Object[] combined = Stream.of(first, second).flatMap(Stream::of).toArray(String[]::new);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingApacheCommons(Object[] first, Object[] second) {
|
||||
Object[] combined = ArrayUtils.addAll(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingGuava(Object[] first, Object[] second) {
|
||||
Object [] combined = ObjectArrays.concat(first, second, Object.class);
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.collections4.ListUtils;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class CombiningLists {
|
||||
|
||||
public static List<Object> usingNativeJava(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = new ArrayList<>();
|
||||
combined.addAll(first);
|
||||
combined.addAll(second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingJava8ObjectStream(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = Stream.concat(first.stream(), second.stream()).collect(Collectors.toList());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingJava8FlatMaps(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = Stream.of(first, second).flatMap(Collection::stream).collect(Collectors.toList());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingApacheCommons(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = ListUtils.union(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingGuava(List<Object> first, List<Object> second) {
|
||||
Iterable<Object> combinedIterables = Iterables.unmodifiableIterable(
|
||||
Iterables.concat(first, second));
|
||||
|
||||
List<Object> combined = Lists.newArrayList(combinedIterables);
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.exec.util.MapUtils;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
public class CombiningMaps {
|
||||
|
||||
public static Map<String, String> usingPlainJava(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = new HashMap<>();
|
||||
combined.putAll(first);
|
||||
combined.putAll(second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Map<String, String> usingJava8ForEach(Map<String, String> first, Map<String, String> second) {
|
||||
second.forEach((key, value) -> first.merge(key, value, String::concat));
|
||||
return first;
|
||||
}
|
||||
|
||||
public static Map<String, String> usingJava8FlatMaps(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = Stream.of(first, second).map(Map::entrySet).flatMap(Collection::stream)
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, String::concat));
|
||||
return combined;
|
||||
|
||||
}
|
||||
|
||||
public static Map<String, String> usingApacheCommons(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = MapUtils.merge(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Map<String, String> usingGuava(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = ImmutableMap.<String, String>builder()
|
||||
.putAll(first)
|
||||
.putAll(second)
|
||||
.build();
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.collections4.SetUtils;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
public class CombiningSets {
|
||||
|
||||
public static Set<Object> usingNativeJava(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = new HashSet<>();
|
||||
combined.addAll(first);
|
||||
combined.addAll(second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingJava8ObjectStream(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = Stream.concat(first.stream(), second.stream()).collect(Collectors.toSet());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingJava8FlatMaps(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = Stream.of(first, second).flatMap(Collection::stream).collect(Collectors.toSet());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingApacheCommons(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = SetUtils.union(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingGuava(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = Sets.union(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.map.util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MapMax {
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingIteration(Map<K, V> map) {
|
||||
|
||||
Map.Entry<K, V> maxEntry = null;
|
||||
|
||||
for (Map.Entry<K, V> entry : map.entrySet()) {
|
||||
|
||||
if (maxEntry == null || entry.getValue()
|
||||
.compareTo(maxEntry.getValue()) > 0) {
|
||||
maxEntry = entry;
|
||||
}
|
||||
}
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingCollectionsMax(Map<K, V> map) {
|
||||
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), new Comparator<Entry<K, V>>() {
|
||||
public int compare(Entry<K, V> e1, Entry<K, V> e2) {
|
||||
return e1.getValue()
|
||||
.compareTo(e2.getValue());
|
||||
}
|
||||
});
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndLambda(Map<K, V> map) {
|
||||
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), (Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
|
||||
.compareTo(e2.getValue()));
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndMethodReference(Map<K, V> map) {
|
||||
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), Comparator.comparing(Map.Entry::getValue));
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingStreamAndLambda(Map<K, V> map) {
|
||||
|
||||
Optional<Entry<K, V>> maxEntry = map.entrySet()
|
||||
.stream()
|
||||
.max((Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
|
||||
.compareTo(e2.getValue()));
|
||||
|
||||
return maxEntry.get()
|
||||
.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingStreamAndMethodReference(Map<K, V> map) {
|
||||
|
||||
Optional<Entry<K, V>> maxEntry = map.entrySet()
|
||||
.stream()
|
||||
.max(Comparator.comparing(Map.Entry::getValue));
|
||||
|
||||
return maxEntry.get()
|
||||
.getValue();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
|
||||
|
||||
map.put(1, 3);
|
||||
map.put(2, 4);
|
||||
map.put(3, 5);
|
||||
map.put(4, 6);
|
||||
map.put(5, 7);
|
||||
|
||||
MapMax mapMax = new MapMax();
|
||||
|
||||
System.out.println(mapMax.maxUsingIteration(map));
|
||||
System.out.println(mapMax.maxUsingCollectionsMax(map));
|
||||
System.out.println(mapMax.maxUsingCollectionsMaxAndLambda(map));
|
||||
System.out.println(mapMax.maxUsingCollectionsMaxAndMethodReference(map));
|
||||
System.out.println(mapMax.maxUsingStreamAndLambda(map));
|
||||
System.out.println(mapMax.maxUsingStreamAndMethodReference(map));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningArraysUnitTest {
|
||||
private static final String first[] = {
|
||||
"One",
|
||||
"Two",
|
||||
"Three"
|
||||
};
|
||||
|
||||
private static final String second[] = {
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
};
|
||||
|
||||
private static final String expected[] = {
|
||||
"One",
|
||||
"Two",
|
||||
"Three",
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
};
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingNativeJava_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingNativeJava(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingObjectStreams_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingJava8ObjectStream(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingFlatMaps_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingJava8FlatMaps(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingApacheCommons_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingApacheCommons(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingGuava_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingGuava(first, second));
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningListsUnitTest {
|
||||
private static final List<Object> first = Arrays.asList(new Object[]{
|
||||
"One",
|
||||
"Two",
|
||||
"Three"
|
||||
});
|
||||
|
||||
private static final List<Object> second = Arrays.asList(new Object[]{
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
});
|
||||
|
||||
private static final List<Object> expected = Arrays.asList(new Object[]{
|
||||
"One",
|
||||
"Two",
|
||||
"Three",
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
});
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingNativeJava_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingNativeJava(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingObjectStreams_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingJava8ObjectStream(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingFlatMaps_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingJava8FlatMaps(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingApacheCommons_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingApacheCommons(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingGuava_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingGuava(first, second), is(expected));
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningMapsUnitTest {
|
||||
private static final Map<String, String> first = new HashMap<>();
|
||||
private static final Map<String, String> second = new HashMap<>();
|
||||
private static Map<String, String> expected = new HashMap<>();
|
||||
|
||||
static {
|
||||
first.put("one", "first String");
|
||||
first.put("two", "second String");
|
||||
|
||||
second.put("three", "third String");
|
||||
second.put("four", "fourth String");
|
||||
|
||||
expected.put("one", "first String");
|
||||
expected.put("two", "second String");
|
||||
expected.put("three", "third String");
|
||||
expected.put("four", "fourth String");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingNativeJava_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingPlainJava(first, second), is(expected));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingForEach_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingJava8ForEach(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingFlatMaps_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingJava8FlatMaps(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingApacheCommons_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingApacheCommons(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingGuava_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingGuava(first, second), is(expected));
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningSetsUnitTest {
|
||||
private static final Set<Object> first = new HashSet<Object>(Arrays.asList(new Object[] { "One", "Two", "Three" }));
|
||||
|
||||
private static final Set<Object> second = new HashSet<Object>(Arrays.asList(new Object[] { "Four", "Five", "Six" }));
|
||||
|
||||
private static final Set<Object> expected = new HashSet<Object>(Arrays
|
||||
.asList(new Object[] { "One", "Two", "Three", "Four", "Five", "Six" }));
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingNativeJava_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingNativeJava(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingObjectStreams_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingJava8ObjectStream(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingFlatMaps_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingJava8FlatMaps(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingApacheCommons_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingApacheCommons(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingGuava_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingGuava(first, second), is(expected));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.map.util;
|
||||
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MapMaxUnitTest {
|
||||
|
||||
Map<Integer, Integer> map = null;
|
||||
MapMax mapMax = null;
|
||||
|
||||
|
||||
@Before
|
||||
public void setupTestData() {
|
||||
map = new HashMap<Integer, Integer>();
|
||||
map.put(23, 12);
|
||||
map.put(46, 24);
|
||||
map.put(27, 38);
|
||||
mapMax = new MapMax();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMap_whenIterated_thenReturnMaxValue() {
|
||||
assertEquals(new Integer(38), mapMax.maxUsingIteration(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMap_whenUsingCollectionsMax_thenReturnMaxValue() {
|
||||
assertEquals(new Integer(38), mapMax.maxUsingCollectionsMax(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMap_whenUsingCollectionsMaxAndLambda_thenReturnMaxValue() {
|
||||
assertEquals(new Integer(38), mapMax.maxUsingCollectionsMaxAndLambda(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMap_whenUsingCollectionsMaxAndMethodReference_thenReturnMaxValue() {
|
||||
assertEquals(new Integer(38), mapMax.maxUsingCollectionsMaxAndMethodReference(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMap_whenUsingStreamAndLambda_thenReturnMaxValue() {
|
||||
assertEquals(new Integer(38), mapMax.maxUsingStreamAndLambda(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMap_whenUsingStreamAndMethodReference_thenReturnMaxValue() {
|
||||
assertEquals(new Integer(38), mapMax.maxUsingStreamAndMethodReference (map));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -31,3 +31,4 @@
|
||||
- [Create a Symbolic Link with Java](http://www.baeldung.com/java-symlink)
|
||||
- [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [ Read a File into an ArrayList](https://www.baeldung.com/java-file-to-arraylist)
|
||||
|
||||
@@ -151,3 +151,4 @@
|
||||
- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist)
|
||||
- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception)
|
||||
- [Add a Character to a String at a Given Position](https://www.baeldung.com/java-add-character-to-string)
|
||||
- [Synthetic Constructs in Java](https://www.baeldung.com/java-synthetic)
|
||||
|
||||
@@ -33,4 +33,7 @@
|
||||
- [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging)
|
||||
- [Kotlin Constructors](https://www.baeldung.com/kotlin-constructors)
|
||||
- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern)
|
||||
- [Kotlin Nested and Inner Classes](https://www.baeldung.com/kotlin-inner-classes)
|
||||
- [Kotlin Nested and Inner Classes](https://www.baeldung.com/kotlin-inner-classes)
|
||||
- [Kotlin with Ktor](https://www.baeldung.com/kotlin-ktor)
|
||||
- [Fuel HTTP Library with Kotlin](https://www.baeldung.com/kotlin-fuel)
|
||||
- [Introduction to Kovenant Library for Kotlin](https://www.baeldung.com/kotlin-kovenant)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user