String invoiceTxt =
"Hi " + name + ",\n"
+ "please send " + (value + tax) + "€ to us or we eat"
+ "your brain.\n\n"
+ "Sincerely\n"
+ "\tThe 🧟s";
Merlin Bögershausen / @mboegie
String invoiceTxt =
"Hi " + name + ",\n"
+ "please send " + (value + tax) + "€ to us or we eat"
+ "your brain.\n\n"
+ "Sincerely\n"
+ "\tThe 🧟s";
hard to read ❌
hard to format ❌
need for braces ❌
String invoiceTxt = new StringBuilder()
.append("Hi ").append(name).append(",\n")
.append("please send ").append(value + tax)
.append("€ to us or we eat you brain.\n\n")
.append("Sincerely\n")
.append("\tThe 🧟s")
.toString();
even worse to read ❌❌
hard to format ❌
very verbose ❌
String invoiceTxt = """
Hi %s,
please send %.2f€ to us or we eat you brain.
Sincerely
The 🧟s""".formatted(name, value + tax);
// pre Java 15
String.format("..", name, value + tax);
better to read ✅
easy to format ✅
decouple parameter from String ❌
Strings with runtime computed values
readable mix of expressions and Strings
easy Formatting of values
String name = "Merlin";
int value = 34;
double tax = value * 0.1;
String invoiceTxt = STR."""
Hi \{ name },
please send \{ value + tax }€ to us or we eat you brain.
Sincerely
The 🧟s""";
// String name; int value; double tax;
String invoiceTxt = FMT."""
Hi %s\{ name },
please send %.2f\{ value + tax }€ to us or we eat you brain.
Sincerely
The 🧟s""";
@FunctionalInterface
public interface Processor<R, E extends Throwable> {
// transform StringTemplate to Result
R process(StringTemplate stringTemplate) throws E;
// Factory Method for Processors
static <T> Processor<T, RuntimeException>
of(Function<? super StringTemplate, ? extends T> process){}
}
class Shuffler implements Processor<String, Throwable> {
@Override
public String process(StringTemplate st) throws Throwable {
var values = st.values().reversed().iterator();
var sb = new StringBuilder();
st.fragments().reversed()
.forEach(frag -> sb.append(frag).append(values.next()));
return sb.toString();
}
} // new Shuffler().""
var JSON = StringTemplate.Processor
.of(st -> new JSONObject(st.interpolate()));
first and last letter
keep order and travers revers
add and remove at both ends
🤔List, (Sorted)Set, Deque 🧐
// List
list.get(0); list.get(list.size() -1));
// Sorted Set at least
sortedSet.first(); sortedSet.last();
// Deque (double ended queue)
deque.getFirst(); deque.getLast();
👑Deque
// List
for(var it = l.listIterator(l.size()); it.hasPrevious();){ }
// sorted Set
sortedSet.descendingSet().forEach();
// Deque
for(var it = deque.descendingIterator(); it.hasNext();){ }
👑Sorted Set
// List
l.add(0, "Hallo"); l.add("World");
// sorted Set
var tmp = new TreeSet<String>();
tmp.add("Hallo");
tmp.addAll(sortedSet);
tmp.add("World");
sortedSet = tmp;
// Deque
deque.addFirst("Hello"); deque.addLast("World");
👑Deque
interface SequencedCollection<E> extends Collection<E> {
// new method
SequencedCollection<E> reversed();
// methods promoted from Deque
void addFirst(E); void addLast(E);
E getFirst(); E getLast();
E removeFirst(); E removeLast();
}
see SequencedCollection JavaDoc.
interface SequencedSet<E>
extends Set<E>, SequencedCollection<E> {
SequencedSet<E> reversed(); // covariant override
}
see SequencedSet JavaDoc.
interface SequencedMap<K,V> extends Map<K,V> {
// new methods
SequencedMap<K,V> reversed();
SequencedSet<K> sequencedKeySet();
SequencedCollection<V> sequencedValues();
SequencedSet<Entry<K,V>> sequencedEntrySet();
V putFirst(K, V); V putLast(K, V);
// methods promoted from NavigableMap
Entry<K, V> firstEntry(); Entry<K, V> lastEntry();
Entry<K, V> pollFirstEntry(); Entry<K, V> pollLastEntry();
}
see SequencedMap JavaDoc.
We live forward, but we understand backward.
public class Main {
public static void main(String[] args) {
System.out.println("Hello 🧟-Land");
}
}
Public Class with public static main with array parametern.
Allow all non-private classes
static void main(String[] args)
static void main()
void main(String[] args)
void main()
public class Main {
public static void main(String[] args) {
System.out.println("Hello 🧟-Land");
}
}
class Main {
void main() {
System.out.println("Hello 🧟-Land");
}
}
void main() {
System.out.println("Hello 🧟-Land");
}
$ java SomeName.java
Hello 🧟-Land
void main() {
System.out.println("Goodbye Info Days")
}
int value = 34;
double tax = value * 0.1;
List<String> brainCarrier = //..
void main() {
FMT."""
Hi %s\{ brainCarrier.reversed().getFirst() },
please send %.2f\{ value + tax }€ to us or we eat you brain.
Sincerely
The 🧟s""";
}
Plants vs Zombies Wallpaper from Fanpop
What do we want Meme from imgflip
Chaos image from jcomp on Freepik
Stagelights from vector_corp on Freepik
Collection Hirachy by Stuart Marks
Gears from wirestock on Freepik
0 to Hero from Bing Image Creator