Skip to content

Instantly share code, notes, and snippets.

@yssharma
yssharma / Bfs.java
Created October 31, 2012 10:43
BFS - Breadth First Java (Confused Coders)
// Came across this cool code with neat code style for BFS
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
class Node {
String data;
boolean visited;
@yssharma
yssharma / BfsDfs.java
Created October 31, 2012 11:13
BFS + DFS (Breadth First Search + Depth First Search) (Confused Coders)
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
class Node {
String data;
boolean visited;
List<Node> neighbours = new ArrayList<Node>();
@yssharma
yssharma / References.java
Created November 3, 2012 13:19
Java Weak - Soft - Phantom References (Confused Coders)
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
class SomeClass{
@yssharma
yssharma / ComparatorComparavle.java
Created November 3, 2012 13:39
A sample method sorting a list by comparable and comparator (Confused Coders)
public void someMethod(){
/** Create a Emp List and add dummy values to Sort **/
List<Employee> myEmpList = new ArrayList<Employee>();
Employee emp = new Employee();
emp.salary = 5000;
emp.name = "Arnold";
myEmpList.add(emp);
emp = new Employee();
emp.salary = 15000;
emp.name = "Clooney";
@yssharma
yssharma / LinkedList.java
Created November 3, 2012 13:45
A linked list implementation in java providing few common utility methods (Confused Coders)
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
@yssharma
yssharma / Queue.java
Created November 3, 2012 13:53
A java implementation of a Queue (Confused Coders)
class Item {
int data;
Item next;
public Item(int data) {
this.data = data;
}
}
@yssharma
yssharma / Stack.java
Created November 3, 2012 13:58
A basic java implementation of Stack data structure. (Confused Coders)
class Item {
intdata;
Item next;
public Item(int data) {
this.data = data;
}
}
@yssharma
yssharma / ConnectionPool.java
Created November 3, 2012 14:02
An implementation of a Java connection pool (Confused Coders)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/** A Connection Pool with 5 Available Connections **/
class ConnectionPool {
@yssharma
yssharma / ProducerConsumer.java
Created November 3, 2012 14:08
A threaded producer consumer class (Confused Coders)
/** The Resource Class - The root cause of all fight **/
class Resource {
private Boolean isProduced = false;
private String data = "EMPTY";
/** Put method : puts only if is not already produced **/
public synchronized void put(String data) throws InterruptedException {
if (isProduced) {
wait(); // Not Consumed Yet,Wait for consumer's signal.
@yssharma
yssharma / Queue.java
Created November 6, 2012 07:46
A Queue data structure implementation in java - Uses Object chaining (Confused Coders)
class Item{
Integer data;
Item next;
public Item(){}
public Item(Integer data){