JAVAutil。树地图。Java中的containskey()和containsValue()

Java中contains()的变体。util。TreeMap,本文将讨论这两种方法。

null

1.containskey(对象o): 如果映射包含指定键的映射,则返回true。

Parameters:
o : The key which will be tested whether present or not.
Return Value:
Returns true if there is a mapping for the given key.
Exception:
ClassCastException : This is thrown if the given key cannot be compared
with the keys currently in the map.
NullPointerException : This is thrown if the specified key is null.

// Java code to demonstrate the working
// of containsKey()
import java.io.*;
import java.util.*;
public class containsKey {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
// assigning the values in the tree map
// using put()
treemap.put( 2 , "two" );
treemap.put( 7 , "seven" );
treemap.put( 3 , "three" );
treemap.put( 6 , "six" );
treemap.put( 9 , "nine" );
// Use of containsKey
// returns true if mapping for the number is present
System.out.println(treemap.containsKey( 6 ));
System.out.println(treemap.containsKey( 4 ));
}
}


输出:

true
false

2.包含值(对象o): 如果此映射将一个或多个键映射到指定值,则返回true。

Parameters:
o : This is the value whose presence in this map is to be tested.
Return Value:
Returns true if a mapping to this value exists else false.
Exception:
NA

// Java code to demonstrate the working
// of containsValue()
import java.io.*;
import java.util.*;
public class containsValue {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
// assigning the values in the tree map
// using put()
treemap.put( 2 , "two" );
treemap.put( 7 , "seven" );
treemap.put( 3 , "three" );
treemap.put( 6 , "six" );
treemap.put( 9 , "nine" );
// Use of containsValue
// returns true if more than one keys are mapped
System.out.println(treemap.containsValue( "six" ));
System.out.println(treemap.containsValue( "four" ));
}
}


输出:

true
false

本文由 沙姆巴维·辛格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧,技术咨询可以联系QQ407933975
点赞11 分享