[JAVA-616] core-java-arrays-operations-basic
* Creation * Moved code from https://www.baeldung.com/java-initialize-array * Moved code from https://www.baeldung.com/java-common-array-operations * Moved code from https://www.baeldung.com/java-add-element-to-array-vs-list * Moved code from https://www.baeldung.com/java-array-contains-value * Moved code from https://www.baeldung.com/java-array-remove-element * Moved code from https://www.baeldung.com/java-array-remove-first-element * Moved code from https://www.baeldung.com/java-array-add-element-at-the-end
This commit is contained in:
parent
dcd863f5d6
commit
e36771a763
|
@ -4,10 +4,5 @@ This module contains articles about Java arrays
|
|||
|
||||
## Relevant Articles
|
||||
|
||||
- [Extending an Array’s Length](https://www.baeldung.com/java-array-add-element-at-the-end)
|
||||
- [Array Operations in Java](https://www.baeldung.com/java-common-array-operations)
|
||||
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
|
||||
- [Removing an Element from an Array in Java](https://www.baeldung.com/java-array-remove-element)
|
||||
- [Removing the First Element of an Array](https://www.baeldung.com/java-array-remove-first-element)
|
||||
- [Adding an Element to a Java Array vs an ArrayList](https://www.baeldung.com/java-add-element-to-array-vs-list)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-arrays)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.array.operations;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ArraysIntersectionOperations {
|
||||
|
||||
public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(Arrays.asList(b)::contains)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static Integer[] intersectionSet(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(Arrays.asList(b)::contains)
|
||||
.distinct()
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(new LinkedList<>(Arrays.asList(b))::remove)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ package com.baeldung.array.operations;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.baeldung.array.operations.ArrayOperations.*;
|
||||
import static com.baeldung.array.operations.ArraysIntersectionOperations.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class IntersectionUnitTest {
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
## Core Java Arrays - Basic Operations
|
||||
|
||||
This module contains articles about basic operations on arrays in Java
|
||||
|
||||
Basic operations would cover anything elementary we should expect to be able to achieve on a collection type:
|
||||
|
||||
- Adding/Removing elements
|
||||
- Getting elements
|
||||
- Iterating
|
||||
|
||||
### Relevant Articles:
|
||||
- [Initializing Arrays in Java](https://www.baeldung.com/java-initialize-array)
|
||||
- [Array Operations in Java](https://www.baeldung.com/java-common-array-operations)
|
||||
- [Adding an Element to a Java Array vs an ArrayList](https://www.baeldung.com/java-add-element-to-array-vs-list)
|
||||
- [Check if a Java Array Contains a Value](https://www.baeldung.com/java-array-contains-value)
|
||||
- [Removing an Element from an Array in Java](https://www.baeldung.com/java-array-remove-element)
|
||||
- [Removing the First Element of an Array](https://www.baeldung.com/java-array-remove-first-element)
|
||||
- [Extending an Array’s Length](https://www.baeldung.com/java-array-add-element-at-the-end)
|
||||
- [[More advanced operations -->]](/core-java-modules/core-java-arrays-operations-advanced)
|
|
@ -0,0 +1,78 @@
|
|||
<?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">
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>core-java-arrays-operations-basic</artifactId>
|
||||
<name>core-java-arrays-operations-basic</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${shade.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>benchmarks</finalName>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Benchmarking -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<shade.plugin.version>3.2.0</shade.plugin.version>
|
||||
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
|
||||
<jmh.version>1.19</jmh.version>
|
||||
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -9,7 +9,7 @@ public class ArrayBenchmarkRunner {
|
|||
public static void main(String[] args) throws Exception {
|
||||
|
||||
Options options = new OptionsBuilder()
|
||||
.include(SearchArrayUnitTest.class.getSimpleName()).threads(1)
|
||||
.include(SearchArrayBenchmark.class.getSimpleName()).threads(1)
|
||||
.forks(1).shouldFailOnError(true).shouldDoGC(true)
|
||||
.jvmArgs("-server").build();
|
||||
|
|
@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit;
|
|||
@BenchmarkMode(Mode.AverageTime)
|
||||
@Warmup(iterations = 5)
|
||||
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||
public class SearchArrayUnitTest {
|
||||
public class SearchArrayBenchmark {
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class SearchData {
|
|
@ -4,7 +4,5 @@ This module contains articles about Java arrays
|
|||
|
||||
### Relevant Articles:
|
||||
- [How to Copy an Array in Java](https://www.baeldung.com/java-array-copy)
|
||||
- [Check if a Java Array Contains a Value](https://www.baeldung.com/java-array-contains-value)
|
||||
- [Initializing Arrays in Java](https://www.baeldung.com/java-initialize-array)
|
||||
- [Find Sum and Average in a Java Array](https://www.baeldung.com/java-array-sum-average)
|
||||
- [[More -->]](/core-java-modules/core-java-arrays-2)
|
||||
|
|
|
@ -126,6 +126,7 @@
|
|||
<module>core-java-arrays-guides</module>
|
||||
<module>core-java-arrays-multidimensional</module>
|
||||
<module>core-java-arrays-convert</module>
|
||||
<module>core-java-arrays-operations-basic</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue