Java 的还是定义类型比较好, JsonNode 看起来不直观
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import lombok.Data;
import lombok.NonNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
@
Data static class TreeNode < T extends TreeNode > {
private List < T > children;
}
@
Data static class Label extends TreeNode < Label > {
private String id;
private String label;
}
@
Data static class OrderNameTreeNode extends TreeNode < OrderNameTreeNode > {
private String id;
private String type;
private String name;
}
@
Data static class Student {
private String name;
private double score;
}
@
Data static class Type {
private String type = "";
private List < Type > typeArgs = new ArrayList < > ();
}
@
FunctionalInterface public interface Recursion < T > {
void go(T t, Recursion < T > self);
}
private static Label question1(@NonNull Label root, @
NonNull String id) {
if (id.equals(root.getId())) {
return root;
} else if (null != root.getChildren()) {
for (Label child: root.getChildren()) {
Label tartget = question1(child, id);
if (tartget != null) {
return tartget;
}
}
}
return null;
}
private static Map < String, List < Student >> question2(@NonNull List < Student > students) {
return students
.stream()
.collect(Collectors.groupingBy(student - >
student.getScore() < 60 ? "C" : student.getScore() < 80 ? "B" : "A"));
}
private static Type question3(@NonNull String typeArgs) {
List < Type > types = new ArrayList < > ();
Type root = new Type();
types.add(root);
for (int i = 0, len = typeArgs.length(); i < len; i++) {
char c = typeArgs.charAt(i);
if (c == '<') {
Type type = new Type();
types.get(types.size() - 1).getTypeArgs().add(type);
types.add(type);
} else if (c == ',') {
types.remove(types.size() - 1);
Type type = new Type();
types.get(types.size() - 1).getTypeArgs().add(type);
types.add(type);
} else if (c == ' ') {
continue;
} else if (c == '>') {
types.remove(types.size() - 1);
} else {
Type type = types.get(types.size() - 1);
type.setType(type.getType() + c);
}
}
return root;
}
private static String question4(@NonNull String srcName, @
NonNull OrderNameTreeNode root) {
List < Integer > existsIndexs = new ArrayList < > ();
Recursion < OrderNameTreeNode > func = (treeNode, self) - > {
if (treeNode.getName().startsWith(srcName)) {
String substring = treeNode.getName().substring(srcName.length());
if ("".equals(substring)) {
existsIndexs.add(0);
} else {
existsIndexs.add(Integer.parseInt(substring.substring(1)));
}
}
if (null != treeNode.getChildren()) {
treeNode.getChildren().forEach(child - > self.go(child, self));
}
};
func.go(root, func);
if (existsIndexs.isEmpty()) {
return srcName;
}
existsIndexs.sort(Comparator.comparingInt(o - > o));
if (existsIndexs.get(0) != 0) {
return srcName;
}
for (int i = 1; i < existsIndexs.size(); i++) {
if (existsIndexs.get(i) - existsIndexs.get(i - 1) > 1) {
return String.format("%s_%d", srcName, i + 1);
}
}
return String.format("%s_%d", srcName, existsIndexs.size());
}
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String treeNodeJson =
"{\"id\":\"1\",\"label\":\"first\",\"children\":[{\"id\":\"2\",\"label\":\"second\"},{\"id\":\"3\",\"label\":\"third\",\"children\":[{\"id\":\"4\",\"label\":\"fourth\"},{\"id\":\"5\",\"label\":\"fifth\"}]}]}";
Label treeNode = mapper.readValue(treeNodeJson, Label.class);
for (int i = 1; i < 5; i++) {
System.out.println(question1(treeNode, String.valueOf(i)));
}
String studentJson =
"[{\"name\":\"张三\",\"score\":84},{\"name\":\"李李四\",\"score\":58},{\"name\":\"王五\",\"score\":99},{\"name\":\"赵六\",\"score\":69}]";
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, Student.class);
List < Student > students = mapper.readValue(studentJson, collectionType);
System.out.printf(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question2(students)));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question3("int")));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question3("bool")));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question3("Array<int>")));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question3("Array<Array<int>>")));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question3(
"Array<Array<Array<int>>>")));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question3(
"Map<string, Map<string, bool>>")));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(question3(
"Map<Map<string, bool>, bool>")));
String orderNameTreeNodeJson =
"{\"id\":\"1\",\"type\":\"View\",\"name\":\"view\",\"children\":[{\"id\":\"2\",\"type\":\"Button\",\"name\":\"button\"},{\"id\":\"3\",\"type\":\"View\",\"name\":\"view_1\",\"children\":[{\"id\":\"4\",\"type\":\"Button\",\"name\":\"button_1\"},{\"id\":\"5\",\"type\":\"View\",\"name\":\"view_2\"}]}]}";
OrderNameTreeNode orderNameTreeNode = mapper.readValue(orderNameTreeNodeJson, OrderNameTreeNode.class);
System.out.println(question4("view", orderNameTreeNode));
System.out.println(question4("button", orderNameTreeNode));
}
}