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

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

how to split one list value to elements

问题

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

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

List<String> getRoles = 
buildRolesList(list, role).stream()
    .map(String::trim)
    .map(String::toLowerCase)
    .map(str->str.replace("[",""))
    .map(str->str.replace("]",""))
    .collect(Collectors.toCollection(ArrayList::new));

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

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

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

英文:

How to get elements from the below list:

List&lt;String&gt; list = new ArrayList&lt;&gt;();

List&lt;String&gt; getRoles = 
buildRolesList(list, role).stream()
    .map(String::trim)
    .map(String::toLowerCase)
    .map(str-&gt;str.replace ( &quot;[&quot;,&quot;&quot; ))
    .map(str-&gt;str.replace ( &quot;]&quot;,&quot;&quot; ))
    .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

假设你有一个类似于 `&quot;[admin, user]&quot;` 的字符串列表然后你做得差不多了只是忘记将字符串拆分为单独的角色

```java
public static void main(String... args) {
    List&lt;String&gt; list = List.of(&quot;[admin, user]&quot;,
                                &quot;[owner, developer]&quot;);

    Set&lt;String&gt; roles = getRoles(list);
    System.out.println(roles);  // [owner, admin, developer, user]
}

public static Set&lt;String&gt; getRoles(List&lt;String&gt; list) {
    return list.stream()
               .map(String::toLowerCase)
               .map(Foo::getRoles)
               .flatMap(Set::stream)
               .collect(Collectors.toSet());
}

private static Set&lt;String&gt; getRoles(String str) {
    return Stream.of(str)
                 .map(s -&gt; s.replace(&quot;[&quot;, &quot;&quot;))
                 .map(s -&gt; s.replace(&quot;]&quot;, &quot;&quot;))
                 .map(s -&gt; s.split(&quot;\\s*,\\s*&quot;))
                 .flatMap(Arrays::stream)
                 .collect(Collectors.toSet());
}
英文:

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.

public static void main(String... args) {
    List&lt;String&gt; list = List.of(&quot;[admin, user]&quot;,
                                &quot;[owner, developer]&quot;);

    Set&lt;String&gt; roles = getRoles(list);
    System.out.println(roles);  // [owner, admin, developer, user]
}

public static Set&lt;String&gt; getRoles(List&lt;String&gt; list) {
    return list.stream()
               .map(String::toLowerCase)
               .map(Foo::getRoles)
               .flatMap(Set::stream)
               .collect(Collectors.toSet());
}

private static Set&lt;String&gt; getRoles(String str) {
    return Stream.of(str)
                 .map(s -&gt; s.replace(&quot;[&quot;, &quot;&quot;))
                 .map(s -&gt; s.replace(&quot;]&quot;, &quot;&quot;))
                 .map(s -&gt; s.split(&quot;\\s*,\\s*&quot;))
                 .flatMap(Arrays::stream)
                 .collect(Collectors.toSet());
}

答案2

得分: 0

将一个列表值拆分为元素的方法
看起来你有一对角色的列表,并且想把它们拆分成大小为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))));
}
}

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

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));
}
}

输出

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

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

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]);
}

输出

0 -> admin
1 -> user


<details>
<summary>英文:</summary>
&gt;how to split one list value to elements
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))));
}
}

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));
}
}

prints

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

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]);
}

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:

确定