输出结果
{{"name":"zhangsan"}:[{"code":"edit"}],{"name":"root"}:[{"$ref":"$.User2\\(name\\=zhangsan\\)[0]"},{"code":"admin"}]}
{"User2(name=zhangsan)":[{"code":"edit"}],"User2(name=root)":[{"code":"edit"},{"code":"admin"}]}
复现过程
版本信息
- jdk8
- fastjson 1.2.58
- streamex 0.6.5
主要代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User2 {
String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Role {
private String code;
}
@Test
public void mapOperations() throws JsonProcessingException {
Map<Role, List<User2>> role2users = new HashMap<>();
User2 root = new User2("root");
role2users.put(new Role("admin"), Lists.newArrayList(root));
role2users.put(new Role("edit"), Lists.newArrayList(root, new User2("zhangsan")));
Map<User2, List<Role>> users2roles = transformMap(role2users);
//Json对象转为String字符串
System.out.println(objectMapper.writeValueAsString(users2roles));
System.out.println(JSON.toJSONString(users2roles));
}
public Map<User2, List<Role>> transformMap(Map<Role, List<User2>> role2users) {
Map<User2, List<Role>> users2roles = EntryStream.of(role2users)
.flatMapValues(List::stream)
.invert()
.grouping();
return users2roles;
}
暂无答案!
目前还没有任何答案,快来回答吧!