在HashSet和HashMap上包含方法的一致性 [英] Consistency of contains method on a HashSet and HashMap

查看:101
本文介绍了在HashSet和HashMap上包含方法的一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在Set上调用remove时HashSet上的containsAll方法不保持一致,而在HashMap上的containsValue方法在删除值后保持一致
从HashSet中移除值后containsAll返回false即使所有的值都存在,在HashMap的情况下,containsValue方法返回正确的值

  public static void main(String []] args)
{

HashSet< String> lookup = new HashSet< String>();
HashMap< Integer,String> findup = new HashMap< Integer,String>();

String [] Alltokens = {This,is,a,programming,test,This,is,a,any, 语言};
for(String s:Alltokens)
lookup.add(s);

String [] tokens = {This,is,a};
集合< String> TOK = Arrays.asList(令牌);

lookup.remove(Alltokens [5]);
if(lookup.containsAll(tok))
System.out.print(found);
else
System.out.print(NOT found);

整数i = 0;
for(String s:Alltokens)
findup.put(i ++,s);
boolean found = true;
findup.remove(Alltokens [0]);
findup.remove(5); //编辑:尝试删除键值为5
的值(String s:令牌)
if(!findup.containsValue(s))
found = false;
System.out.print(\\\
In map+ found);






找不到
In map true
p>

如果在HashSet上调用remove方法,有没有办法保持containsAll一致?
另外,如果传入的值不存在,则移除method.ContainsAll将保持一致。

  lookup。除去( Alltokens [5]); 
if(lookup.containsAll(tok))

//现在将会是true因为它是假的,如果已经存在的值被删除



可能它需要在HashMap中使用键来做什么,而在HashSet中没有键。请你解释一下怎么做他们的工作?

解决方案

Map.remove(Object) 将删除基于键映射



由于您使用整数 put(i ++,s)),当您调用 findup.remove时,您将删除 nothing (Alltokens [0])!检查它的返回值,看它是否会返回 false


Why does containsAll method on a HashSet does not remain consistent if remove is called on the Set whereas a containsValue method on a HashMap remains consistent after a value is removed After a value is removed from a HashSet containsAll returns false even if all values were present where as in case of HashMap the containsValue method returns correct value

public static void main(String[] args)
    {

    HashSet<String> lookup=new HashSet<String>();
    HashMap<Integer,String> findup=new HashMap<Integer,String>();

    String[] Alltokens={"This","is","a","programming","test","This","is","a","any","language"};
    for(String s:Alltokens)
        lookup.add(s);

    String[] tokens={"This","is","a"};
    Collection<String> tok=Arrays.asList(tokens);

    lookup.remove(Alltokens[5]); 
     if(lookup.containsAll(tok))
       System.out.print("found");
    else    
        System.out.print("NOT found");

    Integer i=0;
    for(String s:Alltokens)
        findup.put(i++,s);
    boolean found=true;
    findup.remove(Alltokens[0]);
        findup.remove(5);               //Edit : trying to remove value whose key is 5
    for(String s:tokens)
        if(!findup.containsValue(s))
            found=false;
    System.out.print("\nIn map " + found);
}

Output NOT found In map true

Is there a way to keep containsAll consistent if remove method is called on the HashSet? Also if a value that was not present in the set is passed to remove method.ContainsAll remains consistent

        lookup.remove("Alltokens[5]");
    if(lookup.containsAll(tok))

//This will be true now where as it is false if a value already present is removed

May be it has got to do something with keys in HashMaps and no keys in HashSet.Can you please explain how do they work?

解决方案

Map.remove(Object) removes a mapping based on the key.

Since you use Integer objects as the keys (put(i++, s)) you will remove nothing when you call findup.remove(Alltokens[0])! Check its return value to see that it will return false.

这篇关于在HashSet和HashMap上包含方法的一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