Berikut adalah contoh hal-hal baru yang bisa dilakukan dengan java 8.
A. Comparator, Sorting dengan class Collection
A. Comparator, Sorting dengan class Collection
--- public class Test_1 { public static void main(String[] args) { ListstringList = new ArrayList<>(); stringList.add("fdasadQ"); stringList.add("qfdasadQ"); stringList.add("fwwdasadQ"); stringList.add("sfdfsfdasadQ"); stringList.add("4543fdasadQ"); System.out.println(stringList); // Collections.sort(stringList); //
// Collections.sort(stringList, (s1, s2) -> s1.compareTo(s2));
Collections.sort(stringList, (s1, s2) -> s2.compareTo(s1));
//decending
System.out.println("setelah di sorting"); System.out.println(stringList); } } result: [fdasadQ, qfdasadQ, fwwdasadQ, sfdfsfdasadQ, 4543fdasadQ] setelah di sorting [sfdfsfdasadQ, qfdasadQ, fwwdasadQ, fdasadQ, 4543fdasadQ] ---
B. Functional Interface
result:public class Test_1 { public static void main(String[] args) { TesPrin tesPrin = () -> System.out.println("cetak terserah"); TesPrinParam tesPrinParam = (ss) -> System.out.println("cetak "+ss); System.out.println("memanggil default method di interface >>> "+tesPrin.srqt(9)); tesPrin.prin(); tesPrinParam.prin("param"); Operasi tambah = (int bilA,int bilB)-> bilA + bilB; Operasi kurang = (int bilA,int bilB)-> bilA - bilB; Operasi kali = (int bilA,int bilB)-> bilA * bilB; Operasi bagi = (int bilA,int bilB)-> bilA / bilB; System.out.println(kalkulator(3,4, tambah)); System.out.println(kalkulator(3,4, kurang)); System.out.println(kalkulator(3,4, kali)); System.out.println(kalkulator(3,4, bagi)); } interface TesPrin{ void prin(); default double srqt(double angka){ return Math.sqrt(angka); } } interface TesPrinParam{ void prin(String param); } interface Operasi{ int aksi(int bilA,int bilB); } public static int kalkulator(int bilA,int bilB, Operasi operasi){ return operasi.aksi(bilA,bilB); } }
cetak terserah
cetak param
memanggil default method di interface >>> 3.0
cetak terserah
cetak param
7
-1
12
0
C. For Each
public class Test_1 { public static void main(String[] args) { List<Siswa> siswaList = new ArrayList<>(); siswaList.add(new Siswa("Sucipto", "Pintar")); siswaList.add(new Siswa("Elnanda", "Pintar Sekali")); siswaList.add(new Siswa("Ujup", "1")); siswaList.add(new Siswa("Nazar", "1")); siswaList.add(new Siswa("Suseno", "XL")); System.out.println("forEach Siswa"); siswaList.forEach(siswa -> System.out.println(siswa)); System.out.println(); siswaList.forEach(siswa -> { if (siswa!=null && siswa.getName().equals("Sucipto")) System.out.println(siswa); }); } } class Siswa{ String name; String kelas; // get set @Override public String toString() { return "Siswa{" + "name='" + name + '\'' + ", kelas='" + kelas + '\'' + '}'; } }
D. Stream
siswaList.stream() .filter(siswa -> siswa.getKelas().contains("Pintar")) .forEach(System.out::println); List<Siswa> siswaList1 = siswaList.stream() .filter(siswa -> siswa.getKelas().contains("X")) .collect(Collectors.toList()); siswaList1.forEach(System.out::println);
steam grouping
Map<String, List<Siswa>> collect = siswaList.stream().collect(Collectors.groupingBy(Siswa::getName)); collect.forEach( (s, siswas) -> { System.out.println(s+"|"+siswas); } );
E. Bla bla
F. Bla bla
G. Bla bla
0 komentar:
Post a Comment