[英]contains() method not working
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生成的(源->生成hashCode()和equals())。 為什么沒有檢測到帶有空格的字符中包含相同的沒有空格的字符? 輸出為:
[1, . , I , I, ., 1 ]
如注釋之一所述,您應該使用字符串包裝器包裝字符串並覆蓋equals和hashcode方法。
我的解決方案基於這樣的假設: "I "
應等於"I"
,因此僅應將其中一個添加到結果中。
但是,我需要根據Java Objects和Java Arraylist中的文檔分別添加關於equals
和contains
實現的內容。 hashcode
方法將必須返回一個公共值。 我已經在代碼中寫了解釋作為注釋。 讓我知道是否有任何問題。
主班
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]
字符串類是最終的。 因此,您不能覆蓋其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;
}
}
您將值添加到集合。 在任何情況下,一個集合中的值只出現一次-因此它是一個集合。 ;)
您最好修改循環以查看會發生什么
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 ]
如果它們彼此不包含,添加它們是我的想法嗎?
然后通過集合推送列表將刪除重復項! 參見此處: 向HashSet / HashMap中添加重復值是否替換了先前的值
將Loop中的條件從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 ]
回答您的問題:它確實檢測例如“ I”是否包含“ I”。
System.out.println("I ".contains("I"));
說“真”
希望這可以幫助^^-d
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.