By GokiSoft.com| 20:00 08/07/2022|
Java Basic

[Source Code] Tóm tắt nội dung kiến thức + lambda expression - C2108L

#Test3.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

import java.util.ArrayList;
import java.util.stream.Stream;

/**
 *
 * @author Diep.Tran
 */
public class Test3 {
    public static void main(String[] args) {
        ArrayList<Integer> t = new ArrayList<>();
        t.add(1);
        t.add(2);
        t.add(3);
        t.add(4);
        t.add(5);
        t.add(6);
        
        for (Integer v : t) {
            System.out.println(v);
        }
        
        Stream stream = t.stream();
        stream.forEach(System.out::println);
        
        Stream t2 = t.parallelStream();
        t2.forEach(System.out::println);
    }
}


#Test2.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Test2 {
    static interface IRunning {
        void running();
    }
    
    static interface IRunning2 {
        void running(String msg);
    }
    
    public static void main(String[] args) {
        //Cach 1:
        IRunning r1 = new IRunning() {
            @Override
            public void running() {
                System.out.println("Test 1");
            }
        };
        r1.running();
        
        //Cach 2:
        IRunning r2 = () -> {
            System.out.println("Test 1");
        };
        r2.running();
        
        //Cach 3: -> TH trong than chi co 1 dong code
        IRunning r3 = () -> System.out.println("Test 1");
        r3.running();
        
        IRunning2 r4 = (String xd) -> {
            System.out.println("Testing ...");
            System.out.println("Testing ... " + xd);
        };
        r4.running("Xin chao");
        
        IRunning2 r5 = (msg) -> {
            System.out.println("Testing ...");
            System.out.println("Testing ... " + msg);
        };
        r5.running("Xin chao 123");
        
        showMsg(r5);
        
        showMsg((msg) -> {
            System.out.println(".1.2.3 = " + msg);
        });
        
        //Code tren co the viet tuong minh nhu sau
        IRunning2 abc = (msg) -> {
            System.out.println(".1.2.3 = " + msg);
        };
        showMsg(abc);
        
        //Viet tuong minh hon
        IRunning2 abc1 = (String msg) -> {
            System.out.println(".1.2.3 = " + msg);
        };
        showMsg(abc1);
        
        //Viet tuong minh hon
        IRunning2 abc2 = new IRunning2() {
            @Override
            public void running(String msg) {
                System.out.println(".1.2.3 = " + msg);
            }
        };
        showMsg(abc2);
    }
    
    static void showMsg(IRunning2 r) {
        Scanner s = new Scanner(System.in);
        System.out.println("Nhap thong diep: ");
        String msg = s.nextLine();
        r.running(msg);
    }
}


#Test.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    static interface IAction {
        void running();
        void stopping();
    }
    
    static class People implements IAction {

        @Override
        public void running() {
            System.out.println("Running");
        }

        @Override
        public void stopping() {
            System.out.println("Stopping");
        }
        
    }
    
    public static void main(String[] args) {
//        IAction act = new IAction();
        People p = new People();
        p.running();
        p.stopping();
        
        IAction act = new IAction() {
            @Override
            public void running() {
                System.out.println("Test running");
            }

            @Override
            public void stopping() {
                System.out.println("Test stopping");
            }
        };
        act.stopping();
        act.running();
    }
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Nhap du lieu string -> datetime
        //Chuyen datetime -> string de hien thi
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap vao ngay 1 (yyyy-MM-dd): ");
        String dateStr1 = scan.nextLine();
        System.out.println("Nhap vao ngay 2 (yyyy-MM-dd): ");
        String dateStr2 = scan.nextLine();
        
        //So sanh ngay
        Date date1 = convertStringToDate(dateStr1);
        Date date2 = convertStringToDate(dateStr2);
        
        if(date1.after(date2)) {
            System.out.println("Ngay max: " + dateStr1);
        } else {
            System.out.println("Ngay max: " + dateStr2);
        }
        
        System.out.println(convertDateToString(new Date()));
        
        System.out.println(new Date());
    }
    
    public static Date convertStringToDate(String str) {
        try {  
            Date date=new SimpleDateFormat("yyyy-MM-dd").parse(str);
            
            return date;
        } catch (ParseException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }
    
    public static String convertDateToString(Date mydate) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
        String strDate = dateFormat.format(mydate); 
        
        return strDate;
    }
}


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó