contains()方法不起作用

时间:2016-12-09 01:46:48

标签: java string contains

private List<String> values = new ArrayList<String>();

public WhitespaceEqualsTest() {
    values.add("I ");
    values.add("I");
    values.add(". ");
    values.add(".");
    values.add("1");
    values.add("1 ");

    System.out.println(refine(values));
}

private List<String> refine(List<String> input){
    ArrayList<String> outerLoopValues = (ArrayList<String>) input;
    ArrayList<String> innerLoopValues = (ArrayList<String>) input;
    ArrayList<String> results = new ArrayList<String>();

    for(String string1 : outerLoopValues){
        for(String string2 : innerLoopValues){
            if(string1.contains(string2) == false){
                results.add(string1);
            }
        }
    }

    Set<String> temp = new HashSet<String>();
    temp.addAll(results);
    results.clear();
    results.addAll(temp);

    return results;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((values == null) ? 0 : values.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    WhitespaceEqualsTest other = (WhitespaceEqualsTest) obj;
    if (values == null) {
        if (other.values != null)
            return false;
    } else if (!values.equals(other.values))
        return false;
    return true;
}

我已经覆盖了hashCode()equals(),因此我不确定错误是什么。它们是使用Eclipse生成的(Source - &gt; Generate hashCode()和equals())。为什么没有检测到没有空格的相同字符包含在带空格的字符中?输出是:

[1, . , I , I, ., 1 ]

3 个答案:

答案 0 :(得分:0)

正如其中一条评论中所提到的,您应该使用String包装器来包装字符串并覆盖equals和hashcode方法。

我的解决方案基于"I "应该等于"I"的假设,因此只应将其中一个添加到结果中。

但是我需要根据Java Objects中的文档添加  和Java Arraylist分别涉及equalscontainshashcode方法必须返回一个公共值。我在代码中写了解释为注释。如果有任何问题,请告诉我。

主类

public class StackOverflowMain
{
    private static List<String> values = new ArrayList<String>();

    public static void main(String[] args) {

        values.add("I ");
        values.add("I");
        values.add(". ");
        values.add(".");
        values.add("1");
        values.add("1 ");
        List<WhitespaceEqualsTest> toRefineList = new ArrayList<WhitespaceEqualsTest>();
        for (String value : values) {
            toRefineList.add(new WhitespaceEqualsTest(value));
        }

        System.out.println(refine(toRefineList));
    }

    private static List<WhitespaceEqualsTest> refine(List<WhitespaceEqualsTest> input) {
        ArrayList<WhitespaceEqualsTest> loopValues = (ArrayList<WhitespaceEqualsTest>) input;
        ArrayList<WhitespaceEqualsTest> results = new ArrayList<WhitespaceEqualsTest>();

        for (WhitespaceEqualsTest value : loopValues) {
            if (!results.contains(loopValues)) {
                results.add(value);
            }
        }

        Set<WhitespaceEqualsTest> temp = new HashSet<WhitespaceEqualsTest>();
        temp.addAll(results);
        results.clear();
        results.addAll(temp);

        return results;

    }
}

内部WhitespaceEqualsTest

class WhitespaceEqualsTest {
    private String value;

    public WhitespaceEqualsTest(String value) {
        this.value = value;
    }

    public void setString(String value) {
        this.value = value;
    }

    public String getString() {
        return this.value;
    }

    public int hashCode() {
        /*
         * Arraylist.contains is evaluated by using (o==null ? e==null : o.equals(e)) as mentioned in the javadoc
         * and Object.equals() would evaluate using hashcode() first to check if the object o is equal to object e
         * before calling .equals() method to evaluate.
         * 
         * As mentioned in java doc at http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html#equals(java.lang.Object)
         * c1.equals(c2) implies that c1.hashCode()==c2.hashCode() should be satisfied
         * which is not in this question
         */      
        return 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        WhitespaceEqualsTest other = (WhitespaceEqualsTest) obj;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.contains(other.value) && !other.value.contains(value)){ 
            /*
             * Does a checking on both ends since "I " contains "I" but "I" does not contain "I " due to the whitespace
             * For this question, if one of the condition satisfy it should be equal
             */
            return false;
        }           
        return true;
    }

    @Override
    public String toString() {
        return this.value;
    }
}

结果

[I , . , 1]

答案 1 :(得分:0)

String类是final。所以你不能覆盖它的equals和hashCode方法。

private List<StringWrapper> values = new ArrayList<StringWrapper>();

public WhitespaceEqualsTest() {
    values.add(new StringWrapper("I "));
    values.add(new StringWrapper("I"));
    values.add(new StringWrapper(". "));
    values.add(new StringWrapper("."));
    values.add(new StringWrapper("1"));
    values.add(new StringWrapper("1 "));

    System.out.println(refine(values));
}

private List<StringWrapper> refine(List<StringWrapper> input){
    //no need to iterate the list
    //the set will automatically cancel out the duplicate
    Set<StringWrapper> temp = new HashSet<StringWrapper>(input);

    ArrayList<StringWrapper> results = new ArrayList<StringWrapper>();
    results.addAll(temp);
    return results;
}

创建一个String的包装类,然后重写equals和hashcode方法。

class StringWrapper {
    private String value;

    public StringWrapper(String value){
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    @Override
    public String toString(){
        return value;
    }
    @Override
    public boolean equals(Object obj){
        boolean result = Boolean.FALSE;
        if(obj != null && obj instanceof StringWrapper){
            StringWrapper stringWrapper = (StringWrapper) obj;
            result = value.trim().equals(stringWrapper.getValue().trim());
        }
        return result;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((value.trim() == null) ? 0 : value.trim().hashCode());
        return result;
    }
}

答案 2 :(得分:-1)

您将值添加到Set。在任何情况下,Set都会出现一次值 - 因此它是一个集合。 ;)

您也可以修改循环以查看发生的情况

for(String string1 : outerLoopValues){
            for(String string2 : innerLoopValues){
                if(string1.contains(string2) == false){
                    results.add(string1);
                    System.out.println("added \"" + string1 + "\" since it does not contain \"" + string2 + "\"");
                }
            }
        }

提供以下输出:

added "I " since it does  not contain ". "
added "I " since it does  not contain "."
added "I " since it does  not contain "1"
added "I " since it does  not contain "1 "
added "I" since it does  not contain "I "
added "I" since it does  not contain ". "
added "I" since it does  not contain "."
added "I" since it does  not contain "1"
added "I" since it does  not contain "1 "
......
[1, . , I , I, ., 1 ]

如果他们不相互包含它们是我想的想法吗?

然后通过Set推送List删除重复项!见这里:Does adding a duplicate value to a HashSet/HashMap replace the previous value

将循环中的条件从false更改为true会产生此结果(在最后一行输出中使用Set / HashSet后没有变化!)

added "I " since it does contain "I "
added "I " since it does contain "I"
added "I" since it does contain "I"
added ". " since it does contain ". "
added ". " since it does contain "."
added "." since it does contain "."
added "1" since it does contain "1"
added "1 " since it does contain "1"
added "1 " since it does contain "1 "
[1, . , I , I, ., 1 ]

它回答了你的问题:它确实检测到了“我”包含“我”。

System.out.println("I ".contains("I"));

说“真实”

希望这有助于^^ - d