import java.util.ArrayList; import java.util.HashMap; public class Genealogy { private HashMap<String, String> genealogyMap; public Genealogy() { genealogyMap = new HashMap<String, String>(); genealogyMap.put("祖先", "无"); genealogyMap.put("子孙一", "祖先"); genealogyMap.put("子孙二", "祖先"); genealogyMap.put("子孙三", "子孙一"); genealogyMap.put("子孙四", "子孙一"); genealogyMap.put("子孙五", "子孙三"); genealogyMap.put("子孙六", "子孙三"); genealogyMap.put("子孙七", "子孙三"); genealogyMap.put("子孙八", "子孙四"); genealogyMap.put("子孙九", "子孙四"); } public void addPerson(String name, String parentName) { genealogyMap.put(name, parentName); } public void deletePerson(String name) { genealogyMap.remove(name); } public void updateParent(String name, String newParentName) { genealogyMap.put(name, newParentName); } public String getParent(String name) { return genealogyMap.get(name); } public ArrayList<String> getChildren(String name) { ArrayList<String> children = new ArrayList<String>(); for (String childName : genealogyMap.keySet()) { if (genealogyMap.get(childName).equals(name)) { children.add(childName); } } return children; } }为这段Java代码添加一个母亲节点
时间: 2024-03-14 17:45:16 浏览: 63
可以通过调用addPerson方法来添加一个母亲节点,代码如下:
```
public static void main(String[] args) {
Genealogy genealogy = new Genealogy();
genealogy.addPerson("母亲", "祖先"); // 添加母亲节点
System.out.println(genealogy.getChildren("祖先")); // 获取祖先节点的所有子孙
}
```
相关问题
第一段代码 GroupsResource package ece448.lec16; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GroupsResource { private final GroupsModel groups; public GroupsResource(GroupsModel groups) { this.groups = groups; } @GetMapping("/api/groups") public Collection<Object> getGroups() throws Exception { ArrayList<Object> ret = new ArrayList<>(); for (String group: groups.getGroups()) { ret.add(makeGroup(group)); } logger.info("Groups: {}", ret); return ret; } @GetMapping("/api/groups/{group}") public Object getGroup( @PathVariable("group") String group, @RequestParam(value = "action", required = false) String action) { if (action == null) { Object ret = makeGroup(group); logger.info("Group {}: {}", group, ret); return ret; } // modify code below to control plugs by publishing messages to MQTT broker List<String> members = groups.getGroupMembers(group); logger.info("Group {}: action {}, {}", group, action, members); return null; } @PostMapping("/api/groups/{group}") public void createGroup( @PathVariable("group") String group, @RequestBody List<String> members) { groups.setGroupMembers(group, members); logger.info("Group {}: created {}", group, members); } @DeleteMapping("/api/groups/{group}") public void removeGroup( @PathVariable("group") String group) { groups.removeGroup(group); logger.info("Group {}: removed", group); } protected Object makeGroup(String group) { // modify code below to include plug states HashMap<String, Object> ret = new HashMap<>(); ret.put("name", group); ret.put("members", groups.getGroupMembers(group)); return ret; } private static final Logger logger = LoggerFactory.getLogger(GroupsResource.class); }
第二段代码 GroupsModel package ece448.lec16;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GroupsModel {
private final Map<String, List<String>> groups;
public GroupsModel() {
this.groups = new HashMap<>();
}
public List<String> getGroups() {
return new ArrayList<>(groups.keySet());
}
public List<String> getGroupMembers(String group) {
return new ArrayList<>(groups.getOrDefault(group, new ArrayList<>()));
}
public void setGroupMembers(String group, List<String> members) {
groups.put(group, new ArrayList<>(members));
}
public void removeGroup(String group) {
groups.remove(group);
}
}
ArrayList<HashMap<String, String>> tagList转为json
可以使用Gson库将ArrayList<HashMap<String, String>>转换为JSON格式。以下是一个示例代码:
```java
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// 创建一个 ArrayList<HashMap<String, String>> 对象
ArrayList<HashMap<String, String>> tagList = new ArrayList<>();
HashMap<String, String> tag1 = new HashMap<>();
tag1.put("name", "tag1");
tag1.put("value", "value1");
tagList.add(tag1);
HashMap<String, String> tag2 = new HashMap<>();
tag2.put("name", "tag2");
tag2.put("value", "value2");
tagList.add(tag2);
// 将 ArrayList<HashMap<String, String>> 转换为 JSON 格式
Gson gson = new Gson();
String json = gson.toJson(tagList);
// 输出 JSON 字符串
System.out.println(json);
}
}
```
输出结果为:
```
[{"name":"tag1","value":"value1"},{"name":"tag2","value":"value2"}]
```
阅读全文