當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java JavaTuples contains()用法及代碼示例


org.javatuples中的contains()方法用於檢查在TupleClass中是否存在作為參數給出的值。此方法可用於javatuples庫的任何tuple類對象。它基於TupleClass中存在的布爾值返回true或false。

方法聲明:

public final boolean contains(Object value)

用法:


boolean result = TupleClassObject.contains(X value)

參數:此方法將值作為參數,其中:

  • X-表示參數中值的數據類型。
  • TupleClassObject-表示JavaTuple Class對象,例如Unit,Quintet,Decade等。

返回值:如果元組中存在參數值,則此方法返回true。否則返回假

以下示例程序旨在說明使用contains()方法的各種方法:

程序1:使用contains()和Unit類:

// Below is a Java program to create 
// a Unit tuple from contains() method 
  
import java.util.*; 
import org.javatuples.Unit; 
  
class GfG { 
    public static void main(String[] args) 
    { 
        // Creating an Unit with one value 
        Unit<String> unit = Unit.with("GeeksforGeeks"); 
  
        // Using contains() method 
  
        boolean res; 
        String check; 
  
        // for True result 
        check = "GeeksforGeeks"; 
        res = unit.contains(check); 
  
        System.out.println("Is " + check + " present:" + res); 
  
        // for False result 
        check = "Geeks"; 
        res = unit.contains(check); 
  
        System.out.println("Is " + check + " present:" + res); 
    } 
}

輸出:

Is GeeksforGeeks present:true
Is Geeks present:false

程序2:將contains()與Decade類一起使用:

// Below is a Java program to create 
// a Unit tuple from contains() method 
  
import java.util.*; 
import org.javatuples.Decade; 
  
class GfG { 
    public static void main(String[] args) 
    { 
        // Creating a Decade with 10 value 
        Decade<String, String, String, String, String, 
               String, String, String, String, String> 
            decade = Decade.with("Geeks", 
                                 "for", 
                                 "Geeks", 
                                 "A", 
                                 "Computer", 
                                 "Science", 
                                 "Portal", 
                                 "for", 
                                 "Geeks", 
                                 "RishabhPrabhu"); 
  
        // Using contains() method 
  
        boolean res; 
        String check; 
  
        // for True result 
        check = "GeeksforGeeks"; 
        res = decade.contains(check); 
  
        System.out.println("Is " + check + " present:" + res); 
  
        // for False result 
        check = "Geeks"; 
        res = decade.contains(check); 
  
        System.out.println("Is " + check + " present:" + res); 
    } 
}

輸出:

Is GeeksforGeeks present:true
Is Geeks present:false

注意:同樣,它可以與任何其他JavaTuple類一起使用。



相關用法


注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 JavaTuples contains() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。