wangbot1 最近的时间轴更新
wangbot1

wangbot1

V2EX 第 464900 号会员,加入于 2020-01-14 09:10:45 +08:00
wangbot1 最近回复了
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());
ㅤ......}

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

}
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1525 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 56ms · UTC 17:01 · PVG 01:01 · LAX 09:01 · JFK 12:01
Developed with CodeLauncher
♥ Do have faith in what you're doing.