Checking if an ArrayList contains a certain String while being case insensitive

℡╲_俬逩灬. 提交于 2020-06-23 06:43:09

问题


How can i search through an ArrayList using the .contains method while being case insensitive? I've tried .containsIgnoreCase but found out that the IgnoreCase method only works for Strings.

Here's the method I'm trying to create:

 private ArrayList<String> Ord = new ArrayList<String>(); 

 public void leggTilOrd(String ord){
     if (!Ord.contains(ord)){
         Ord.add(ord);
     }
 }

回答1:


You will need to iterate over the list and check each element. This is what is happening in the contains method. Since you are wanting to use the equalsIgnoreCase method instead of the equals method for the String elements you are checking, you will need to do it explicitly. That can either be with a for-each loop or with a Stream (example below is with a Stream).

private final List<String> list = new ArrayList<>();

public void addIfNotPresent(String str) {
    if (list.stream().noneMatch(s -> s.equalsIgnoreCase(str))) {
        list.add(str);
    }
}



回答2:


If you are using Java7, simply override the contains() method,

public class CastInsensitiveList extends ArrayList<String> {
    @Override
    public boolean contains(Object obj) {
        String object = (String)obj;
        for (String string : this) {
            if (object.equalsIgnoreCase(string)) {
              return true;
            }
        }
        return false;
    }
}

If you are using Java 8.0, using streaming API,

List<String> arrayList = new ArrayList<>();
arrayList.stream().anyMatch(string::equalsIgnoreCase);



回答3:


The List#Ccontains() method check if the parameter is present in the list but no changes are made in the list elements

use streams instead

public void leggTilOrd(String ordParameter) {
    final List<String> ord = Arrays.asList(new String[]{ "a", "A", "b" });
    final boolean ordExists = ord.stream().anyMatch(t -> t.equalsIgnoreCase(ordParameter));
    System.out.println(ordExists);
}



回答4:


If you want to avoid duplicates use HashSet instead of List. Hashing works faster while searching. In the underlying class override the equals and hashcode method to return expected results using String.toUpperCase(). If you have a List of String, you can create a string wrapper class.

String Wrapper could look like this:-

public class CaseInsensitiveString {

    String string;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((string == null) ? 0 : string.toUpperCase().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;
        CaseInsensitiveString other = (CaseInsensitiveString) obj;
        if (string == null) {
            if (other.string != null)
                return false;
        } else if (!string.toUpperCase().equals(other.string.toUpperCase()))
            return false;
        return true;
    }

    // Other Methods to access string
}


来源:https://stackoverflow.com/questions/39992020/checking-if-an-arraylist-contains-a-certain-string-while-being-case-insensitive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!