Use
ArrayList.contains("StringToBeChecked");
If the String is present in the ArrayList, this function will return true, else will return false.
Answer from Logan on Stack OverflowUse
ArrayList.contains("StringToBeChecked");
If the String is present in the ArrayList, this function will return true, else will return false.
Look at the List#contains(T obj) method, like this:
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("xyz");
list.contains("abc"); // true
list.contains("foo"); // false
I have a list,
List<Equipment> equipment = response.getEquipment();
public class Equipment {
private String initial;
private String number;
// other attributes im not concerned with atm and I want to see check if the value of initial and number for each piece of equipment equipment.get(index).getInitial()
equipment.get(index).getNumber()
is in the String:
String params = "ABCD1234,DEFG5678"
If it is, remove from string or replace with ""
The end goal is to have the
String params = "the number/numbers and initial/initials not present in List<Equipment> equipment"
So if equipment contains 1 entry whose initial = "ABC" and number="123", and the String params = "ABC123,DEF456", the resulting String params should be = "DEF456".
I'm not sure if my question makes sense, please let me know if you need any more info.
Java streams are handy for this
String[] value = {"Available to trade at 12 30", "I love sherlock"};
Stream.of(value).anyMatch(s -> s.contains("sherlock"));
If you want to get the string that has sherlock:
String[] value = {"Available to trade at 12 30", "I love sherlock"};
Stream.of(value).filter(s -> s.contains("sherlock")).findFirst().get();
Or use findAny(), if you don't care about order. Both findAny and findFirst return an Optional, which will be empty if there are no matches and .get() will throw.
You can do something like this
Arrays.asList(Value).contains("string to be searched");
Better option is to convert array to list so that multiple functions can be used
That's simple with Java 8:
String str = "foo";
List<String> strings = Arrays.asList("foo", "foo", "foo", "foo", "foo");
boolean allMatch = strings.stream().allMatch(s -> s.equals(str));
For Java 7 replace the last line with:
boolean allMatch = true;
for (String string : strings) {
if (!string.equals(str)) {
allMatch = false;
break;
}
}
If you want to know if the array contains the string use ArrayList::contains()
String s = "HI";
ArrayList<String> strings = // here you have your string
if (string.contains(s)) {
// do your stuff
}
If you want to check if all values are same, iterate and count. If you have JAVA8 check steffen sollution.
boolean areSame = true;
for (String str : strings) {
if (!str.equals(s)) areSame = false;
}
if (areSame) {
// all elements are same
}
Perhaps the easiest solution would be to use two List<List<String>>s instead. Assuming the List implementations used extend AbstractList, using equals will give you the desired behavior. From the documentation for AbstractList.equals:
Compares the specified object with this list for equality. Returns
trueif and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elementse1ande2are equal if(e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.
You can easily wrap a String[] in a thin List<String> implementation that extends AbstractList by using Arrays.asList.
EDIT: Here's an example:
String[] array1 = {"1", "2", "3"};
String[] array2 = {"4", "7"};
String[] array3 = {"1", "2", "3"};
String[] array4 = {"4", "7"};
List<List<String>> lst1 = new ArrayList<>();
lst1.add(Arrays.asList(array1));
lst1.add(Arrays.asList(array2));
List<List<String>> lst2 = new ArrayList<>();
lst2.add(Arrays.asList(array3));
lst2.add(Arrays.asList(array4));
System.out.println(lst1.equals(lst2)); //prints true
Typically you should avoid dealing with Arrays. they are ugly and lead to these kind of problems. If possible use List<List<String>> then you can use .equals() normally.
if you insist, you could use a custom isequal implementation like below. the key is to use Arrays.equals()
public class DemoEquals {
List<String[]> listOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
List<String[]> listOneOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
List<String[]> listTwo = (List<String[]>) Arrays.asList(new String[]{"2one1", "2one2"}, new String[]{"2two1"});
private boolean isEqual(List<String[]> list1, List<String[]> list2) {
if (list1.size() != list2.size()) return false;
for (int i = 0; i < list1.size(); i++) {
if (!Arrays.equals(list1.get(i), list2.get(i))) return false;
}
return true;
}
@SuppressWarnings("unchecked")
private void isEqual() {
//prints true
System.out.println(isEqual(Collections.EMPTY_LIST, Collections.EMPTY_LIST));
//prints true
System.out.println(isEqual(listOne, listOne));
//prints true
System.out.println(isEqual(listOne, listOneOne));
//prints false
System.out.println(isEqual(listOne, listTwo));
//prints false
System.out.println(isEqual(listOne, Collections.EMPTY_LIST));
}
public static void main(String[] args) {
new DemoEquals().isEqual();
}
}
You are right in saying that if they are arrays you can't use Arrays.equals(), if they are List<>s then you cannot just use equals. In both cases they also check order.
So yes your main option without writing your own comparison algorithm is to use Collections.sort() on both lists.
If you don't need to check for duplicates you could drop both lists into a HashSet using addAll and then compare the two HashSet or just use List.containsAll. The bad news though is that these would both have the same limitation, if you need to compare lists that might have repeated elements then it may give incorrect results. i.e. "bob", "bob", "fred" would compare as equal to "bob", "fred", "fred".
Please refer to the example below:
Copypublic class Example{
public static void main(String[] args){
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
list1.add("A");
list1.add("B");
list1.add("C");
list2.add("C");
list2.add("X");
list2.add("Y");
for(String s : list1){
if(list2.contains(s)){
System.out.println("List 2 contains: " + s);
}
}
}
}
The code above is by all means not the cleanest or the most compact way to achieve what you are asking. But given the information presented, this should suffice.
Don't use != to compare strings. Use the equals method :
if (! Blist.get(i).equals(Alist.get(j))
But this wouldn't probably fix your algorithmic problem (which isn't clear at all).
If what you want is know what items are the same at the same position, you could use a simple loop :
int sizeOfTheShortestList = Math.min(Alist.size(), Blist.size());
for (int i=0; i<sizeOfTheShortestList; i++) {
if (Blist.get(i).equals(Alist.get(i))) {
System.out.println("Equals..: " + Blist.get(i));
}
}
If you want to get items that are in both lists, use
for (int i = 0; i < Alist.size(); i++) {
if (Blist.contains(Alist.get(i))) {
System.out.println("Equals..: " + Alist.get(i));
}
}
You can use the RemoveAll(Collection c) on one of the lists, if you happen to know if one list always contains them all.