java 比较两个 String[] 数组并打印出不同的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36122735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two String[] arrays and print out the strings which differ
提问by Martin Erlic
I've got a list of all the file names in a folder and a list of files which have been manually "checked" by a developer. How would I go about comparing the two arrays such that we print out only those which are not contained in the master list.
我有一个文件夹中所有文件名的列表和一个由开发人员手动“检查”的文件列表。我将如何比较两个数组,以便我们只打印出那些未包含在主列表中的数组。
public static void main(String[] args) throws java.lang.Exception {
String[] list = {"my_purchases", "my_reservation_history", "my_reservations", "my_sales", "my_wallet", "notifications", "order_confirmation", "payment", "payment_methods", "pricing", "privacy", "privacy_policy", "profile_menu", "ratings", "register", "reviews", "search_listings", "search_listings_forms", "submit_listing", "submit_listing_forms", "terms_of_service", "transaction_history", "trust_verification", "unsubscribe", "user", "verify_email", "verify_shipping", "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu", "main_searchbar", "primary_navbar"};
String[] checked = {"404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu"};
ArrayList<String> ar = new ArrayList<String>();
for(int i = 0; i < checked.length; i++)
{
if(!Arrays.asList(list).contains(checked[i]))
ar.add(checked[i]);
}
}
回答by SatyaTNV
Change your loop to :
将您的循环更改为:
ArrayList<String> ar = new ArrayList<String>();
for(int i = 0; i < checked.length; i++) {
if(!Arrays.asList(list).contains(checked[i]))
ar.add(checked[i]);
}
ArrayList
ar
should be outside of the forloop. Otherwise ar
will be created every time when element of checked
array exists in list
.
ArrayList
ar
应该在for循环之外。否则ar
每次当checked
数组元素存在于list
.
Edit:
编辑:
if(!Arrays.asList(list).contains(checked))
With this statement you are checking whether the checked
reference is not the element of list
. It should be checked[i]
to check whether the element of checked
exists in list
or not.
使用此语句,您将检查checked
引用是否不是 的元素list
。应该是checked[i]
检查 的元素是否checked
存在list
。
If you want to print elements in list
that are not in checked
. Then use :
如果要打印list
不在checked
. 然后使用:
for(int i = 0; i < list.length; i++) {
if(!Arrays.asList(checked).contains(list[i]))
ar.add(list[i]);
}
System.out.println(ar);
回答by Luke Wahlmeier
Your updated solution seems kind of odd to me, not sure why you would add list[i] to the result list. Generally this sounds like something hashsets are made for:
您更新后的解决方案对我来说似乎有点奇怪,不知道为什么要将 list[i] 添加到结果列表中。通常,这听起来像是哈希集的用途:
String[] list = { "my_purchases", "my_reservation_history","my_reservations","my_sales", "my_wallet", "notifications", "order_confirmation", "payment", "payment_methods", "pricing", "privacy", "privacy_policy", "profile_menu", "ratings", "register", "reviews", "search_listings", "search_listings_forms", "submit_listing", "submit_listing_forms", "terms_of_service", "transaction_history", "trust_verification", "unsubscribe", "user", "verify_email", "verify_shipping", "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu", "main_searchbar", "primary_navbar"};
String[] checked = { "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu"};
HashSet<String> s1 = new HashSet<String>(Arrays.asList(checked));
s1.removeAll(Arrays.asList(list));
System.out.println(s1);
回答by Pat Myron
for (String s: checked) { // go through all in second list
if (! list.contains(s)) { // if string not in master list
System.out.println(s); // print that string
}
}
回答by Philipp
First of all, I think your code has some errors:
首先,我认为您的代码有一些错误:
- s1 is not defined
- ar is not defined
- you mean to use Arrays.toString instead of Array.toString
- s1 未定义
- ar 未定义
- 你的意思是使用 Arrays.toString 而不是 Array.toString
So I fixed your code (using Java 8) and it should work like that:
所以我修复了你的代码(使用 Java 8),它应该像这样工作:
public static void main(String[] args) throws java.lang.Exception {
String[] list = {"my_purchases", "my_reservation_history", "my_reservations", "my_sales", "my_wallet", "notifications", "order_confirmation", "payment", "payment_methods", "pricing", "privacy", "privacy_policy", "profile_menu", "ratings", "register", "reviews", "search_listings", "search_listings_forms", "submit_listing", "submit_listing_forms", "terms_of_service", "transaction_history", "trust_verification", "unsubscribe", "user", "verify_email", "verify_shipping", "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu", "main_searchbar", "primary_navbar"};
String[] checked = {"404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu"};
final List<String> result = Stream.of(list)
.filter(listEntry -> Stream.of(checked)
.filter(checkedEntry -> checkedEntry.equals(listEntry)).findFirst().orElse(null) == null)
.collect(Collectors.toList());
System.out.println(result);
}
If you don't want to use Java 8, you have to replace the usage of Streams and filters and collect with the appropriate functions in Java 7 (see e.g., Satya's post).
如果您不想使用 Java 8,则必须替换 Streams 和过滤器的使用,并使用 Java 7 中的适当函数进行收集(参见例如 Satya 的帖子)。
Anyways, I should mention that there are better (regarding performance) implementations to solve your problem, e.g.,
无论如何,我应该提到有更好的(关于性能)实现来解决您的问题,例如,
- you could sort your lists prior to searching for duplicates,
- you could use, e.g., hash-based implementations to increase the speed when searching for duplicates,
- you could move the code outside of the inner loop,
- and many more
- 您可以在搜索重复项之前对列表进行排序,
- 您可以使用例如基于哈希的实现来提高搜索重复项时的速度,
- 你可以将代码移到内部循环之外,
- 还有很多