无法从java中的字符串数组中获取匹配的字符串值
我有一个字符串值,其中包含以逗号分隔的电子邮件地址,我必须检查是否有任何相同的邮件地址从字符串数组中添加 我使用逗号使用正则表达式进行拆分,然后不确定我缺少什么,以检查字符串数组中是否找到任何重复项 我使用split来获取用逗号分隔的电子邮件地址数组 然后声明两个for循环进行检查。在内部for循环中,我指定了条件,但它只是打印所有邮件地址。 如何获得匹配的电子邮件地址和唯一的电子邮件地址无法从java中的字符串数组中获取匹配的字符串值,java,arrays,string,Java,Arrays,String,我有一个字符串值,其中包含以逗号分隔的电子邮件地址,我必须检查是否有任何相同的邮件地址从字符串数组中添加 我使用逗号使用正则表达式进行拆分,然后不确定我缺少什么,以检查字符串数组中是否找到任何重复项 我使用split来获取用逗号分隔的电子邮件地址数组 然后声明两个for循环进行检查。在内部for循环中,我指定了条件,但它只是打印所有邮件地址。 如何获得匹配的电子邮件地址和唯一的电子邮件地址 public class EmailDomains { public static void m
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]);
}
}
}
公共类电子邮件域{
公共静态void main(字符串[]args){
字符串st=”test1@gmail.com,"
+ "test2@gmail.com,"
+ "test@yahoo.com,"
+ "test@hotmail.com,"
+ "test@rediff.com,"
+ "test@rediff.com,"
+ "wer@gmailcom";
圣特里姆();
字符串[]总计=st.split(“\\,”);
//系统输出打印长度(总长度);
对于(int i=0;i您将通过列表两次–使用i
和j
。有时i
和j
是相同的值,比如说“3”。在这种情况下if(total[i].trim().equals(total[j].trim()))
为真-这很有意义,因为您正在将列表中的特定条目与自身进行比较
当i
和j
相同时,您似乎想忽略它。您可以通过添加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]);
}
}
}
}
for(int i=0;i
为了让它更简单一点,我建议使用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");
}
最终字符串st=”test1@gmail.com,"
+ "test2@gmail.com,"
+ "test@yahoo.com,"
+ "test@hotmail.com,"
+ "test@rediff.com,"
+ "test@rediff.com,"
+ "wer@gmailcom";
圣特里姆();
最终字符串[]总计=st.split(“,”);
final Map=new HashMap();
对于(最终字符串邮件:总计){
如果(地图集装箱箱(邮件)){
int count=map.get(mail.intValue();
计数++;
map.put(mail,Integer.valueOf(count));
继续;
}
map.put(mail,Integer.valueOf(1));
}
final Iterator Iterator=map.entrySet().Iterator();
while(iterator.hasNext()){
final Entry next=iterator.next();
System.out.println(next.getKey()+“-”+next.getValue().toString()+“entries”);
}
您可以在java8中使用streams api实现这一点。要获得两个列表,一个具有唯一地址,另一个具有重复地址,请使用分组收集器。例如
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);
}
});
String st=”test1@gmail.com,"
+ "test2@gmail.com,"
+ "test@yahoo.com,"
+ "test@hotmail.com,"
+ "test@rediff.com,"
+ "test@rediff.com,"
+ "wer@gmailcom";
圣特里姆();
字符串[]总计=st.split(“\\,”);
List unique=new ArrayList();
List duplicate=new ArrayList();
Arrays.stream(总计)
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.forEach((k,v)->{
如果(v>1){
重复。添加(k);
}否则{
唯一。添加(k);
}
});
在检查字符串的值之前,您可能应该检查以确保i!=j。我必须检查数组@kaan中的重复邮件ID