Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
@@ -24,3 +24,7 @@
|
|||||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
- [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)
|
- [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)
|
- [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)
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.algorithms.mergesort;
|
||||||
|
|
||||||
|
public class MergeSort {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] a = { 5, 1, 6, 2, 3, 4 };
|
||||||
|
mergeSort(a, a.length);
|
||||||
|
for (int i = 0; i < a.length; i++)
|
||||||
|
System.out.println(a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void mergeSort(int[] a, int n) {
|
||||||
|
if (n < 2)
|
||||||
|
return;
|
||||||
|
int mid = n / 2;
|
||||||
|
int[] l = new int[mid];
|
||||||
|
int[] r = new int[n - mid];
|
||||||
|
|
||||||
|
for (int i = 0; i < mid; i++) {
|
||||||
|
l[i] = a[i];
|
||||||
|
}
|
||||||
|
for (int i = mid; i < n; i++) {
|
||||||
|
r[i - mid] = a[i];
|
||||||
|
}
|
||||||
|
mergeSort(l, mid);
|
||||||
|
mergeSort(r, n - mid);
|
||||||
|
|
||||||
|
merge(a, l, r, mid, n - mid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void merge(int[] a, int[] l, int[] r, int left, int right) {
|
||||||
|
|
||||||
|
int i = 0, j = 0, k = 0;
|
||||||
|
|
||||||
|
while (i < left && j < right) {
|
||||||
|
|
||||||
|
if (l[i] < r[j])
|
||||||
|
a[k++] = l[i++];
|
||||||
|
else
|
||||||
|
a[k++] = r[j++];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < left)
|
||||||
|
a[k++] = l[i++];
|
||||||
|
|
||||||
|
while (j < right)
|
||||||
|
a[k++] = r[j++];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.algorithms.mergesort;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MergeSortUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void positiveTest() {
|
||||||
|
int[] actual = { 5, 1, 6, 2, 3, 4 };
|
||||||
|
int[] expected = { 1, 2, 3, 4, 5, 6 };
|
||||||
|
MergeSort.mergeSort(actual, actual.length);
|
||||||
|
Assert.assertArrayEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+3
-3
@@ -7,7 +7,7 @@ import junit.framework.TestSuite;
|
|||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
*/
|
*/
|
||||||
public class AppTest
|
public class AppUnitTest
|
||||||
extends TestCase
|
extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -15,7 +15,7 @@ public class AppTest
|
|||||||
*
|
*
|
||||||
* @param testName name of the test case
|
* @param testName name of the test case
|
||||||
*/
|
*/
|
||||||
public AppTest( String testName )
|
public AppUnitTest( String testName )
|
||||||
{
|
{
|
||||||
super( testName );
|
super( testName );
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,7 @@ public class AppTest
|
|||||||
*/
|
*/
|
||||||
public static Test suite()
|
public static Test suite()
|
||||||
{
|
{
|
||||||
return new TestSuite( AppTest.class );
|
return new TestSuite( AppUnitTest.class );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>apache-avro</artifactId>
|
<artifactId>apache-avro</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>Apache Avro</version>
|
<name>Apache Avro</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ import java.util.Objects;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class AvroSerealizerDeSerealizerTest {
|
public class AvroSerealizerDeSerealizerUnitTest {
|
||||||
|
|
||||||
AvroSerealizer serealizer;
|
AvroSerealizer serealizer;
|
||||||
AvroDeSerealizer deSerealizer;
|
AvroDeSerealizer deSerealizer;
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
### Relevant articles
|
### Relevant articles
|
||||||
|
|
||||||
- [Introduction to Asciidoctor](http://www.baeldung.com/introduction-to-asciidoctor)
|
|
||||||
- [Generating a Book with Asciidoctor](http://www.baeldung.com/asciidoctor-book)
|
- [Generating a Book with Asciidoctor](http://www.baeldung.com/asciidoctor-book)
|
||||||
- [Introduction to Asciidoctor in Java](http://www.baeldung.com/asciidoctor)
|
- [Introduction to Asciidoctor in Java](http://www.baeldung.com/asciidoctor)
|
||||||
|
|||||||
+4
-3
@@ -61,9 +61,10 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<asciidoctor-maven-plugin.version>1.5.5</asciidoctor-maven-plugin.version>
|
<asciidoctor-maven-plugin.version>1.5.6</asciidoctor-maven-plugin.version>
|
||||||
<asciidoctorj.version>1.5.4</asciidoctorj.version>
|
<asciidoctorj.version>1.5.6</asciidoctorj.version>
|
||||||
<asciidoctorj-pdf.src.version>1.5.0-alpha.11</asciidoctorj-pdf.src.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>
|
<asciidoctorj-pdf.plugin.version>1.5.0-alpha.15</asciidoctorj-pdf.plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ package com.baeldung.asciidoctor;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class AsciidoctorDemoTest {
|
public class AsciidoctorDemoIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_whenConverting_thenResultingHTMLCode() {
|
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)
|
- [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)
|
- [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)
|
- [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)
|
||||||
|
|||||||
@@ -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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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)
|
- [Create a Symbolic Link with Java](http://www.baeldung.com/java-symlink)
|
||||||
- [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter)
|
- [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter)
|
||||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
- [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)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import java.net.URLConnection;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@@ -50,7 +51,7 @@ public class FileOperationsManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
|
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
|
||||||
String expectedData = "BSD License";
|
String expectedData = "MIT License";
|
||||||
|
|
||||||
Class clazz = Matchers.class;
|
Class clazz = Matchers.class;
|
||||||
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
|
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
|
||||||
@@ -79,7 +80,7 @@ public class FileOperationsManualTest {
|
|||||||
|
|
||||||
ClassLoader classLoader = getClass().getClassLoader();
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
File file = new File(classLoader.getResource("fileTest.txt").getFile());
|
File file = new File(classLoader.getResource("fileTest.txt").getFile());
|
||||||
String data = FileUtils.readFileToString(file);
|
String data = FileUtils.readFileToString(file, "UTF-8");
|
||||||
|
|
||||||
assertEquals(expectedData, data.trim());
|
assertEquals(expectedData, data.trim());
|
||||||
}
|
}
|
||||||
@@ -102,12 +103,11 @@ public class FileOperationsManualTest {
|
|||||||
|
|
||||||
Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI());
|
Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI());
|
||||||
|
|
||||||
StringBuilder data = new StringBuilder();
|
|
||||||
Stream<String> lines = Files.lines(path);
|
Stream<String> lines = Files.lines(path);
|
||||||
lines.forEach(line -> data.append(line).append("\n"));
|
String data = lines.collect(Collectors.joining("\n"));
|
||||||
lines.close();
|
lines.close();
|
||||||
|
|
||||||
assertEquals(expectedData, data.toString().trim());
|
assertEquals(expectedData, data.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readFromInputStream(InputStream inputStream) throws IOException {
|
private String readFromInputStream(InputStream inputStream) throws IOException {
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
Hello World from fileTest.txt!!!
|
||||||
@@ -151,3 +151,4 @@
|
|||||||
- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist)
|
- [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)
|
- [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)
|
- [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)
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.ternaryoperator;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TernaryOperatorUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACondition_whenUsingTernaryOperator_thenItEvaluatesConditionAndReturnsAValue() {
|
||||||
|
int number = 10;
|
||||||
|
String msg = number > 10 ? "Number is greater than 10" : "Number is less than or equal to 10";
|
||||||
|
|
||||||
|
assertThat(msg).isEqualTo("Number is less than or equal to 10");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenATrueCondition_whenUsingTernaryOperator_thenOnlyExpression1IsEvaluated() {
|
||||||
|
int exp1 = 0, exp2 = 0;
|
||||||
|
int result = 12 > 10 ? ++exp1 : ++exp2;
|
||||||
|
|
||||||
|
assertThat(exp1).isEqualTo(1);
|
||||||
|
assertThat(exp2).isEqualTo(0);
|
||||||
|
assertThat(result).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAFalseCondition_whenUsingTernaryOperator_thenOnlyExpression2IsEvaluated() {
|
||||||
|
int exp1 = 0, exp2 = 0;
|
||||||
|
int result = 8 > 10 ? ++exp1 : ++exp2;
|
||||||
|
|
||||||
|
assertThat(exp1).isEqualTo(0);
|
||||||
|
assertThat(exp2).isEqualTo(1);
|
||||||
|
assertThat(result).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenANestedCondition_whenUsingTernaryOperator_thenCorrectValueIsReturned() {
|
||||||
|
int number = 6;
|
||||||
|
String msg = number > 10 ? "Number is greater than 10" : number > 5 ? "Number is greater than 5" : "Number is less than or equal to 5";
|
||||||
|
|
||||||
|
assertThat(msg).isEqualTo("Number is greater than 5");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,3 +34,6 @@
|
|||||||
- [Kotlin Constructors](https://www.baeldung.com/kotlin-constructors)
|
- [Kotlin Constructors](https://www.baeldung.com/kotlin-constructors)
|
||||||
- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern)
|
- [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)
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax</groupId>
|
<groupId>javax</groupId>
|
||||||
<artifactId>javaee-api</artifactId>
|
<artifactId>javaee-api</artifactId>
|
||||||
<version>${javaee-api.version}</version>
|
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user