How to merge two "strings" with unique values to new String in java? Using converting to arrays or lists

huangapple go评论78阅读模式
英文:

How to merge two "strings" with unique values to new String in java? Using converting to arrays or lists

问题

可以有人帮我解决这个问题吗?
谢谢

String a = 1 || 2 || 3
String b = 2 || 4 || 5

String c = 1 || 2 || 3 || 4 || 5 ||  // 必须是唯一的
英文:

can someone please help me to solve this?
Thank you

String a = 1||2||3
String b = 2||4||5

String c = 1||2||3||4||5|| //Must be uniqe

答案1

得分: 1

使用转换成数组还是列表?我不知道你的意思。但我建议使用以下代码:

String a = "1||2||3";
String b = "2||4||5";
String[] arrayA = a.split("\\|\\|");
String[] arrayB = b.split("\\|\\|");
List<String> list = new ArrayList<>(Arrays.asList(arrayA));
list.addAll(Arrays.asList(arrayB));
System.console().printf(list.stream().distinct().collect(Collectors.joining("||")));
英文:

Using converting to arrays or lists? I do not know what your mean. but I suggest

String a = &quot;1||2||3&quot;;
String b = &quot;2||4||5&quot;;
String[] arrayA = a.split(&quot;||&quot;);
String[] arrayB = b.split(&quot;||&quot;);
List&lt;String&gt; list = Arrays.asList(arrayA);
list.addAll(Arrays.asList(arrayB));            
System.console().printf(list.stream().distinct().collect(Collectors.joining(&quot;||&quot;)));

huangapple
  • 本文由 发表于 2020年7月23日 19:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63053246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定