📜  JavaTuples contains() 方法

📅  最后修改于: 2022-05-13 01:54:52.365000             🧑  作者: Mango

JavaTuples contains() 方法

org.javatuples 中的contains()方法用于检查 TupleClass 中是否存在作为参数给出的值。此方法可用于 javatuples 库的任何元组类对象。它根据 TupleClass 中该值的存在返回一个布尔值,该值是真还是假。

方法声明:

public final boolean contains(Object value)

句法:

boolean result = TupleClassObject.contains(X value)

参数:此方法将作为参数,其中:

  • X – 表示参数中值的数据类型。
  • TupleClassObject – 表示使用的 JavaTuple 类对象,如 Unit、Quintet、Decade 等。

返回值:如果元组中存在参数值,则此方法返回 true。否则返回 false

下面的程序说明了使用 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 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
            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 类一起使用。