将一个列表的值分割为元素。

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

how to split one list value to elements

问题

获取以下列表中的元素方法:

  1. List<String> list = new ArrayList<>();
  2. List<String> getRoles =
  3. buildRolesList(list, role).stream()
  4. .map(String::trim)
  5. .map(String::toLowerCase)
  6. .map(str->str.replace("[",""))
  7. .map(str->str.replace("]",""))
  8. .collect(Collectors.toCollection(ArrayList::new));

getRoles = "admin, user" ---这是我得到的列表,但想要的列表是["admin","user"]分割的

如何将列表的值设置为[0->"admin",1->"user"]

上述结果的大小为1,但想要的大小是2。

英文:

How to get elements from the below list:

  1. List&lt;String&gt; list = new ArrayList&lt;&gt;();
  2. List&lt;String&gt; getRoles =
  3. buildRolesList(list, role).stream()
  4. .map(String::trim)
  5. .map(String::toLowerCase)
  6. .map(str-&gt;str.replace ( &quot;[&quot;,&quot;&quot; ))
  7. .map(str-&gt;str.replace ( &quot;]&quot;,&quot;&quot; ))
  8. .collect( Collectors.toCollection ( ArrayList::new ) );

getRoles = &quot;[admin, user]&quot; ---this is the list I am getting, but the list wanted is [&quot;admin&quot;,&quot;user&quot;] splitted

How to achieve the list values as [0---&gt;&quot;admin&quot;,1---&gt;&quot;user&quot;]

The above result is coming in size 1 but wanted it to be size 2

答案1

得分: 1

  1. 假设你有一个类似于 `&quot;[admin, user]&quot;` 的字符串列表然后你做得差不多了只是忘记将字符串拆分为单独的角色
  2. ```java
  3. public static void main(String... args) {
  4. List&lt;String&gt; list = List.of(&quot;[admin, user]&quot;,
  5. &quot;[owner, developer]&quot;);
  6. Set&lt;String&gt; roles = getRoles(list);
  7. System.out.println(roles); // [owner, admin, developer, user]
  8. }
  9. public static Set&lt;String&gt; getRoles(List&lt;String&gt; list) {
  10. return list.stream()
  11. .map(String::toLowerCase)
  12. .map(Foo::getRoles)
  13. .flatMap(Set::stream)
  14. .collect(Collectors.toSet());
  15. }
  16. private static Set&lt;String&gt; getRoles(String str) {
  17. return Stream.of(str)
  18. .map(s -&gt; s.replace(&quot;[&quot;, &quot;&quot;))
  19. .map(s -&gt; s.replace(&quot;]&quot;, &quot;&quot;))
  20. .map(s -&gt; s.split(&quot;\\s*,\\s*&quot;))
  21. .flatMap(Arrays::stream)
  22. .collect(Collectors.toSet());
  23. }
英文:

Suppose you have a list of strings like &quot;[admin, user]&quot;. Then you do it almost right, just forget to split a string to individual roles.

  1. public static void main(String... args) {
  2. List&lt;String&gt; list = List.of(&quot;[admin, user]&quot;,
  3. &quot;[owner, developer]&quot;);
  4. Set&lt;String&gt; roles = getRoles(list);
  5. System.out.println(roles); // [owner, admin, developer, user]
  6. }
  7. public static Set&lt;String&gt; getRoles(List&lt;String&gt; list) {
  8. return list.stream()
  9. .map(String::toLowerCase)
  10. .map(Foo::getRoles)
  11. .flatMap(Set::stream)
  12. .collect(Collectors.toSet());
  13. }
  14. private static Set&lt;String&gt; getRoles(String str) {
  15. return Stream.of(str)
  16. .map(s -&gt; s.replace(&quot;[&quot;, &quot;&quot;))
  17. .map(s -&gt; s.replace(&quot;]&quot;, &quot;&quot;))
  18. .map(s -&gt; s.split(&quot;\\s*,\\s*&quot;))
  19. .flatMap(Arrays::stream)
  20. .collect(Collectors.toSet());
  21. }

答案2

得分: 0

  1. 将一个列表值拆分为元素的方法
  2. 看起来你有一对角色的列表,并且想把它们拆分成大小为2的独立列表。`List.toString()` 所以你能否返回一个所有角色的 `List<String>`,然后将它们分成一对。以下是如何做到的。

List<List> roles = List.of("admin", "user", "steno", "typist"));

List<List> roleLists = new ArrayList<>();

if (roles.size() % 2 != 0) {
System.out.println("Roles must be in pairs");
} else {
for (int i = 0; i < roles.size(); i += 2) {
roleLists.add(new ArrayList<>(List.of(roles.get(i), roles.get(i + 1))));
}
}

  1. 然后,当你得到列表的列表时,你可以按以下方式检索值:

for (List lst : roles) {
System.out.println("List -> " + lst);
for (int i = 0; i < lst.size(); i++) {
System.out.printf(" %d -> %s%n", i, lst.get(i));
}
}

  1. 输出

List -> [admin, user]
0 -> admin
1 -> user
List -> [steno, typist]
0 -> steno
1 -> typist

  1. 否则,只需执行以下操作:

String s = "[admin, user]";

String[] result = s.replaceAll("[\[\]\s+]+","").split(",");
for (int i = 0; i < result.length; i++) {
System.out.printf("%d -> %s%n", i, result[i]);
}

  1. 输出

0 -> admin
1 -> user

  1. <details>
  2. <summary>英文:</summary>
  3. &gt;how to split one list value to elements
  4. It looks like you have a list of roles in pairs and want to break that up into separate lists of size 2. `List.toString()` So can&#39;t you instead return a `List&lt;String&gt;` of all the roles and just copy in pairs. Here is how it would work.

List<List<String>> roles = List.of("admin", "user",
"steno","typist"));

List<List<String>> roleLists = new ArrayList<>();

if (roles.size()%2 != 0) {
System.out.println("Roles must be in pairs");
} else {
for (int i = 0; i < roles.size(); i+=2) {
roleLists.add(new ArrayList<>(List.of(roles.get(i),roles.get(i+1))));
}
}

  1. Then when you get the list of lists you can retrieve the values as follows:

for(List<String> lst : roles) {
System.out.println("List -> " + lst);
for(int i = 0; i < lst.size(); i++) {
System.out.printf(" %d -> %s%n", i, lst.get(i));
}
}

  1. prints

List -> [admin, user]
0 -> admin
1 -> user
List -> [steno, typist]
0 -> steno
1 -> typist

  1. Otherwise, just do the following:

String s = "[admin, user]";

String[] result = s.replaceAll("[\[\]\s+]+","").split(",");
for (int i = 0; i < result.length; i++) {
System.out.printf("%d -> %s%n", i, result[i]);
}

  1. prints

0 -> admin
1 -> user

huangapple
  • 本文由 发表于 2023年5月18日 03:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275529.html
匿名

发表评论

匿名网友

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

确定