在nameList.forEach过程中,为nameList中每个元素,添加一个“;”
- AtomicReference<String> namesAtomicReference = new AtomicReference<>();
- List<String> nameList = new ArrayList<>();
- nameList.add("张三");
- nameList.add("李四");
- nameList.add("王五");
-
- //在nameList.forEach过程中,为nameList中每个元素,添加一个“;”
- nameList.forEach(name->{
- String str = "";
- if(ObjectUtil.isEmpty(namesAtomicReference.get())){
- str = name+";";
- }
- else{
- str = namesAtomicReference.get()+name+";";
- }
- namesAtomicReference.set(str);
- });
-
- String strNames = namesAtomicReference.toString();
- StringBuilder names = new StringBuilder(strNames);
- names.replace(strNames.length()-1,strNames.length(),"");
- System.out.println("姓名为:{"+names+"}");
参考:【精选】java:AtomicReference使用_java atomicreference_小徐也要努力鸭的博客-CSDN博客