Useful Java Patterns
Java
Kotlin
Tutorials
Stream API
List Comprehension
Switch Expressions
Eclipse
JetBrains Plugin
Beanshell Syntax Highlighting
Array Element Occurrences
This guide offers tutorials in Java and Kotlin, covering patterns like Java Stream, list comprehension, switch expressions, and iterating through lists with indices. Additionally, it discusses eclipse and JetBrains plugin support for BeanShell syntax highlighting. The guide also teaches various methods to count array element occurrences in both languages.
learn java and kotlin the same time?
beanshell syntax highlight seems to be rare. beanshell workspace has the highlight.
use v2.1.1 and above for varargs support.
there are eclipse and jetbrains plugin support for beanshell.
creating lists
var a = new ArrayList<>(Arrays.asList(1,2,3));
var mset = new HashSet<>();
.addAll(a);
msetvar mset2 = a.stream().collect(Collectors.toSet());
var mymin = Collections.min(a);
var mymax = Collections.max(a);
Collections.reverse(a);
var a = arrayOf(1,2,3);
(a.contentToString())
print(a.max())
print(a.min())
print.reverse() a
creating hashmap
var ah= new HashMap<>();
.put(1,2); ah
var ah = hashMapOf(1 to 2, 2 to 3)
iterate lists with indices
java double colon ::
operator acts as anonymous function
var l = a.listIterator();
while (l.hasNext()){
var index = l.nextIndex();
var val = l.next();
System.out.println("INDEX: "+index+" ELEM: "+val);
}
.range(0,a.size()).forEach(index -> a.get(index));
IntStreamvar index=0;
for (int i: a){
++;
index}
.forEachIndexed{ind, elem -> println("index? $ind"); println("elem? $elem")}
afor (var i in a.indices){
var elem = a[i]
}
.indices.forEach {
avar elem = a[it]
elem}
list comprehension
var mlist = a.stream().map(x-> x*2).collect(Collectors.toList());
var evenNums = a.stream().filter(x-> x%2 == 0).collect(Collectors.toList());
var mmap = a.stream().collect(Collectors.toMap(x->x.getId(),x->x.getName()));
switch expressions
var val = 2;
var mswitch = switch (val){
case 1,2,3 -> {
"good";
yield }
case 4,5,6 -> {
"bad";
yield } // either throw or yield.
default -> {
System.out.println("out of expectation");
"really bad";
yield }
};
System.out.println(mswitch);
var grade = 30
var res = when(grade) {
in 0..40 -> "Fail"
in 41..70 -> "Pass"
in 71..100 -> "Distinction"
else -> false
}
(res)
printvar mcase = 1
var res = when(mcase){
1 -> "good"
2 -> "bad"
else -> "really bad"
}
(res) print
count occurance of elements in array
String[] array = {"name1","name2","name3","name4", "name5", "name2"};
Arrays.stream(array)
.collect(Collectors.groupingBy(s -> s))
.forEach((k, v) -> System.out.println(k+" "+v.size()));
List asList = Arrays.asList(array);
Set<String> mySet = new HashSet<String>(asList);
for(String s: mySet){
System.out.println(s + " " + Collections.frequency(asList,s));
}
Map<String, Long> map = Arrays.stream(array)
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));
var a = arrayOf(1,2,3,3,3,3)
.toSet().forEach{it -> println("elem? $it"); println("count? "+a.count{it2->it2 == it})} a
lambdas
= (n) -> {System.out.println(n);}
Consumer mcons .andThen(mcons).accept("mval");
mcons<Integer,Integer> mfunc = n-> n+1;
Function = () -> 1;
Supplier msup var mval = msup.get();
var mfunc = {n :Int -> n+1}
var mprint = {n:Any -> print(n)}