Instantly share code, notes, and snippets.

View java.util.Optional.map.codesnippet1.java
// Get all notes by userId
java.util.Optional;
java.util.Optional.map()
java.util.Optional class
// A container object which may or may not contain a non-null value.
// If a value is present, {@code isPresent()} will return {@code true} and
// {@code get()} will return the value.
// Approach-1
View NoteUserJavaStreamUsageWithMapReduce.java
// How to get a Note from noteId and userId
private Note findNote(int noteId, List<Note> notes) {
return notes.stream()
.filter(note -> note.getNoteId() == noteId)
.findAny()
.orElse(null);
}
// Approach-1
View deleteNoteUsageOfJavaStream.java
public boolean deleteNote(String userId, int noteId) {
NoteUser dbUser = this.noteRepository.findById(userId).orElse(null);
Note dbNote = null;
if(dbUser != null) {
dbNote = this.findNote(noteId, dbUser.getNotes());
if(dbNote != null) {
dbUser.getNotes().remove(dbNote);
dbUser = this.noteRepository.save(dbUser);
}
View CodeParsingJWT_ReceivedFromRequest.java
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
final HttpServletRequest req = (HttpServletRequest) request;
final String authHeader = req.getHeader("authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new ServletException("Missing or invalid Authorization header");
}
View AuthenticationControllerCreatingJWT.java
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
@RestController
public class AuthenticationController { ...
@ApiOperation(value = "Validate the user login")
@PostMapping("/api/authentication/login")
public ResponseEntity<?> userLogin(@RequestBody User user) {
String jwtToken = "";
View application.properties
server.port=8889
hello.greeting= nice to meet you
hello.version= 2.0
View HelloController.java
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
View manifest.yml
---
applications:
- name: hello-boot
instances: 1
host: hello-boot-${random-word}
path: target/hello-boot-1.0.jar
View simple-inefficient-quicksort-algo
// Sort an array using a simple but inefficient quicksort.
public int[] quicksortSimple(int[] data) {
if (data.length < 2) {
return data;
}
int pivotIndex = data.length / 2;
int pivotValue = data[pivotIndex];
int leftCount = 0;
// Count how many are less than the pivot
View SwaggerChangesForSpringREST.java
/* Mentioning all the swagger changes made on top of
Spring REST app: https://gist.github.com/codinko/79986dbed38d0f2b2d1b */
---------------------------------------
Modified File: pom.xml
---------------------------------------
<properties>
<swagger.version>1.0.2</swagger.version>
</properties>
<dependencies>
<dependency>