Skip to content

Instantly share code, notes, and snippets.

@Jimexist
Jimexist / design for reuse.md
Last active April 8, 2016 03:07
Java short tutorials

Design for reuse

You should aim to design for reuse, otherwise, hide it!

The reasoning behind this is that your users, including yourself in future, will naturally ignore any documentation and convention that you put in code, and use it the way they like.

If you design the class, you should aim for reusability and otherwise hide them, leaving yourself with leeway to redo.

@Jimexist
Jimexist / immutability.md
Last active April 4, 2016 05:59
Java short tutorials

Prefer immutable objects by default

POJO

public class Person {
  
  private firstName;
  private lastName;
  private email;
@Jimexist
Jimexist / Solution.java
Created May 14, 2015 18:33
Solution to leetcode course schedule II
import java.util.*;
public class Solution {
public static void main(String[] args) {
int[][] deps = {{1, 0}};
System.out.printf("%s\n", Arrays.toString(new Solution().findOrder(2, deps)));
}
private static Integer append(int i, Map<Integer, List<Integer>> map, Map<Integer, Integer> depthMap) {
@Jimexist
Jimexist / LRUCache.java
Last active August 29, 2015 14:16
LRUCache in Java (not fully tested)
import java.util.HashMap;
import java.util.Map;
public class LRUCache<K, V> {
private final Map<K, Node<K, V>> map;
private final Node<K, V> head, tail;
private final int maxSize;
private static class Node<K, V> {
@Jimexist
Jimexist / OOSTest.java
Created February 8, 2015 03:31
OOSTest.java
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class OOSTest {
public static void main(String[] args) throws Exception {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
@Jimexist
Jimexist / InterruptThreadPools.java
Created November 12, 2014 05:48
InterruptThreadPools.java
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class InterruptThreadPools<V> {
private final ExecutorService threadPool = Executors.newCachedThreadPool();
private final CompletionService<V> completionService = new ExecutorCompletionService<>(threadPool);
private final BlockingQueue<Future<V>> tasks = new LinkedBlockingQueue<>();
@Jimexist
Jimexist / container.c
Created October 19, 2014 18:51
rain container
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define max(A, B) ((A) > (B) ? (A) : (B))
#define min(A, B) ((A) < (B) ? (A) : (B))
int contained(int *cont, size_t len) {
if (len == 0) {
return 0;
@Jimexist
Jimexist / subset.hs
Created October 19, 2014 18:50
subset n k
subset :: Int -> Int -> [[Int]]
subset 0 _ = [[]]
subset _ 0 = [[]]
subset n k = [i:y | i <- [k..n], y <- subset (i-1) (k-1)]
@Jimexist
Jimexist / BadNeighbors.java
Created October 2, 2014 00:53
BadNeighbors
public class BadNeighbors {
public static void main(String[] args) {
int[] collections = new int[args.length];
for (int i=0; i<args.length; ++i) {
collections[i] = Integer.parseInt(args[i]);
}
System.out.printf("%d\n", maxDonations(collections));
}
@Jimexist
Jimexist / ZigZag.java
Created October 1, 2014 23:49
Another solution for ZigZag
public class ZigZag {
public static int longestZigZag(int[] sequence) {
final int n = sequence.length;
int[] positive = new int[n];
int[] negative = new int[n];
for (int i=0; i<n; ++i) {
positive[i] = negative[i] = 1;
}