Ragnar Johansen
Ragnar Johansen

Reputation: 83

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

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);
     }
 }

Upvotes: 8

Views: 26222

Answers (4)

Kaushik
Kaushik

Reputation: 3381

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
}

Upvotes: 0

Kris
Kris

Reputation: 1724

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);

Upvotes: 6

Aaron Davis
Aaron Davis

Reputation: 1731

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);
    }
}

Upvotes: 8

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);
}

Upvotes: 2

Related Questions