Java BiPredicate Method Reference - Java 147

Java BiPredicate Method Reference – Java 147

Java BiPredicate Method Reference

引用其他方法為實例,使用 Lambda 語法,傳入 2 個泛型物件參數,執行完後會回傳 boolean 值,例如引用 Objects 、 String 、 List 、 Map 、 Queue 等, BiPredicate Method Reference Java 介紹常見的 equals 、 add 、 offer 、 contains 、 remove 等方法,了解 BiPredicate 的不同操作和應用,本篇增加了範例,並透過單元測試來驗證產出結果。

@FunctionalInterface
public interface BiPredicate<T> {
    boolean test(T t, U u);
}

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- functional
       |                   +- bipredicate
       |                       +- BiPredicateReferenceTest.java   

單元測試

BiPredicate Java Method Reference 提供方法引用、參考等操作 BiPredicate test Java 。

Objects_equals

建立一個 BiPredicate ,使用 Objects equals ,檢查 2 個物件是否相等。

	@Test
	public void Objects_equals() {
		BiPredicate<String, String> equals = Objects::equals;
		boolean result = equals.test("Bacon", "Bacon");
		System.out.println(result);
		assertTrue(result);

		result = equals.test("Bacon", null);
		System.out.println(result);
		assertFalse(result);
	}
true
false

String_contains

建立一個 BiPredicate ,使用 String contains ,檢查包含指定的字元。

	@Test
	public void String_contains() {
		String value = "Bacon";
		BiPredicate<String, String> contains = String::contains;
		boolean result = contains.test(value, "o");
		System.out.println(result);
		assertTrue(result);

		value = "Ham";
		result = contains.test(value, "o");
		System.out.println(result);
		assertFalse(result);
	}
true
false

List_add

BiPredicate Java Method Reference 建立一個 BiPredicate ,使用 List add ,增加三個元素。

	@Test
	public void List_add() {
		List<String> list = new ArrayList<String>();
		BiPredicate<List<String>, String> add = List::add;
		boolean result = add.test(list, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = add.test(list, "Ham");
		System.out.println(result);
		assertTrue(result);
		result = add.test(list, "Pork");
		System.out.println(result);
		assertTrue(result);
		System.out.println(list);
	}
true
true
true
[Bacon, Ham, Pork]

List_remove

BiPredicate Java Method Reference 建立一個 BiPredicate ,使用 List remove ,內有三個元素,刪除指定元素。

	@Test
	public void List_remove() {
		List<String> list = new ArrayList<String>(Arrays.asList("Bacon", "Ham", "Pork"));
		BiPredicate<List<String>, String> remove = List::remove;
		boolean result = remove.test(list, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = remove.test(list, "Bread");
		System.out.println(result);
		assertFalse(result);
		System.out.println(list);
	}
true
false
[Ham, Pork]

List_contains

BiPredicate Java Method Reference 建立一個 BiPredicate ,使用 List contains ,內有三個元素,檢查包含指定的元素。

	@Test
	public void List_contains() {
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		BiPredicate<List<String>, String> contains = List::contains;
		boolean result = contains.test(list, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = contains.test(list, "Bread");
		System.out.println(result);
		assertFalse(result);
	}
true
false

Map_containsKey

BiPredicate Java Method Reference 建立一個 BiPredicate ,使用 Map containsKey ,內有三個元素,檢查包含指定的 Key 。

	@Test
	public void Map_containsKey() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);

		BiPredicate<Map<String, Integer>, String> containsKey = Map::containsKey;
		boolean result = containsKey.test(map, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = containsKey.test(map, "Bread");
		System.out.println(result);
		assertFalse(result);
	}
true
false

Map_containsValue

Java BiPredicate test 建立一個 BiPredicate ,使用 Map containsValue ,內有三個元素,檢查包含指定的 Value 。

	@Test
	public void Map_containsValue() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);

		BiPredicate<Map<String, Integer>, Integer> containsValue = Map::containsValue;
		boolean result = containsValue.test(map, 1);
		System.out.println(result);
		assertTrue(result);
		result = containsValue.test(map, 9);
		System.out.println(result);
		assertFalse(result);
	}
true
false

Queue_offer

