java中Contain和Containskey两个方法的区别及作用

**### Contains()和ContainsKey()使用及区别

Contains()

用法:一个字符串中是否包含另外一个字符串

public static void main(String[] args) {
    String str1="abcdefg";
    String str2="def";
    boolean res = str1.contains(str2);
    if (res){
        System.out.println("包括");
    }else {
        System.out.println("不包含");
    }
}

注意:判断一个字符串是不是在领一个字符串中包含着,前提条件是你所判断的字符串再另一个字符串中是紧挨着的 例如: “abcdefgh”.contains(“def”) 该结果返回值为true, 如果“abcdefgh”.contains(“df”) 此时返回结果是false

ContainsKey()

用法:主要用于判断map中是否包含指定的键名

public static void main(String[] args) {
    Map<String,String> map=new HashMap<String,String>();
    map.put("book","语文");
    map.put("food","零食");
    boolean res = map.containsKey("book");
    boolean foods = map.containsValue("零食");
    if(foods){
        System.out.println("存在值");
    }else {
        System.out.println("不存在值");
    }
    if(res){
        System.out.println(map.get("book"));
        System.out.println("存在键");
    }else {
        System.out.println("不存在键");
    }
}
```**
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值