0Pricing
Java Academy · บทเรียน

Function และ BiFunction

แปลงค่าด้วยแนวคิดเชิงฟังก์ชัน

Function และ BiFunction เป็นบทเรียน Java Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Java Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Java Academy มีบทเรียนทั้งหมด 4 บทเรียน

สิ่งที่ฟังก์ชันแทน

Function<T, R> คืออินเทอร์เฟซการแปลงหลักใน java.util.function โดยรับอาร์กิวเมนต์หนึ่งรายการชนิด T และคืนผลลัพธ์ชนิด R

  • เมธอดนามธรรมเพียงรายการเดียวคือ R apply(T t)
  • ใช้แทนการแมปจากข้อมูลนำเข้าไปเป็นข้อมูลส่งออกแบบบริสุทธิ์
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> length = s -> s.length();
        System.out.println(length.apply("hello"));
    }
}

การเรียกใช้ apply

คุณเรียกใช้ Function ด้วยการเรียก apply เนื้อหาของแลมบ์ดาคือตรรกะการแปลง

  • ชนิดของข้อมูลนำเข้าและข้อมูลส่งออกอาจแตกต่างกันได้
  • การอ้างอิงเมธอดอย่าง String::toUpperCase สามารถใช้แทนแลมบ์ดาได้
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, String> upper = String::toUpperCase;
        System.out.println(upper.apply("java"));
    }
}

การประกอบด้วย andThen

andThen เชื่อมฟังก์ชันเข้าด้วยกัน โดยผลลัพธ์ของฟังก์ชันแรกจะกลายเป็นข้อมูลนำเข้าของฟังก์ชันที่สอง

  • f.andThen(g) หมายถึง g(f(x))
  • อ่านลำดับจากซ้ายไปขวา
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> times2 = n -> n * 2;
        Function<Integer, Integer> plus3 = n -> n + 3;
        System.out.println(times2.andThen(plus3).apply(5));
    }
}

การประกอบด้วย compose

compose จะเรียกใช้ฟังก์ชันอาร์กิวเมนต์ก่อน

  • f.compose(g) หมายถึง f(g(x))
  • เป็นภาพกลับด้านของ andThen
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> times2 = n -> n * 2;
        Function<Integer, Integer> plus3 = n -> n + 3;
        System.out.println(times2.compose(plus3).apply(5));
    }
}

Function.identity

Function.identity() คืนฟังก์ชันที่ส่งข้อมูลนำเข้ากลับมาโดยไม่เปลี่ยนแปลง

  • มีประโยชน์ในตัวรวบรวมของสตรีมและการแมปเริ่มต้น
  • เทียบเท่ากับ x -> x
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, String> id = Function.identity();
        System.out.println(id.apply("unchanged"));
    }
}

ทำความรู้จัก BiFunction

BiFunction<T, U, R> รับอาร์กิวเมนต์สองรายการและคืนผลลัพธ์หนึ่งรายการ

  • เมธอดของมันคือ R apply(T t, U u)
  • เหมาะสำหรับรวมค่าสองค่า เช่น การรวมแผนที่
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        System.out.println(add.apply(4, 6));
    }
}

BiFunction กับชนิดที่ผสมกัน

พารามิเตอร์ชนิดทั้งสามรายการของ BiFunction อาจแตกต่างกันทั้งหมด

  • ในที่นี้ เรารวม String และ int ให้เป็น String
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        BiFunction<String, Integer, String> repeat = (s, n) -> s.repeat(n);
        System.out.println(repeat.apply("ab", 3));
    }
}

BiFunction กับ andThen

BiFunction ยังรองรับ andThen ซึ่งรับ Function ที่มีอาร์กิวเมนต์เดียวเพื่อประมวลผลผลลัพธ์ภายหลัง

  • BiFunction ไม่มี compose เพราะ compose จะต้องรับข้อมูลนำเข้าสองรายการ
import java.util.function.BiFunction;
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        Function<Integer, String> label = n -> "sum=" + n;
        System.out.println(add.andThen(label).apply(7, 8));
    }
}

ฟังก์ชันในคอลเลกชัน

ส่วนติดต่อโปรแกรมประยุกต์จำนวนมากรับ Function ตัวอย่างเช่น Map.computeIfAbsent รับฟังก์ชันการแมปสำหรับคีย์ที่ไม่มีอยู่

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> lengths = new HashMap<>();
        lengths.computeIfAbsent("hello", String::length);
        System.out.println(lengths);
    }
}

การเก็บฟังก์ชันไว้ในตัวแปร

เนื่องจากฟังก์ชันเป็นออบเจ็กต์ คุณจึงสามารถเก็บ ส่งต่อ และคืนฟังก์ชันได้ นี่คือรากฐานของ Java เชิงฟังก์ชัน

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> square = n -> n * n;
        Function<Integer, Integer> cube = n -> n * n * n;
        Function<Integer, Integer> chosen = square;
        System.out.println(chosen.apply(5));
    }
}

การสร้างสายการแปลงข้อมูล

การเชื่อม andThen ซ้ำหลายครั้งจะสร้างสายการแปลงข้อมูลที่อ่านเข้าใจง่ายและนำกลับมาใช้ใหม่ได้

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, String> trim = String::trim;
        Function<String, String> upper = String::toUpperCase;
        Function<String, Integer> len = String::length;
        System.out.println(trim.andThen(upper).andThen(len).apply("  abc "));
    }
}

ตรวจสอบความเข้าใจอย่างรวดเร็ว

f.andThen(g).apply(x) คำนวณอะไร

สรุปทบทวน

คุณได้เรียนรู้อินเทอร์เฟซการแปลงหลัก

  • Function<T,R> แมปค่าหนึ่งค่าด้วย apply
  • andThen เชื่อมไปข้างหน้า (g(f(x))); compose เชื่อมย้อนกลับ (f(g(x)))
  • Function.identity() คืนข้อมูลนำเข้าของตัวเอง
  • BiFunction<T,U,R> จัดการข้อมูลนำเข้าสองรายการ และรองรับ andThen แต่ไม่รองรับ compose

คำถามที่พบบ่อย

บทเรียน “Function และ BiFunction” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Function และ BiFunction” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Java Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Java Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Function และ BiFunction”

แปลงค่าด้วยแนวคิดเชิงฟังก์ชัน คุณปฏิบัติ Java Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Java Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Java Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “Function และ BiFunction” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Java Academy นี้ได้ไหม

ได้ บทเรียน Java Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Function และ BiFunction
  2. Supplier และ Consumer
  3. Predicate และการประกอบ
  4. อินเทอร์เฟซเชิงฟังก์ชันแบบกำหนดเอง
← กลับไปที่ Java Academy