java – skill Lập trình cho agent AI
Hướng dẫn viết mã chuyên biệt cho ngôn ngữ Java.
Cài skill java bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/java-a7489.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /java – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/java-a7489.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $java – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/java-a7489.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /java – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/java-a7489.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /java – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Java: Idiomatic Efficiency Reference
Apply only within the requested scope and current project conventions. Preserve behavior, ownership, error handling, accessibility and security; examples are independent fragments, not equivalent rewrites in every context. Verify language/library versions and run relevant checks. Required tests and user-facing progress/results take precedence over brevity rules. Do not install dependencies or delete files merely to follow these examples.
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for java.
Table of Contents
1. [Streams & Collections](#streams)
2. [Optional](#optional)
3. [Records & Data Classes](#records)
4. [Switch Expressions](#switch)
5. [Concurrency](#concurrency)
6. [Error Handling](#errors)
7. [Anti-patterns specific to Java](#antipatterns)
---
1. Streams & Collections {#streams}
// ❌ Imperative accumulation
List<String> result = new ArrayList<>();
for (Item item : items) {
if (item.isActive()) result.add(item.getName().toUpperCase());
}
// ✅
List<String> result = items.stream()
.filter(Item::isActive)
.map(item -> item.getName().toUpperCase())
.toList(); // Java 16+; use .collect(Collectors.toList()) before// ❌ Manual grouping
Map<String, List<Item>> grouped = new HashMap<>();
for (Item item : items) {
grouped.computeIfAbsent(item.getCategory(), k -> new ArrayList<>()).add(item);
}
// ✅
Map<String, List<Item>> grouped = items.stream()
.collect(Collectors.groupingBy(Item::getCategory));// ❌ Manual sum
int total = 0;
for (Order o : orders) total += o.getAmount();
// ✅
int total = orders.stream().mapToInt(Order::getAmount).sum();Prefer method references (Item::isActive) over equivalent lambdas (item -> item.isActive()).
---
2. Optional {#optional}
// ❌ Null check chain
String city = null;
if (user != null && user.getAddress() != null) {
city = user.getAddress().getCity();
}
// ✅
String city = Optional.ofNullable(user)
.map(User::getAddress)
.map(Address::getCity)
.orElse(null);// ❌ Optional.get() without isPresent()
String name = optional.get(); // throws if empty
// ✅
String name = optional.orElse("default");
// or: optional.orElseThrow(() -> new IllegalStateException("name required"));// ❌ Optional as a field or parameter (anti-pattern)
class User { private Optional<String> nickname; }
// ✅ — Optional is for return types only
class User { private String nickname; } // nullable field
public Optional<String> getNickname() { return Optional.ofNullable(nickname); }---
3. Records & Data Classes {#records}
// ❌ Manual POJO
class Point {
private final int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
public int getX() { return x; }
public int getY() { return y; }
// + equals, hashCode, toString...
}
// ✅ (Java 16+)
record Point(int x, int y) {}// ❌ Builder pattern for a 2-field object
User user = new User.Builder().name("Alice").age(30).build();
// ✅ — use record or constructor directly for small objects
record User(String name, int age) {}
var user = new User("Alice", 30);Use record for any immutable data carrier. Keep builders only for objects with many optional fields.
---
4. Switch Expressions {#switch}
// ❌ Switch statement with fall-through and break
String label;
switch (status) {
case ACTIVE: label = "Active"; break;
case INACTIVE: label = "Inactive"; break;
default: label = "Unknown";
}
// ✅ (Java 14+)
String label = switch (status) {
case ACTIVE -> "Active";
case INACTIVE -> "Inactive";
default -> "Unknown";
};// ❌ instanceof + cast
if (shape instanceof Circle) {
Circle c = (Circle) shape;
return c.radius() * c.radius() * Math.PI;
}
// ✅ Pattern matching (Java 16+)
if (shape instanceof Circle c) {
return c.radius() * c.radius() * Math.PI;
}---
5. Concurrency {#concurrency}
// ❌ Raw Thread creation
Thread t = new Thread(() -> doWork());
t.start();
// ✅
try (ExecutorService exec = Executors.newVirtualThreadPerTaskExecutor()) { // Java 21
exec.submit(() -> doWork()).get(); // observe failure; declare/handle checked exceptions
}// ❌ synchronized on this for fine-grained state
synchronized(this) { counter++; }
// ✅
AtomicInteger counter = new AtomicInteger();
counter.incrementAndGet();Prefer CompletableFuture.allOf() over blocking .get() chains for parallel async work.
---
6. Error Handling {#errors}
// ❌ Catching Exception to log and swallow
try {
risky();
} catch (Exception e) {
log.error("error", e);
}
// ✅ — rethrow as unchecked if you can't handle it
try {
risky();
} catch (IOException e) {
throw new UncheckedIOException(e);
}// ❌ Checked exceptions declared on every method
public void process() throws IOException, SQLException, ParseException { ... }
// ✅ — wrap at the boundary; internal methods throw unchecked---
7. Anti-patterns specific to Java {#antipatterns}
| Anti-pattern | Preferred |
|---|---|
| new ArrayList<String>() (Java 7+) | new ArrayList<>() (diamond) |
| "string".equals(variable) (Yoda) | Objects.equals(variable, "string") |
| for (int i = 0; i < list.size(); i++) | enhanced for or stream |
| StringBuffer in single-threaded code | StringBuilder |
| e.printStackTrace() | log.error("msg", e) |
| null return for "not found" | Optional<T> return type |
| Public fields | private + accessor, or record |
| Mutable static fields | avoid; use dependency injection |
| instanceof + cast without pattern matching | pattern matching (Java 16+) |
Limitations
- These are language-specific guidelines and do not cover overall architectural decisions.
- Over-compression might reduce readability; apply judgement.
Skill liên quan
- java-pro – Thành thạo Java 21+ với các tính năng hiện đại như virtual threads, pattern matching và…
- javascript-mastery – 33+ khái niệm JavaScript thiết yếu mà mọi lập trình viên nên biết, lấy cảm hứng từ…
- javascript-pro – Thành thạo JavaScript hiện đại với ES6+, các mẫu async và API Node.js. Xử lý promise…
- javascript-testing-patterns – Hướng dẫn toàn diện triển khai chiến lược kiểm thử vững chắc cho ứng dụng…
- javascript-typescript-typescript-scaffold – Khởi tạo khung dự án TypeScript với công cụ build, kiểm tra kiểu, kiểm thử và cấu hình.
- jest-skill – Tạo test unit và integration với Jest trong JavaScript hoặc TypeScript. Bao gồm mocking…
- testrail – Đồng bộ test với TestRail. Dùng khi người dùng nhắc đến "testrail", "test management"…
- js-reverse – Dịch ngược JavaScript phía front-end: xác định chuỗi chữ ký, phân tích tham số request…