首先设计note类封装标题、内容、分类和创建时间,再通过notemanager使用map按分类存储笔记列表,实现增删查及分类展示功能,结构清晰且易扩展。

在Java中实现个人笔记分类管理,核心是设计合理的类结构和组织数据的方式。重点在于将“笔记”和“分类”两个概念建模,并提供增删改查操作。下面是一个简单实用的实现思路。
定义笔记类(Note)
每个笔记应包含基本属性如标题、内容、创建时间以及所属分类。
- 使用String存储标题和内容
- 用LocalDateTime记录创建时间
- 添加一个字段表示分类名称(如category)
示例代码:
<font face="Courier New" size="2" color="#000080">public class Note {
private String title;
private String content;
private String category;
private LocalDateTime createTime;
public Note(String title, String content, String category) {
this.title = title;
this.content = content;
this.category = category;
this.createTime = LocalDateTime.now();
}
// Getter和Setter方法
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
public LocalDateTime getCreateTime() { return createTime; }
@Override
public String toString() {
return "Note{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", category='" + category + '\'' +
", createTime=" + createTime +
'}';
}
}</font>
设计分类管理类(NoteManager)
这个类负责管理所有笔记,支持按分类组织和操作。
你接手一个需求时,是不是经常这样:"改一个小字段 → 结果把不相关的代码也改了"、"需求说得很模糊 → 做着做着发现跑偏了"、"数据库加个字段 → 上线才发现漏了数据兼容"、"做了个新功能 → 一个月后自己都忘了当时怎么设计的"、"写到一半被中断 → 再回来不知道从哪继续";这个 Skill 就是专门解决这些问题的。 它会自动探测你的项目环境(JDK、Spring Boot、ORM、数据库、前端框架),记在
- 使用Map
>以分类名为键存储笔记列表 - 提供添加、删除、查询、按分类查看等方法
示例代码:
<font face="Courier New" size="2" color="#000080">import java.util.*;
public class NoteManager {
private Map<string list>> notesByCategory;
public NoteManager() {
notesByCategory = new HashMap();
}
public void addNote(Note note) {
String category = note.getCategory();
notesByCategory.computeIfAbsent(category, k -> new ArrayList()).add(note);
}
public void removeNote(String title, String category) {
List<note> notes = notesByCategory.get(category);
if (notes != null) {
notes.removeIf(note -> note.getTitle().equals(title));
if (notes.isEmpty()) {
notesByCategory.remove(category); // 分类为空时清理
}
}
}
public List<note> getNotesByCategory(String category) {
return notesByCategory.getOrDefault(category, new ArrayList());
}
public void displayAllCategories() {
System.out.println("当前分类:");
notesByCategory.keySet().forEach(System.out::println);
}
public void displayNotesInCategory(String category) {
List<note> notes = getNotesByCategory(category);
System.out.println("【" + category + "】类下的笔记:");
notes.forEach(System.out::println);
}
}</note></note></note></string></font>
使用示例
通过主程序测试功能,模拟添加不同分类的笔记并查询。
<font face="Courier New" size="2" color="#000080">public class Main {
public static void main(String[] args) {
NoteManager manager = new NoteManager();
// 添加笔记
manager.addNote(new Note("Java基础", "学习变量与类型", "编程"));
manager.addNote(new Note("健身计划", "每周三次跑步", "生活"));
manager.addNote(new Note("Spring框架", "IoC和AOP理解", "编程"));
// 查看所有分类
manager.displayAllCategories();
// 查看某一分类下的笔记
manager.displayNotesInCategory("编程");
// 删除一条笔记
manager.removeNote("健身计划", "生活");
}
}</font>
基本上就这些。这个结构清晰、易于扩展。如果需要更复杂功能,比如持久化到文件或数据库、支持子分类、搜索关键字等,可以在现有基础上逐步添加。关键是先把模型理清楚,再考虑功能延伸。不复杂但容易忽略的是分类的动态维护和空值处理。
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










