Skip to content

Commit 5e0920b

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 908b766 + 4f0ae5d commit 5e0920b

7 files changed

Lines changed: 178 additions & 1 deletion

File tree

‎core-java/src/main/java/com/baeldung/array/ArrayInverter.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public void invertUsingCollectionsReverse(Object[] array) {
2525
}
2626

2727
public Object[] invertUsingStreams(final Object[] array) {
28-
return IntStream.range(1, array.length + 1).mapToObj(i -> array[array.length - i]).toArray();
28+
return IntStream.rangeClosed(1, array.length)
29+
.mapToObj(i -> array[array.length - i])
30+
.toArray();
2931
}
3032

3133
public void invertUsingCommonsLang(Object[] array) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.collection;
2+
3+
import java.util.ConcurrentModificationException;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.Map;
7+
import java.util.TreeMap;
8+
9+
import org.hamcrest.Matchers;
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
13+
public class WhenComparingTreeMapVsHashMap {
14+
15+
@Test
16+
public void whenInsertObjectsTreeMap_thenNaturalOrder() {
17+
Map<Integer, String> treemap = new TreeMap<>();
18+
treemap.put(3, "TreeMap");
19+
treemap.put(2, "vs");
20+
treemap.put(1, "HashMap");
21+
Assert.assertThat(treemap.keySet(), Matchers.contains(1, 2, 3));
22+
}
23+
24+
@Test(expected = NullPointerException.class)
25+
public void whenInsertNullInTreeMap_thenException() {
26+
Map<Integer, String> treemap = new TreeMap<>();
27+
treemap.put(null, "NullPointerException");
28+
}
29+
30+
@Test
31+
public void whenInsertObjectsHashMap_thenRandomOrder() {
32+
Map<Integer, String> hashmap = new HashMap<>();
33+
hashmap.put(3, "TreeMap");
34+
hashmap.put(2, "vs");
35+
hashmap.put(1, "HashMap");
36+
Assert.assertThat(hashmap.keySet(), Matchers.containsInAnyOrder(1, 2, 3));
37+
}
38+
39+
@Test
40+
public void whenInsertNullInHashMap_thenInsertsNull() {
41+
Map<Integer, String> hashmap = new HashMap<>();
42+
hashmap.put(null, null);
43+
Assert.assertNull(hashmap.get(null));
44+
}
45+
46+
@Test
47+
public void givenHashMapAndTreeMap_whenputDuplicates_thenOnlyUnique() {
48+
Map<Integer, String> treeMap = new HashMap<>();
49+
treeMap.put(1, "Baeldung");
50+
treeMap.put(1, "Baeldung");
51+
52+
Assert.assertTrue(treeMap.size() == 1);
53+
54+
Map<Integer, String> treeMap2 = new TreeMap<>();
55+
treeMap2.put(1, "Baeldung");
56+
treeMap2.put(1, "Baeldung");
57+
58+
Assert.assertTrue(treeMap2.size() == 1);
59+
}
60+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.spring.cloud.connectors.heroku.book;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Book {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
12+
private Long id;
13+
private String title;
14+
private String author;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
public String getTitle() {
25+
return title;
26+
}
27+
28+
public void setTitle(String title) {
29+
this.title = title;
30+
}
31+
32+
public String getAuthor() {
33+
return author;
34+
}
35+
36+
public void setAuthor(String author) {
37+
this.author = author;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.spring.cloud.connectors.heroku.book;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.*;
5+
6+
@RestController
7+
@RequestMapping("/books")
8+
public class BookController {
9+
10+
private final BookService bookService;
11+
12+
@Autowired
13+
public BookController(BookService bookService) {
14+
this.bookService = bookService;
15+
}
16+
17+
@GetMapping("/{bookId}")
18+
public Book findBook(@PathVariable Long bookId) {
19+
return bookService.findBookById(bookId);
20+
}
21+
22+
@PostMapping
23+
public Book createBook(@RequestBody Book book) {
24+
return bookService.createBook(book);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.spring.cloud.connectors.heroku.book;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface BookRepository extends JpaRepository<Book, Long>{
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.spring.cloud.connectors.heroku.book;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.transaction.annotation.Propagation;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Service
9+
@Transactional(readOnly = true)
10+
public class BookService {
11+
private final BookRepository bookRepository;
12+
13+
@Autowired
14+
public BookService(BookRepository bookRepository) {
15+
this.bookRepository = bookRepository;
16+
}
17+
18+
public Book findBookById(Long bookId) {
19+
return bookRepository.findOne(bookId);
20+
}
21+
22+
@Transactional(propagation = Propagation.REQUIRED)
23+
public Book createBook(Book book) {
24+
Book newBook = new Book();
25+
newBook.setTitle(book.getTitle());
26+
newBook.setAuthor(book.getAuthor());
27+
return bookRepository.save(newBook);
28+
}
29+
}

‎testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6+
import java.util.ConcurrentModificationException;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
610
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.function.Executable;
712

813
public class ExceptionUnitTest {
914

@@ -22,4 +27,14 @@ void assertThrowsException() {
2227
Integer.valueOf(str);
2328
});
2429
}
30+
31+
@Test
32+
public void whenModifyMapDuringIteration_thenThrowExecption() {
33+
Map<Integer, String> hashmap = new HashMap<>();
34+
hashmap.put(1, "One");
35+
hashmap.put(2, "Two");
36+
37+
Executable executable = () -> hashmap.forEach((key, value) -> hashmap.remove(1));
38+
assertThrows(ConcurrentModificationException.class, executable);
39+
}
2540
}

0 commit comments

Comments
 (0)