Java BiPredicate test 建立一個 BiPredicate ,使用 Queue offer ,增加三個元素。

	@Test
	public void Queue_offer() {
		Queue<String> queue = new ConcurrentLinkedQueue<String>();
		BiPredicate<Queue<String>, String> offer = Queue::offer;
		boolean result = offer.test(queue, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = offer.test(queue, "Ham");
		System.out.println(result);
		assertTrue(result);

		Predicate<String> queueOfferPredicate = queue::offer;
		result = queueOfferPredicate.test("Pork");
		System.out.println(result);
		assertTrue(result);
		System.out.println(queue);
	}
true
true
true
[Bacon, Ham, Pork]

Queue_remove

Java BiPredicate test 建立一個 BiPredicate ,使用 Queue remove ,內有三個元素,刪除指定元素。

	@Test
	public void Queue_remove() {
		Queue<String> queue = new ConcurrentLinkedQueue<String>(Arrays.asList("Bacon", "Ham", "Pork"));
		BiPredicate<Queue<String>, String> remove = Queue::remove;
		boolean result = remove.test(queue, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = remove.test(queue, "Bread");
		System.out.println(result);
		assertFalse(result);
		System.out.println(queue);
	}
true
false
[Ham, Pork]

Queue_contains

Java BiPredicate test 建立一個 BiPredicate ,使用 Queue contains ,內有三個元素,檢查包含指定的元素。

	@Test
	public void Queue_contains() {
		Queue<String> queue = new ConcurrentLinkedQueue<String>(Arrays.asList("Bacon", "Ham", "Pork"));
		BiPredicate<Queue<String>, String> contains = Queue::contains;
		boolean result = contains.test(queue, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = contains.test(queue, "Bread");
		System.out.println(result);
		assertFalse(result);
	}
true
false

BiPredicateReferenceTest.java

Java BiPredicate test 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.functional.bipredicate;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.BiPredicate;
import java.util.function.Predicate;

import org.junit.Test;

public class BiPredicateReferenceTest {

	@Test
	public void Objects_equals() {
		BiPredicate<String, String> equals = Objects::equals;
		boolean result = equals.test("Bacon", "Bacon");
		System.out.println(result);
		assertTrue(result);

		result = equals.test("Bacon", null);
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void String_contains() {
		String value = "Bacon";
		BiPredicate<String, String> contains = String::contains;
		boolean result = contains.test(value, "o");
		System.out.println(result);
		assertTrue(result);

		value = "Ham";
		result = contains.test(value, "o");
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void List_add() {
		List<String> list = new ArrayList<String>();
		BiPredicate<List<String>, String> add = List::add;
		boolean result = add.test(list, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = add.test(list, "Ham");
		System.out.println(result);
		assertTrue(result);
		result = add.test(list, "Pork");
		System.out.println(result);
		assertTrue(result);
		System.out.println(list);
	}

	@Test
	public void List_remove() {
		List<String> list = new ArrayList<String>(Arrays.asList("Bacon", "Ham", "Pork"));
		BiPredicate<List<String>, String> remove = List::remove;
		boolean result = remove.test(list, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = remove.test(list, "Bread");
		System.out.println(result);
		assertFalse(result);
		System.out.println(list);
	}

	@Test
	public void List_contains() {
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		BiPredicate<List<String>, String> contains = List::contains;
		boolean result = contains.test(list, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = contains.test(list, "Bread");
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void Map_containsKey() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);

		BiPredicate<Map<String, Integer>, String> containsKey = Map::containsKey;
		boolean result = containsKey.test(map, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = containsKey.test(map, "Bread");
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void Map_containsValue() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);

		BiPredicate<Map<String, Integer>, Integer> containsValue = Map::containsValue;
		boolean result = containsValue.test(map, 1);
		System.out.println(result);
		assertTrue(result);
		result = containsValue.test(map, 9);
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void Queue_offer() {
		Queue<String> queue = new ConcurrentLinkedQueue<String>();
		BiPredicate<Queue<String>, String> offer = Queue::offer;
		boolean result = offer.test(queue, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = offer.test(queue, "Ham");
		System.out.println(result);
		assertTrue(result);

		Predicate<String> queueOfferPredicate = queue::offer;
		result = queueOfferPredicate.test("Pork");
		System.out.println(result);
		assertTrue(result);
		System.out.println(queue);
	}

	@Test
	public void Queue_remove() {
		Queue<String> queue = new ConcurrentLinkedQueue<String>(Arrays.asList("Bacon", "Ham", "Pork"));
		BiPredicate<Queue<String>, String> remove = Queue::remove;
		boolean result = remove.test(queue, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = remove.test(queue, "Bread");
		System.out.println(result);
		assertFalse(result);
		System.out.println(queue);
	}

	@Test
	public void Queue_contains() {
		Queue<String> queue = new ConcurrentLinkedQueue<String>(Arrays.asList("Bacon", "Ham", "Pork"));
		BiPredicate<Queue<String>, String> contains = Queue::contains;
		boolean result = contains.test(queue, "Bacon");
		System.out.println(result);
		assertTrue(result);
		result = contains.test(queue, "Bread");
		System.out.println(result);
		assertFalse(result);
	}
}

心得分享

BiPredicate test Java 除了傳統實作接口的方法,使用 Lambda 表達式實作功能,能讓程式碼更加簡潔與直接,大幅提高可讀性, Java BiPredicate test 提供了幾種 BiPredicate 常見方法的操作範例。

發佈留言