> Java > java지도 시간 > 본문

Java 술어

王林
풀어 주다: 2024-08-30 15:58:16
원래의
998명이 탐색했습니다.

Java 조건자는 java.util.function 패키지의 일부인 기능적 인터페이스로, 메서드를 참조하거나 부울 값이 true 또는 부울 기반 함수를 평가하기 위해 람다 식에 대상으로 할당된 일반 조건자 역할을 합니다. false는 람다 식이나 메서드 참조의 대상으로 할당됩니다. 따라서 객체로 구성된 사용 사례와 구현을 위한 평가 조건 규칙을 기반으로 프로그래밍 측면에서 특정 구현에 대한 조건을 평가하는 데 도움이 되는 이러한 조건자의 할당을 기반으로 하는 적절한 평가 조건이 필요합니다.

광고 이 카테고리에서 인기 있는 강좌 JAVA MASTERY - 전문 분야 | 78 코스 시리즈 | 15가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문

술어의 구문 흐름은 표시된 것과 같습니다. 구문을 살펴보면 기본적으로 프로그램 본문의 테스트 조건을 확인하기 위한 메서드 참조에 대한 조건부로 사용되는 기능적 인터페이스인 것처럼 보일 수 있습니다.

package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Predicate<T>
{
boolean test(T t);
// Body of the Program
}
로그인 후 복사

설명: 구문 흐름을 살펴보면 기본적으로 프로그램 본문의 테스트 조건을 확인하기 위한 메서드 참조에 대한 조건자로 사용되는 기능적 인터페이스인 것처럼 보일 수 있습니다.

  • package java.util.function: predicate 함수와 관련된 메소드와 함수를 지원하는 데 사용되는 함수에 대한 패키지를 나타냅니다.
  • import java.util.Objects: 이 가져온 패키지는 기능적 인터페이스와 관련하여 생성된 객체를 지원하는 역할을 담당합니다.
  • @FunctionalInterface: 이는 스프링 부트 애플리케이션에 대한 주석을 나타내며, 애플리케이션 또는 애플리케이션이 사용하는 인터페이스 지원이 기능적 인터페이스인 조건자입니다.
  • 공용 인터페이스 Predicate: 현재 클래스나 메소드를 지원하는 인터페이스 타입이 조건자 인터페이스임을 나타냅니다.
  • 부울 테스트(T t): 이 메서드는 조건자 인터페이스의 일부입니다. 람다 식의 중요성을 정의하고 평가하는 데 사용되는 추상 메서드 또는 형식 조건자의 대상을 할당하는 데 사용되는 메서드 참조입니다. 이 메소드는 반환 유형으로 부울을 갖습니다. 또한 T는 인터페이스로서 조건자의 입력 유형인 전달된 인수입니다. 전반적으로 이 테스트 방법은 평가 조건의 만족 여부에 따라 true 또는 false를 반환합니다.

Java에서 Predicate는 어떻게 작동하나요?

Java의 조건자는 프로그래머가 더 깔끔하고 읽기 쉬운 형식으로 코드를 만들고 생성할 수 있는 구세주입니다. 테스트 케이스 형식을 더 좋게 만들고 테스트 케이스를 향상시키는 데 도움이 됩니다. 일반적으로 Predicate는 제약 조건이 있는 조건을 평가하는 데 도움이 되는 부울 형식의 명령문입니다. Java의 조건자는 기본적으로 java.util.function.package에서 얻은 값으로 조건자를 할당하여 기능적 인터페이스를 구현하는 데 사용됩니다. 전체 메소드 또는 코드베이스의 함수에 대해 true 또는 false로 반환되는 부울 값을 얻기 위해 패키지 메소드를 조건자 패키지의 객체 전달 대상으로 만듭니다. 전체 방법을 참고자료로 평가하는데 사용되는 테스트 방법과 각각의 기능으로 구성되어 있습니다. Java에는 독립형 함수라는 개념이 없습니다. 따라서 이 인터페이스에서 개체를 정의하고 생성하는 작업을 수행합니다. Iterable.filter() 메소드는 메소드의 객체와 협력하여 사용할 수 있습니다. 술어가 있는 람다 표현식도 좋은 역할을 하며 술어와 쉽게 작동합니다.

Java 술어의 메소드

Java 조건자 메서드를 사용하는 메서드는 다양하며 다음과 같이 표시됩니다.

  • boolean test(T t ): This method is used for evaluating the present predicate based on the passed argument t.
  • default Predicate and(Predicate other): The return type of this method is the composed predicate based on the AND logical operator of short-circuiting that will predicate and then the value for the other predicate if throws the value as false then there will be no evaluation with any argument or other logical operators.
  • default Predicate negate(): Any negated value, i.e. a negative value, will get returned using the default Predicate negate method whenever defined within the package.
  • default Predicate or(Predicate other): This behavior is also the same as and default predicate with a mere difference that the short-circuit will predicate and follow the value for the other predicate and is any of the value gets out to be true then only the other value will become false.
  • static Predicate isEqual(Object targetRef): This method returns a predicate value which will be used for testing whether the two objects have similar values or both have some transformed values.

Thus, using these methods with the predicate helps in evaluating any of the conditions defined with the predicate’s method types.

Examples to Implement Java Predicate

Below are examples mentioned:

Example #1

This program demonstrates the creation of a simple Predicate and then calling that Predicate method later point of time for evaluation condition with a test method as shown.

Code:

import java.util.function.Predicate;
public class Frst_Java_Predicate_Ex  {
public static void main(String[] args) {
Predicate<Integer> prdc = vl -> (vl > 20);
System.out.println(prdc.test(80));
}
}
로그인 후 복사

Output:

Java 술어

Example #2

This program demonstrates the predicate value with the boolean constraint evaluating the marks of the student by using a predicate where a constraint says that if the student secures marks more than 35, then that student passes; otherwise, the student will fail if it returns the value false.

Code:

import java.util.function.Predicate;
public class Predict_Java_ex_2 {
static Boolean checkMarks(int marks){
if(marks>35)
return true;
else
return false;
}
public static void main(String[] args){
Predicate<Integer> pred =  Predict_Java_ex_2::checkMarks;
boolean rslt = pred.test(15);
System.out.println("If marks is more than 35 then only the student will get pass otherwise fail." + rslt);
}
}
로그인 후 복사

Output:

Java 술어

Conclusion

Java Predicate is one of the new packages and utility being introduced in java 8, which is very flexible with the lambda expressions and helps the programmers create a neat and clean enhanced code for reference and understanding. A predicate with the values helps in the evaluation of the conditions with the return types and values.

위 내용은 Java 술어의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!