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 {
/**
* Aim: Set does not allow duplicates. So if you have a hashset of employee
* objects with same name and id, then will the Set allow them to be added?
*
* So: Set does not allow duplicates with the help of hashcode() and equals() method
* Basically when you add an element to Set, it checks if the element is already present.
*
* If you use HashSet, this checking is done like this:
* It internally make use of hashcode() and equals() to find out.
The add() method of HashSet -
@codinko
codinko / OddEvenThreadType1.java
Last active May 3, 2020 21:16
Java Threads - Print Odd numbers and even numbers using two threads
package com.codinko.oddeven;
/* OddEvenThreadType1: One thread prints odd, other thread prints even so o/p will be 0 2 4 6 8 10 1 3 5 7 9 11
*/
public class OddEvenThreadType1 {
public static void main(String[] args) {
Runnable r1 = new Runnable1();
Runnable r2 = new Runnable2();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
@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;