Compare array of strings using collection

I have two arrays String a, b.

String a [] = {"one","two","three"}; String b [] = {"one","Two","Three","four"}; 

I need to check if both arrays are the same or not, with Case Insensitive. I know that the following code snippet is perfect for case sensitivity.

 List <String> l1 = Arrays.asList(a); List <String> l2 = Arrays.asList(b); System.out.println(l2.containsAll(l1)); 

Is there any other way to compare two string arrays (case insensitive) using a collection?

+4
source share
8 answers

Finally, I used a TreeSet with a case-insensitive comparator.

Example:

  String [] oldVal = {"one","two","three","Four"}; String [] newVal = {"one","Two","Three","four"}; Set <String> set1 = new TreeSet <String> (String.CASE_INSENSITIVE_ORDER); Set <String> set2 = new TreeSet <String> (String.CASE_INSENSITIVE_ORDER); set1.addAll(Arrays.asList(oldVal)); set2.addAll(Arrays.asList(newVal)); System.out.println("--Using Tree Set --- "+ set1.containsAll(set2)); // Return True 

Thanks guys..

+4
source

Could you just loop it or use some kind of linq (sorry it was java, you can't use linq ...?)

  List<string> matches = new List<string>(); bool isSame=true; foreach(string s1 in l1) { foreach(string s2 in l2) { if(s1.ToLower() == s2.ToLower()) matches.Add(s1); else { isSame=false; break; } } if (isSame) continue; else break; } if (isSame) Console.Writeline("They are the same") else Console.Writeline("Not the same"); 

You can check the counter since I did not add it to the code, for example l1.count> l2.count (in this case you know if they are the same by the number of elements in the list). A simple test before the loop:

 if (l1.Count != l2.Count) { //don't even bother looping //display no matches } else { //place rest of code here since l1.count = l2.count } 
  • CRAP DOES NOT IMPLEMENT IT WAS FOR JAVA THAT IT WAS FOR C #. APPLY YOUR JELLY LOGIC IN JAVA MYS ...
+1
source

If arrays do not contain duplicates , one way to do this in O(N) is to use Set , which represents the canonical form of the strings in the array. Something like that:

 static Set<String> canonicalSet(String[] arr) { Set<String> upperSet = new HashSet<String>(); for (String s : arr) { upperSet.add(s.toUpperCase()); } return upperSet; } static boolean equalsCanonically(String[] arr1, String[] arr2) { return canonicalSet(arr1).equals(canonicalSet(arr2)); } 

This is optimal in time.

You can also make variations of this technique to save more space, for example. instead of constructing canonical sets and comparing them, you can build a canonical set for arr1 , and then remove entries from this set in accordance with the elements of arr2 . After that, the set is empty, and you can always find what you need to delete, the two arrays are canonically equal.

 static boolean equalsCanonically2(String[] arr1, String[] arr2) { Set<String> canon = canonicalSet(arr1); for (String s : arr2) { if (!canon.remove(s.toUpperCase())) return false; } return canon.isEmpty(); } 

You can also do a simple size matching check if you think it's worth it (i.e. if often two arrays don't have the same number of elements).

If there are duplicates in arrays, the Set method will not work as it is. You will need a multiset, and you can either implement your own or use the Google collection.


There is also an O(N log N) way to do this using string sorting. You can sort both arrays and then do a simple linear check. A case-insensitive comparator should be used, and in fact it already exists as String.CASE_INSENSITIVE_ORDER .

 static boolean equalsCanonically3(String[] arr1, String[] arr2) { int N = arr1.length; if (arr2.length != N) return false; Arrays.sort(arr1, String.CASE_INSENSITIVE_ORDER); Arrays.sort(arr2, String.CASE_INSENSITIVE_ORDER); for (int i = 0; i < N; i++) { if (String.CASE_INSENSITIVE_ORDER.compare(arr1[i], arr2[i]) != 0) { return false; } } return true; } 

This last method works even if arrays contain duplicates. He does it O(N log N) . It sorts the arrays passed as parameters, so if the initial state is important, you want to pass them to clone() .

+1
source

You can use TreeMap with a case-insensitive comparator.

+1
source

check it in nested loops if you want to do arbitrary comparisons. or if you have large datasets, it may be cheaper to sort arrays first.

0
source

Your sample data is sorted. If this is really guaranteed, you should do as Andrei says, and use nested loops on the arrays themselves, breaking if / when you find an unequal pair of records.

If they are not sorted, I would dump each of them into a HashSet, and then you can use the java Set containsAll method.

Edit: As Tomman noted, containsAll () ultimately relies on equals (). Therefore, in order to get case-insensitive registration of questions, you have two options:

1) Flip or collapse lines when pasting into sets. When considering, I am not crazy about this method, since you will not only lose duplicate records, but also discard records differentiated by case. And so these lists will look equal to each other:

 String a [] = {"one","one","one", "Two"}; String b [] = {"One", Two"}; 

2) Another option is to put your lines in holder objects that override equals (), making case insensitive comparisons.

0
source

You can first check if their lengths are equal. Then you can put elements a in the HashMap and go b and check if there are any elements.

0
source

Using one for a loop -

 String [] oldVal = {"one","two","three","Four"}; String [] newVal = {"one","Two","Three","four"}; if(oldVal.length == newVal.length) { // for(int y =0; y<oldVal.length; y++) { oldVal[y] = oldVal[y].toUpperCase(); newVal[y] = newVal[y].toUpperCase(); } return Arrays.asList(oldVal).containsAll(Arrays.asList(newVal)); } return false; 
0
source

Source: https://habr.com/ru/post/1303701/


All Articles