Skip to content

Instantly share code, notes, and snippets.

//This is the class that creates the two “Singleton” instances on the same JVM:
import java.lang.reflect.Method;
public class ClassLoaderSingletonBreakingTest {
public static void main(String[] args) {
MyClassLoader cl1 = new MyClassLoader();
Object o1;
try {
@codinko
codinko / JsonNodeReadingExample1.java
Last active January 20, 2020 02:57
Read elements from the Json Tree - say responseData.json
String jsonString = Util.readFileFromClassPathAndConvert2String(fileNameInCP);
JsonNode root = objectMapper.readTree(jsonString);
---------------------
APPROACH-1
---------------------
JsonNode nodes = root.get("companies");
// imp note that you are going to iterate over the node values, not the keys ...
Iterator<JsonNode> itr = nodes.iterator();
@codinko
codinko / readFileFromClassPathAndConvert2String.java
Created January 19, 2020 17:06
readFileFromClassPathAndConvert2String , Convert InputStream to String
package com.codinko.json.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Util {
public static String readFileFromClassPathAndConvert2String(String filenameInCP) {
@codinko
codinko / pom.xml
Created November 22, 2019 21:40
mavenprofilepom1
<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.codinko</groupId>
<artifactId>mavenprofilesample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<profiles>
@codinko
codinko / MainCachingGet.java
Created November 21, 2019 01:01
MainClassCachingGet
package com.codinko.caching;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
@codinko
codinko / EmployeeDAOCaching.java
Created November 21, 2019 00:59
EmployeeDAOCachingGetEmployee
package com.codinko.caching;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@codinko
codinko / Employee.java
Created November 21, 2019 00:58
EmployeeClassWithIdNameDesig
package com.codinko.caching;
public class Employee {
private int id;
private String name;
private String designation;
public Employee(int id, String name, String designation) {
super();
this.id = id;
@codinko
codinko / java.util.Optional.map.codesnippet1.java
Last active November 19, 2018 01:01
java.util.Optional.map code snippet-1
// 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
@codinko
codinko / NoteUserJavaStreamUsageWithMapReduce.java
Last active November 19, 2018 00:39
JavaStreamUsageWithMapReduce
// 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