英文:
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<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]"
---this is the list I am getting, but the list wanted is ["admin","user"]
splitted
How to achieve the list values as [0--->"admin",1--->"user"]
The above result is coming in size 1 but wanted it to be size 2
答案1
得分: 1
假设你有一个类似于 `"[admin, user]"` 的字符串列表。然后你做得差不多了,只是忘记将字符串拆分为单独的角色。
```java
public static void main(String... args) {
List<String> list = List.of("[admin, user]",
"[owner, developer]");
Set<String> roles = getRoles(list);
System.out.println(roles); // [owner, admin, developer, user]
}
public static Set<String> getRoles(List<String> list) {
return list.stream()
.map(String::toLowerCase)
.map(Foo::getRoles)
.flatMap(Set::stream)
.collect(Collectors.toSet());
}
private static Set<String> getRoles(String str) {
return Stream.of(str)
.map(s -> s.replace("[", ""))
.map(s -> s.replace("]", ""))
.map(s -> s.split("\\s*,\\s*"))
.flatMap(Arrays::stream)
.collect(Collectors.toSet());
}
英文:
Suppose you have a list of strings like "[admin, user]"
. Then you do it almost right, just forget to split a string to individual roles.
public static void main(String... args) {
List<String> list = List.of("[admin, user]",
"[owner, developer]");
Set<String> roles = getRoles(list);
System.out.println(roles); // [owner, admin, developer, user]
}
public static Set<String> getRoles(List<String> list) {
return list.stream()
.map(String::toLowerCase)
.map(Foo::getRoles)
.flatMap(Set::stream)
.collect(Collectors.toSet());
}
private static Set<String> getRoles(String str) {
return Stream.of(str)
.map(s -> s.replace("[", ""))
.map(s -> s.replace("]", ""))
.map(s -> s.split("\\s*,\\s*"))
.flatMap(Arrays::stream)
.collect(Collectors.toSet());
}
答案2
得分: 0
将一个列表值拆分为元素的方法
看起来你有一对角色的列表,并且想把它们拆分成大小为2的独立列表。`List.toString()` 所以你能否返回一个所有角色的 `List<String>`,然后将它们分成一对。以下是如何做到的。
List<List
List<List
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
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>
>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't you instead return a `List<String>` 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论