[英]Unable to get matching string values from String array in java
I have string value which contain email addresses separated by comma and i have to check whether any same mail address been added from String array i have splitted using regex using comma and then not sure what i am missing to check whether any duplicates found in string array 我的字符串值包含用逗号分隔的电子邮件地址,我必须检查是否已从字符串数组中添加任何相同的邮件地址,而我已使用逗号使用正则表达式拆分了它,然后不确定要丢失的内容以检查是否在字符串数组中找到了重复项
I have used split to get array of email addresses separated by comma and then declared two for loop to check. 我使用split来获取用逗号分隔的电子邮件地址数组,然后声明两个for循环进行检查。 inside inner for loop i have specified condition but it is just printing all the mail addresses.
在内部for循环中,我指定了条件,但它只是打印所有邮件地址。 How can i get matched email addresses and unique ones
我如何获得匹配的电子邮件地址和唯一的电子邮件地址
public class EmailDomains {
public static void main(String[] args) {
String st = "test1@gmail.com,"
+ "test2@gmail.com,"
+ "test@yahoo.com,"
+ "test@hotmail.com,"
+ "test@rediff.com,"
+ "test@rediff.com,"
+ "wer@gmailcom";
st.trim();
String[] total = st.split("\\,");
//System.out.println(total.length);
for(int i=0;i<total.length;i++) {
for(int j=0;j<total.length;j++)
{
System.out.println(total[i]+" --- "+total[j]);
if(total[i].trim().equals(total[j].trim()))
{
/*
* System.out.println(total[i]); break;
*/
}
}
//System.out.println(total[i]);
}
}
}
Need to get matched and unmatched email addresses from the above string 需要从上面的字符串中获取匹配和不匹配的电子邮件地址
Friends i have to store matched mails in one array and unique one in another so that i can proceed for validation with unique mail id's, please give me an idea as i am beginner i need help and thanks in advance 朋友,我必须将匹配的邮件存储在一个数组中,将唯一的存储在另一个数组中,以便我可以使用唯一的邮件ID进行验证,请给我一个主意,因为我是新手,我需要帮助并且在此先感谢
You are passing through the list two times – with i
and again with j
. 您将两次遍历该列表–与
i
以及与j
。 Sometimes i
and j
are the same value, say "3". 有时
i
和j
是相同的值,说“ 3”。 In that case if(total[i].trim().equals(total[j].trim()))
is true - which makes sense because you're comparing a specific entry in the list to itself. 在这种情况下,
if(total[i].trim().equals(total[j].trim()))
为if(total[i].trim().equals(total[j].trim()))
这很有意义,因为您正在将列表中的特定条目与其自身进行比较。
It looks like you want to ignore when i
and j
are the same. 当
i
和j
相同时,您似乎想忽略。 You can do this by adding an if
check, something like this: 您可以通过添加
if
检查来完成此操作,如下所示:
for (int i = 0; i < total.length; i++) {
for (int j = 0; j < total.length; j++) {
if (i != j) {
if (total[i].trim().equals(total[j].trim())) {
System.out.println(total[i] + " --- " + total[j]);
}
}
}
}
to keep it a little more simple, i suggest to use a HashMap
to collect email-addresses and counts 为了使其更加简单,我建议使用
HashMap
来收集电子邮件地址和计数
final String st = "test1@gmail.com,"
+ "test2@gmail.com,"
+ "test@yahoo.com,"
+ "test@hotmail.com,"
+ "test@rediff.com,"
+ "test@rediff.com,"
+ "wer@gmailcom";
st.trim();
final String[] total = st.split(",");
final Map<String, Integer> map = new HashMap<>();
for (final String mail : total) {
if (map.containsKey(mail)) {
int count = map.get(mail).intValue();
count++;
map.put(mail, Integer.valueOf(count));
continue;
}
map.put(mail, Integer.valueOf(1));
}
final Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
final Entry<String, Integer> next = iterator.next();
System.out.println(next.getKey() + " - " + next.getValue().toString() + " entries");
}
You can do this in java8 with the streams api. 您可以使用流API在java8中完成此操作。 To get two lists, one with unique addresses and one with duplicates, use a grouping collector.
要获取两个列表,一个具有唯一的地址,一个具有重复的地址,请使用分组收集器。 For example
例如
String st = "test1@gmail.com,"
+ "test2@gmail.com,"
+ "test@yahoo.com,"
+ "test@hotmail.com,"
+ "test@rediff.com,"
+ "test@rediff.com,"
+ "wer@gmailcom";
st.trim();
String[] total = st.split("\\,");
List<String> unique = new ArrayList<>();
List<String> duplicate = new ArrayList<>();
Arrays.stream(total)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.forEach((k, v) -> {
if (v > 1) {
duplicate.add(k);
} else {
unique.add(k);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.