Skip to content

Instantly share code, notes, and snippets.

@tamboer
tamboer / ImmutableClass.java
Created December 17, 2017 14:29
Immutable class java
/**
How Do We Create an Immutable Class
In order to create an immutable class, you should follow the below steps:
Make your class final, so that no other classes can extend it.
Make all your fields final, so that they’re initialized only once inside the constructor and never modified afterward.
package com.example.tdd;
import java.math.BigDecimal;
public class UtilizationController {
private CalculatesAverage calculatesAverage;
private CountsHoursInMonth countsHoursInMonth;
private AccumulatesActualsInMonth accumulatesActualsInMonth;
@tamboer
tamboer / Java Anagram Check.java
Created December 16, 2017 17:20 — forked from jsbonso/Java Anagram Check.java
Java - Check if 2 strings are an Anagram of each other
/**
* Check if the two strings are an anagram of each other.
* For example: LISTEN is an Anagram of SILENT.
*
* Two strings are an anagram of each other if the characters
* and the numbers of characters that are composing them,
* are exactly the same. So for example, SUE and USE are anagrams
* because both words have one E, one S and one U,
* hence: ESU = ESU.
@tamboer
tamboer / Reverse a List.java
Created December 16, 2017 17:18 — forked from jsbonso/Reverse a List.java
Reverse a List in Java
/**
* Reverses a given List
* using Collections.reverse() method
* @param list
* @author Jon Bonso
*/
public static void reverse(List<?> list) {
Collections.reverse(list);
}
@tamboer
tamboer / Java Contains Sum.java
Created December 16, 2017 17:10 — forked from jsbonso/Java Contains Sum.java
Java - Checks if the number is a sum of any of the 2 numbers in the list
import java.util.Arrays;
public class App {
public static void main( String[] args ) {
int[] inArray = {1, 3, 3, 5, 7};
int baseNum = 6;
System.out.println("Number List: " + Arrays.toString(inArray) );
@tamboer
tamboer / Java ForEach Lambda Example.java
Created December 16, 2017 17:07 — forked from jsbonso/Java ForEach Lambda Example.java
Java ForEach Lambda Example
/**
* Example of Java's Stream API - ForEach
* @author jonbonso
* @param args
*/
public static void main(String... args) {
System.out.println("Iterate using the traditional for loop...");
int[] numArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
@tamboer
tamboer / gist:80be356cf189bfb67e0a0bf376e437ba
Created December 16, 2017 17:05 — forked from chali/gist:f4ef583c6b6d0b100191
Java lambda formating example
private Map<Integer, ? extends Address> getAddressIndex(Optional<SubjectData> currentSubjectData) {
return currentSubjectData
.map((SubjectData subjectData) ->
subjectData.getAddresses().stream()
.filter((Address address) -> address.getEsoId() != null)
.collect(toMap(Address::getEsoId, identity()))
).orElse(emptyMap());
}
@tamboer
tamboer / SimpleJava8Lambda.java
Created December 16, 2017 12:50 — forked from darsen/SimpleJava8Lambda.java
Java Simple Lambda Koan
interface Caps {
public String capitalize(String name);
}
@Koan
public void simpleLambda() {
Caps caps = (String n) -> {
return n.toUpperCase();
};
String capitalized = caps.capitalize("James");
@tamboer
tamboer / java8-lambda-example.java
Created December 16, 2017 12:45 — forked from antic183/java8-lambda-example.java
java 8 lambda example
@java.lang.FunctionalInterface
interface Math
{
public int calc(int a, int b);
}
public class LambdaExample
{
public static void main(String[] args) {
// define your function
@tamboer
tamboer / 7.java
Created December 16, 2017 12:37 — forked from shadowfacts/7.java
Java 8 lambda example
Runnable thingy = new Runnable() {
public void run() {
System.out.println("Annonymous class");
}
}
somethingElse(thingy);