Last active
September 27, 2015 23:48
-
-
Save PierreMage/1350982 to your computer and use it in GitHub Desktop.
Java snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.Math.*; | |
//TODO: use BigInteger instead of long | |
public class Factorial { | |
public long recursiveFactorial(long n) { | |
if (n == 0) { | |
return 1; | |
} | |
return n * recursiveFactorial(n - 1); | |
} | |
public long analyticFactorial(long n) { | |
return round(exp(Gamma.logGamma(n + 1))); | |
} | |
/** | |
* @see http://introcs.cs.princeton.edu/java/91float/Gamma.java.html | |
*/ | |
public static class Gamma { | |
static double logGamma(double x) { | |
double tmp = (x - 0.5) * log(x + 4.5) - (x + 4.5); | |
double ser = 1.0 + 76.18009173 / (x + 0) - 86.50532033 / (x + 1) | |
+ 24.01409822 / (x + 2) - 1.231739516 / (x + 3) | |
+ 0.00120858003 / (x + 4) - 0.00000536382 / (x + 5); | |
return tmp + log(ser * sqrt(2 * PI)); | |
} | |
static double gamma(double x) { | |
return exp(logGamma(x)); | |
} | |
} | |
public static void main(String[] args) { | |
Factorial f = new Factorial(); | |
for (int i = 0; i < 100; i++) { | |
long a = f.analyticFactorial(i); | |
long r = f.recursiveFactorial(i); | |
if (a != r) { | |
System.out.println(String.format("different values for i=%d: %d != %d", i, a, r)); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.Math.*; | |
public class Fibonacci { | |
public long recursiveFibonacci(long n) { | |
if (n == 0) { | |
return 0; | |
} | |
if (n == 1) { | |
return 1; | |
} | |
return recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2); | |
} | |
public long analyticFibonacci(long n) { | |
return round((pow(.5 + .5 * sqrt(5.0), n) - pow(.5 -.5 * sqrt(5.0), n)) / sqrt(5.0)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using http://hc.apache.org/ | |
public void httpPostWithBasicAuthentication(byte[] byteArray) throws IOException { | |
HttpPost httpPost = new HttpPost(); | |
httpPost.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("user", "password"), "UTF-8", false)); | |
httpPost.setHeader("Content-Type", "application/octet-stream"); | |
httpPost.setEntity(new ByteArrayEntity(byteArray)); | |
HttpClient httpClient = new DefaultHttpClient(); | |
httpClient.execute(httpPost); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import java.nio.file.*; | |
import com.sun.net.httpserver.*; | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
int port = Integer.parseInt(System.getProperty("app.port", "8080")); | |
System.out.println(String.valueOf(port)); | |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); | |
server.createContext("/", new HttpHandler() { | |
@Override | |
public void handle(HttpExchange exchange) throws IOException { | |
String uri = exchange.getRequestURI().toString(); | |
switch (uri) { | |
case "/": { | |
byte[] body = "Hello World!".getBytes(); | |
exchange.getResponseHeaders().add("Content-Type", "text/html"); | |
exchange.sendResponseHeaders(200, body.length); | |
exchange.getResponseBody().write(body); | |
break; | |
} | |
case "/html": { | |
byte[] body = Files.readAllBytes(Paths.get("app", "index.html")); | |
exchange.getResponseHeaders().add("Content-Type", "text/html"); | |
exchange.sendResponseHeaders(200, body.length); | |
exchange.getResponseBody().write(body); | |
break; | |
} | |
default: { | |
exchange.sendResponseHeaders(404, 0); | |
} | |
} | |
exchange.close(); | |
} | |
}); | |
server.start(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java < 7 | |
public void readFile(String pathname) throws IOException { | |
File file = new File(pathname); | |
BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); | |
String line; | |
try { | |
while ((line = bufferedReader.readLine()) != null) { | |
//do something | |
} | |
} finally { | |
bufferedReader.close(); | |
} | |
} | |
// Java >= 7 | |
public void readFile(String pathname) throws IOException { | |
File file = new File(pathname); | |
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { | |
String line; | |
while ((line = bufferedReader.readLine()) != null) { | |
//do something | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("path/to/file"); | |
//with Spring | |
InputStream inputStream = new ClassPathRessource("path/to/file").getInputStream(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java < 7 | |
public void writeToFile(String pathname, List<Output> outputs) throws IOException { | |
File file = new File(pathname); | |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); | |
try { | |
for (int i = 0; i < outputs.size(); i++) { | |
Output output = outputs.get(i); | |
bufferedWriter.write(output.toString()); | |
bufferedWriter.newLine(); | |
} | |
} finally { | |
bufferedWriter.close(); | |
} | |
} | |
// Java >= 7 | |
public void writeToFile(String pathname, List<Output> outputs) throws IOException { | |
File file = new File(pathname); | |
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) { | |
for (Output output : outputs) { | |
bufferedWriter.write(output.toString()); | |
bufferedWriter.newLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment