Skip to content

Instantly share code, notes, and snippets.

@codinko
codinko / ConcurrentModificationExceptionDemo.java
Created October 3, 2015 23:48
ConcurrentModificationExceptionDemo in ArrayList
package com.codinko;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ConcurrentModificationExceptionDemo {
public static void main(String[] args) {
new ConcurrentModificationExceptionDemo().method1();
}
@codinko
codinko / GenericClassT.java
Last active October 8, 2015 19:34
Generics-part4
package com.codinko;
public class GenericClassT<T> {
private T t;
public T get() {
return this.t;
}
@codinko
codinko / GenericClass.java
Created October 8, 2015 22:12
Java Generics – Part5 – Generic methods - Sample Program
package com.codinko.generics;
public class GenericClass<T> {
private T t;
public T get() {
return this.t;
}
@codinko
codinko / JavaPassByValueProof.java
Last active January 30, 2016 01:41
Java is pass-by-value, and not pass-by-reference
/**
Pass By Value
In Java, all parameters are passed by value, i.e. assigning a method argument is not visible to the caller.
*/
Example 1:
@codinko
codinko / ThreadWaitDemo.java
Last active October 17, 2015 12:30
Threads and Locks - Object.wait() method
package com.codinko.example;
/**
*
* Feel free to play around with this code to try various topics covered under Threads & Locks:
*
* Theory reference: https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html
*
*/
public class ThreadWaitDemo {
/**
* 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 / ThreadJoinDemo.java
Created November 29, 2015 23:07
Use of Thread Join - say current thread must wait for another thread to terminate its execution[something like Pause]
/*
Output WITHOUT Join [although the mix sequence could vary but inconsistent]
Thread 1 arrived...
Thread 2 arrived...
Thread 1 working on 1...
Thread 2 working on 1...
Thread 1 working on 2...
Thread 2 working on 2...
Thread 2 working on 3...
@codinko
codinko / CompareTreeMapHashMap.java
Created December 18, 2015 04:45
TreeMap vs HashMap
package com.codinko.sample;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.TreeMap;
public class CompareTreeMapHashMap {
public static void main(String[] args) {
Employee emp1 = new Employee("Harley", 100);
Employee emp2 = new Employee("Peter", 100);
@codinko
codinko / Employee.java
Created December 18, 2015 04:48
hashcode() and equals() method implementation sample
package com.codinko.sample;
public class Employee {
String name;
Employee(String name) {
this.name = name;
}
@Override
@codinko
codinko / session-storage.html
Created December 25, 2015 23:55
HTML5 Session Storage example
<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter() {
if(typeof(Storage) !== "undefined") {
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
} else {
sessionStorage.clickcount = 1;