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


Java JavaTuples containsAll()用法及代码示例


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

方法声明:

public final boolean containsAll(Object... value)

用法:


boolean result = TupleClassObject.containsAll(X value1, X value2, ...)
                    OR
boolean result = TupleClassObject.containsAll(X[] values)

参数:此方法将一个或多个值作为参数,其中:

  • X-表示参数中值的数据类型。
  • TupleClassObject-表示JavaTuple Class对象,例如Unit,Quintet,Decade等。

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

以下示例程序旨在说明使用containsAll()方法的各种方法:

程序1:使用containsAll()和Unit类:

// Below is a Java program to use containsAll() 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 containsAll() method 
  
        boolean res; 
  
        // for True result 
        String[] check = { "GeeksforGeeks" }; 
        res = unit.containsAll(check); 
  
        System.out.println("Is " + Arrays.toString(check) + " present:" + res); 
  
        // for False result 
        String[] check1 = { "Geeks", "for", "Geeks" }; 
        res = unit.containsAll(check1); 
  
        System.out.println("Is " + Arrays.toString(check1) + " present:" + res); 
    } 
}

输出:

Is [GeeksforGeeks] present:true
Is [Geeks, for, Geeks] present:false

程序2:将containsAll()与Decade类一起使用:

// Below is a Java program to use containsAll() 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 containsAll() method 
        boolean res; 
  
        // for true result 
        String[] check = { "Geeks", "for", "Geeks" }; 
        res = decade.containsAll(check); 
  
        System.out.println("Is " + Arrays.toString(check) + " present:" + res); 
  
        // for False result 
        String[] check1 = { "Geeks", "not", "for", "Geeks" }; 
        res = decade.containsAll(check1); 
  
        System.out.println("Is " + Arrays.toString(check1) + " present:" + res); 
    } 
}

输出:

Is [Geeks, for, Geeks] present:true
Is [Geeks, not, for, Geeks] present:false

注意:同样,它可以与任何其他JavaTuple类一起使用。



相关用法


注:本文由纯净天空筛选整理自RishabhPrabhu大神的英文原创作品 JavaTuples containsAll() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。