当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。