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

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

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

问题

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

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

can someone please help me to solve this?
Thank you

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

答案1

得分: 1

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

  1. String a = "1||2||3";
  2. String b = "2||4||5";
  3. String[] arrayA = a.split("\\|\\|");
  4. String[] arrayB = b.split("\\|\\|");
  5. List<String> list = new ArrayList<>(Arrays.asList(arrayA));
  6. list.addAll(Arrays.asList(arrayB));
  7. 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

  1. String a = &quot;1||2||3&quot;;
  2. String b = &quot;2||4||5&quot;;
  3. String[] arrayA = a.split(&quot;||&quot;);
  4. String[] arrayB = b.split(&quot;||&quot;);
  5. List&lt;String&gt; list = Arrays.asList(arrayA);
  6. list.addAll(Arrays.asList(arrayB));
  7. 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:

确定