Skip to content

Commit 5f23e39

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-2550
2 parents 29452ab + 52db7dd commit 5f23e39

159 files changed

Lines changed: 3375 additions & 308 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.java8.lambda.methodreference;
2+
3+
public class Bicycle {
4+
5+
private String brand;
6+
private Integer frameSize;
7+
8+
public Bicycle(String brand) {
9+
this.brand = brand;
10+
this.frameSize = 0;
11+
}
12+
13+
public Bicycle(String brand, Integer frameSize) {
14+
this.brand = brand;
15+
this.frameSize = frameSize;
16+
}
17+
18+
public String getBrand() {
19+
return brand;
20+
}
21+
22+
public void setBrand(String brand) {
23+
this.brand = brand;
24+
}
25+
26+
public Integer getFrameSize() {
27+
return frameSize;
28+
}
29+
30+
public void setFrameSize(Integer frameSize) {
31+
this.frameSize = frameSize;
32+
}
33+
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.java8.lambda.methodreference;
2+
3+
import java.util.Comparator;
4+
5+
public class BicycleComparator implements Comparator<Bicycle> {
6+
7+
@Override
8+
public int compare(Bicycle a, Bicycle b) {
9+
return a.getFrameSize()
10+
.compareTo(b.getFrameSize());
11+
}
12+
13+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.java8.lambda.methodreference;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.BiFunction;
7+
8+
import org.junit.Test;
9+
10+
public class MethodReferenceExamples {
11+
12+
private static <T> void doNothingAtAll(Object... o) {
13+
}
14+
15+
;
16+
17+
@Test
18+
public void referenceToStaticMethod() {
19+
List<String> messages = Arrays.asList("Hello", "Baeldung", "readers!");
20+
messages.forEach((word) -> {
21+
System.out.println(word);
22+
});
23+
messages.forEach(System.out::println);
24+
}
25+
26+
@Test
27+
public void referenceToInstanceMethodOfParticularObject() {
28+
BicycleComparator bikeFrameSizeComparator = new BicycleComparator();
29+
createBicyclesList().stream()
30+
.sorted((a, b) -> bikeFrameSizeComparator.compare(a, b));
31+
createBicyclesList().stream()
32+
.sorted(bikeFrameSizeComparator::compare);
33+
}
34+
35+
@Test
36+
public void referenceToInstanceMethodOfArbitratyObjectOfParticularType() {
37+
List<Integer> numbers = Arrays.asList(5, 3, 50, 24, 40, 2, 9, 18);
38+
numbers.stream()
39+
.sorted((a, b) -> Integer.compare(a, b));
40+
numbers.stream()
41+
.sorted(Integer::compare);
42+
}
43+
44+
@Test
45+
public void referenceToConstructor() {
46+
BiFunction<String, Integer, Bicycle> bikeCreator = (brand, frameSize) -> new Bicycle(brand, frameSize);
47+
BiFunction<String, Integer, Bicycle> bikeCreatorMethodReference = Bicycle::new;
48+
List<Bicycle> bikes = new ArrayList<>();
49+
bikes.add(bikeCreator.apply("Giant", 50));
50+
bikes.add(bikeCreator.apply("Scott", 20));
51+
bikes.add(bikeCreatorMethodReference.apply("Trek", 35));
52+
bikes.add(bikeCreatorMethodReference.apply("GT", 40));
53+
}
54+
55+
@Test
56+
public void referenceToConstructorSimpleExample() {
57+
List<String> bikeBrands = Arrays.asList("Giant", "Scott", "Trek", "GT");
58+
bikeBrands.stream()
59+
.map(Bicycle::new)
60+
.toArray(Bicycle[]::new);
61+
}
62+
63+
@Test
64+
public void limitationsAndAdditionalExamples() {
65+
createBicyclesList().forEach(b -> System.out.printf("Bike brand is '%s' and frame size is '%d'%n", b.getBrand(), b.getFrameSize()));
66+
createBicyclesList().forEach((o) -> MethodReferenceExamples.doNothingAtAll(o));
67+
}
68+
69+
private List<Bicycle> createBicyclesList() {
70+
List<Bicycle> bikes = new ArrayList<>();
71+
bikes.add(new Bicycle("Giant", 50));
72+
bikes.add(new Bicycle("Scott", 20));
73+
bikes.add(new Bicycle("Trek", 35));
74+
bikes.add(new Bicycle("GT", 40));
75+
return bikes;
76+
}
77+
78+
}

‎core-java-collections-list/pom.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@
3636
<version>${lombok.version}</version>
3737
<scope>provided</scope>
3838
</dependency>
39+
40+
<dependency>
41+
<groupId>net.sf.trove4j</groupId>
42+
<artifactId>trove4j</artifactId>
43+
<version>3.0.2</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>it.unimi.dsi</groupId>
47+
<artifactId>fastutil</artifactId>
48+
<version>8.1.0</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>colt</groupId>
52+
<artifactId>colt</artifactId>
53+
<version>1.2.0</version>
54+
</dependency>
3955
</dependencies>
4056

4157
<properties>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.list.primitive;
2+
3+
import com.google.common.primitives.ImmutableIntArray;
4+
import com.google.common.primitives.Ints;
5+
import gnu.trove.list.array.TIntArrayList;
6+
import it.unimi.dsi.fastutil.ints.IntArrayList;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.OptionalDouble;
11+
import java.util.function.IntPredicate;
12+
import java.util.stream.IntStream;
13+
14+
public class PrimitiveCollections {
15+
16+
public static void main(String[] args) {
17+
18+
int[] primitives = new int[] {5, 10, 0, 2, -8};
19+
20+
guavaPrimitives(primitives);
21+
22+
intStream(primitives);
23+
24+
TIntArrayList tList = new TIntArrayList(primitives);
25+
26+
cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList(primitives);
27+
28+
IntArrayList fastUtilList = new IntArrayList(primitives);
29+
30+
System.out.println(tList);
31+
32+
System.out.println(coltList);
33+
34+
System.out.println(fastUtilList);
35+
}
36+
37+
private static void intStream(int[] primitives) {
38+
39+
IntStream stream = IntStream.of(5, 10, 0, 2, -8);
40+
41+
IntStream newStream = IntStream.of(primitives);
42+
43+
OptionalDouble average = stream.filter(i -> i > 0).average();
44+
}
45+
46+
47+
private static void guavaPrimitives(int[] primitives) {
48+
49+
ImmutableIntArray immutableIntArray = ImmutableIntArray.builder().addAll(primitives).build();
50+
System.out.println(immutableIntArray);
51+
}
52+
}

‎core-java-io/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@
3636
- [Reading a CSV File into an Array](https://www.baeldung.com/java-csv-file-array)
3737
- [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader)
3838
- [How to Get the File Extension of a File in Java](http://www.baeldung.com/java-file-extension)
39-
- [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type)
39+
- [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type)
40+
- [Create a Directory in Java](https://www.baeldung.com/java-create-directory)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.files;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.DirectoryStream;
6+
import java.nio.file.FileVisitResult;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.nio.file.SimpleFileVisitor;
11+
import java.nio.file.attribute.BasicFileAttributes;
12+
import java.util.HashSet;
13+
import java.util.Set;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
16+
17+
public class ListFiles {
18+
public static final int DEPTH = 1;
19+
20+
public Set<String> listFilesUsingJavaIO(String dir) {
21+
return Stream.of(new File(dir).listFiles())
22+
.filter(file -> !file.isDirectory())
23+
.map(File::getName)
24+
.collect(Collectors.toSet());
25+
}
26+
27+
public Set<String> listFilesUsingFileWalk(String dir, int depth) throws IOException {
28+
try (Stream<Path> stream = Files.walk(Paths.get(dir), depth)) {
29+
return stream.filter(file -> !Files.isDirectory(file))
30+
.map(Path::getFileName)
31+
.map(Path::toString)
32+
.collect(Collectors.toSet());
33+
}
34+
}
35+
36+
public Set<String> listFilesUsingFileWalkAndVisitor(String dir) throws IOException {
37+
Set<String> fileList = new HashSet<>();
38+
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
39+
@Override
40+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
41+
if (!Files.isDirectory(file)) {
42+
fileList.add(file.getFileName()
43+
.toString());
44+
}
45+
return FileVisitResult.CONTINUE;
46+
}
47+
});
48+
return fileList;
49+
}
50+
51+
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
52+
Set<String> fileList = new HashSet<>();
53+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
54+
for (Path path : stream) {
55+
if (!Files.isDirectory(path)) {
56+
fileList.add(path.getFileName()
57+
.toString());
58+
}
59+
}
60+
}
61+
return fileList;
62+
}
63+
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.file;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
import org.junit.Test;
10+
11+
import com.baeldung.files.ListFiles;
12+
13+
public class ListFilesUnitTest {
14+
15+
private ListFiles listFiles = new ListFiles();
16+
private String DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
17+
private static final int DEPTH = 1;
18+
private Set<String> EXPECTED_FILE_LIST = new HashSet<String>() {
19+
{
20+
add("test.xml");
21+
add("employee.json");
22+
add("students.json");
23+
add("country.txt");
24+
}
25+
};
26+
27+
@Test
28+
public void givenDir_whenUsingJAVAIO_thenListAllFiles() throws IOException {
29+
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY));
30+
}
31+
32+
@Test
33+
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException {
34+
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH));
35+
}
36+
37+
@Test
38+
public void givenDir_whenWalkingTreeWithVisitor_thenListAllFiles() throws IOException {
39+
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalkAndVisitor(DIRECTORY));
40+
}
41+
42+
@Test
43+
public void givenDir_whenUsingDirectoryStream_thenListAllFiles() throws IOException {
44+
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingDirectoryStream(DIRECTORY));
45+
}
46+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a sample txt file for unit test ListFilesUnitTest
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)