使用“动态”后缀将唯一元素添加到 ArrayList

我在 Stack 中看到过类似的问题,但它是针对 C 中的文件的。情况:我有一个 ArrayList of Strings ,其中元素是电子邮件。例如:


jony@test.com

jony1@test.com

gerd@test.com

等等


我想编写一个函数/方法,它将向该列表添加一封新电子邮件,其中包含一个非常重要的问题!:


例如,如果我想添加“jony@test.com”,该方法应该通过 ArrayList,如果“jony@test.com”已经存在,那么它会检查“jony1@test.com”是否存在等,直到它找到免费号码后缀,然后添加它。在我们的示例中,它应该添加“jony2@test.com”。


问题是我想要一些简短而优雅的解决方案,我写的是丑陋的 150 行代码和 3 种方法等,看起来不太好。任何人都有一个很好的算法或者可能是高级的 Collection 功能来完成这个?


茅侃侃
浏览 161回答 4
4回答

ibeautiful

您可以在添加之前检查您的列表,如下所示:public void addWithSuffix(String email) {    if(list.contains(email)) {        int number = 0;        String[] tmp = email.split("@");        for(; list.contains(tmp[0] + number + "@" + tmp[1]); number++){}        list.add((tmp[0] + number + "@" + tmp[1]));    }    else {        list.add(email);    }}但是,当然,在尝试添加之前验证您的输入以确保电子邮件有效。而且我还建议将 a 换成ListaSet

倚天杖

创建自定义添加方法并使用拆分值并检查直到达到最大值,import java.util.ArrayList;import java.util.List;public class CustomListAdd {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List <String> list=new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; add("jony@test.com",list);&nbsp; &nbsp; &nbsp; &nbsp; add("jony@test.com",list);&nbsp; &nbsp; &nbsp; &nbsp; add("jony@test.com",list);&nbsp; &nbsp; &nbsp; &nbsp; add("jony@test.com",list);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list);&nbsp; &nbsp; }&nbsp; &nbsp; public static void add(String value,List<String> list) {&nbsp; &nbsp; &nbsp; &nbsp; if(list.contains(value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int count = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] strSplited = value.split("@");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(list.contains(strSplited[0] + count + "@" + strSplited[1]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add((strSplited[0] + count + "@" + strSplited[1]));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

吃鸡游戏

尝试这个主班List <String> list=new ArrayList<String>();&nbsp; &nbsp; list.add("jony@test.com");&nbsp; &nbsp; list.add("jony1@test.com");&nbsp; &nbsp; list.add("jony2@test.com");&nbsp; &nbsp; list.add("jony3@test.com");&nbsp; &nbsp; list.add("gerd@test.com");&nbsp; &nbsp; String newEmail = "jony@test.com";&nbsp; &nbsp; String last = "";&nbsp; &nbsp; for (String string : list) {&nbsp; &nbsp; &nbsp; &nbsp; if(compareString(newEmail , string)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last = string;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; int newNumber = Integer.valueOf(last.substring(last.indexOf("@")-1, last.indexOf("@")))+1;&nbsp; &nbsp; String[] s = newEmail.split("@");&nbsp; &nbsp; list.add(s[0]+ newNumber + s[1]);&nbsp; &nbsp; System.out.println(list);替换数字后比较字符串private static boolean compareString(String string, String string2) {&nbsp; &nbsp; string2 = string2.replaceAll("\\d","");&nbsp; &nbsp; return string.equals(string2);}

幕布斯6054654

尝试这个:import java.io.*;import java.util.*;public class DynamicMail{private Map<String, Integer> map = new HashMap<>();private List<String> emails = new ArrayList<>();public static void main(String args[]){&nbsp; &nbsp; DynamicMail dm = new DynamicMail();&nbsp; &nbsp; dm.addToList("jony@test.com");&nbsp; &nbsp; dm.addToList("gary@test.com");&nbsp; &nbsp; dm.addToList("jony@test.com");&nbsp; &nbsp; dm.addToList("jony@test.com");&nbsp; &nbsp; dm.addToList("jony@test.com");&nbsp; &nbsp; dm.addToList("gary@test.com");&nbsp; &nbsp; dm.addToList("ghost@test.com");&nbsp; &nbsp; System.out.println(dm.getEmails());}public Map<String, Integer> getMap(){&nbsp; &nbsp; return map;}public List<String> getEmails(){&nbsp; &nbsp; return emails;}public void addToList(String email){&nbsp; &nbsp; String[] parts = email.split("@");&nbsp; &nbsp; Integer i = map.computeIfAbsent(parts[0], x->new Integer(0));&nbsp; &nbsp; if(i!=0)&nbsp; &nbsp; &nbsp; &nbsp; emails.add(parts[0]+i+"@"+parts[1]);&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; emails.add(email);&nbsp; &nbsp; map.put(parts[0], ++i);}}假设您从一个空列表开始,并且 jony1@test.com 或 gerd2@test.com 之类的电子邮件不会作为输入,这将起作用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java