Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check two lists of strings against eachother?

Taking a class in OOP Java and since I am completely new to the language and as of yet unaware of the many tools it has to offer I find myself fumbling in the dark for solutions to simple things, I can hardcode these problems but I feel there is far simpler ways to do this with java.util.

Assume I have a list of strings

String[] stringOne = {"a","b","c", "potato"};
String[] stringTwo = {"potato", "13"};

How do I check how many times any given item in stringTwo occurs in stringOne? Or vice versa. I would rather not double loop everytime I have this problem (or make a double-loop method).

Right now I get away with using Collections.frequency() and only looping once but is there a simpler way? Thanks.

like image 404
arynaq Avatar asked Feb 04 '13 19:02

arynaq


People also ask

How do you compare two lists of strings?

By using Intersect , we can check which elements in source list are also contained in compare list. var source = new List<string>() { "a", "b", "c" }; var compare = new List<string>() { "b", "c", "d" }; var result = source. Intersect(compare);

How do I compare two lists of strings in Python?

Python sort() method and == operator to compare lists We can club the Python sort() method with the == operator to compare two lists. Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

How do I compare two identical lists?

Using Counter() , we usually are able to get frequency of each element in list, checking for it, for both the list, we can check if two lists are identical or not. But this method also ignores the ordering of the elements in the list and only takes into account the frequency of elements.


1 Answers

Assume I have a list of strings

String[] stringOne = {"a","b","c", "potato"};
String[] stringTwo = {"potato", "13"};

These are "arrays", not "lists". Arrays.asList(stringOne) is a list wrapper for the array referenced by stringOne.

How do I check how many times any given item in stringTwo occurs in stringOne?

new HashSet(Arrays.asList(stringOne)).retainAll(Arrays.asList(stringTwo))

will give you the set of elements in both.

A similar approach with a Bag instead of a set will get you counts.

retainAll(Collection coll)

Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object and the bag has m > n copies, then delete m - n copies from the bag. In addition, if e is an object in the bag but !coll.contains(e), then remove e and any of its copies.

The notion of equivalence used in the collection classes is that defined by Object.equals(object) which java.lang.String overrides to compare lexicographically.

If you need the result back in an array, try myBag.toArray(new String[0]) which dumps the contents to a string. The argument is explained in the documentation, but in short, its a type-hint that works around Java's broken array variance.

like image 123
Mike Samuel Avatar answered Nov 15 '22 00:11

Mike Samuel