杰瑞科技汇

Preference Java,如何高效实现?

Of course. When discussing "preference" in the context of Java, it usually refers to two main things:

Preference Java,如何高效实现?-图1
(图片来源网络,侵删)
  1. Java Preferences API: A standard API for storing and retrieving user and system configuration data.
  2. Best Practices and Coding Conventions: The preferred way to write Java code for clarity, maintainability, and performance.

Let's break down both.


The Java Preferences API

This is a built-in Java utility for managing persistent data that is structured as key-value pairs. It's designed to be a simple, platform-independent way to store configuration settings, user preferences, and small amounts of application state.

What is it for?

  • Storing user settings (e.g., window size, theme, default font).
  • Remembering recent files or actions.
  • Caching application state to restore it on the next launch.
  • Storing license keys or feature flags.

How it Works

The API is centered around the Preferences class. You get a specific Preferences node for your application, and then you store and retrieve data from it using simple put(), get(), getBoolean(), getInt(), etc., methods.

The data is stored in a backing store that is implementation-dependent:

Preference Java,如何高效实现?-图2
(图片来源网络,侵删)
  • Windows: The Windows Registry.
  • Linux/macOS: A set of files in the user's home directory (~/.java/.userPrefs).
  • Other Platforms: Could be a database or another mechanism.

Simple Example

Let's say you want to save the user's name and whether they want notifications enabled.

import java.util.prefs.Preferences;
public class PreferenceExample {
    public static void main(String[] args) {
        // 1. Get a preferences node for this class.
        //    The path is hierarchical, like a file system.
        Preferences prefs = Preferences.userNodeForPackage(PreferenceExample.class);
        // 2. Store some preferences
        prefs.put("userName", "Alice");
        prefs.putBoolean("enableNotifications", true);
        prefs.putInt("windowWidth", 1024);
        System.out.println("Preferences have been saved.");
        // 3. Retrieve the preferences
        String name = prefs.get("userName", "Guest"); // "Guest" is the default value
        boolean notificationsEnabled = prefs.getBoolean("enableNotifications", false);
        int width = prefs.getInt("windowWidth", 800);
        System.out.println("Retrieved User Name: " + name);
        System.out.println("Notifications Enabled: " + notificationsEnabled);
        System.out.println("Window Width: " + width);
        // 4. You can also remove a preference
        prefs.remove("userName");
        System.out.println("Removed 'userName'. Retrieving it again: " + prefs.get("userName", "Guest"));
    }
}

Key Takeaways for the API:

  • User vs. System: Use Preferences.userNodeForPackage(...) for per-user settings and Preferences.systemNodeForPackage(...) for settings shared by all users on the system.
  • Defaults: Always provide a sensible default value when retrieving data (e.g., prefs.get("key", "default_value")).
  • Not for Large Data: This API is for small, simple data. For large datasets, use a database or a file.
  • Simplicity: It's incredibly easy to use and requires no complex setup.

Best Practices and Coding Conventions (The "Preferred" Way to Code)

This is about the style and idioms that the Java community has adopted to write high-quality code. Adhering to these is a strong indicator of a professional Java developer.

A. Naming Conventions (The "Java Way")

  • Classes and Interfaces: PascalCase (e.g., CustomerService, List).
  • Methods and Variables: camelCase (e.g., calculateTotal(), customerName).
  • Constants (static final fields): SCREAMING_SNAKE_CASE (e.g., MAXIMUM_RETRY_COUNT).
  • Packages: lowercase (e.g., com.example.myapp).
  • Abbreviations: Avoid them unless they are extremely common (e.g., URL, HTML, IO). XmlHttpRequest is preferred over XMLHTTPRequest.

B. Core Language Preferences

  • var Keyword (Java 10+):

    • Preferred for: Local variables where the type is obvious from the initializer. It reduces boilerplate and improves readability.
    • Example: var list = new ArrayList<String>(); is cleaner than ArrayList<String> list = new ArrayList<String>();.
    • Avoid for: When the type on the right side is not clear or is overly complex. var stream = ... is less clear than InputStream stream = ....
  • StringBuilder vs. String Concatenation ():

    • Preferred for: Building strings in a loop or from multiple parts. StringBuilder is much more performant because it modifies the object in memory, whereas creates a new String object in each iteration.

    • Example:

      // Preferred way
      StringBuilder sb = new StringBuilder();
      for (String item : items) {
          sb.append(item).append(", ");
      }
      String result = sb.toString();
      // Avoid in loops
      String result = "";
      for (String item : items) {
          result = result + item + ", "; // Creates many intermediate String objects
      }
  • List.of(), Set.of(), Map.of() (Java 9+):

    • Preferred for: Creating immutable collections. It's concise and the immutability is guaranteed at compile time.
    • Example: List<String> names = List.of("Alice", "Bob", "Charlie");
    • Avoid for: Lists that you need to modify later. Use the ArrayList constructor for that: new ArrayList<>(List.of(...)).
  • try-with-resources:

    • Preferred for: Any resource that needs to be closed (files, database connections, sockets). It automatically closes the resource for you, even if an exception occurs, preventing resource leaks.

    • Example:

      // Preferred way
      try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
          String line;
          while ((line = reader.readLine()) != null) {
              System.out.println(line);
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      // The 'reader' is automatically closed here.
      // Old, error-prone way
      // BufferedReader reader = null;
      // try {
      //     reader = new BufferedReader(...);
      //     ...
      // } finally {
      //     if (reader != null) {
      //         try { reader.close(); } catch (IOException e) { /* handle */ }
      //     }
      // }

C. Modern Java Features (Java 8+)

  • Streams and Lambda Expressions:

    • Preferred for: Processing collections in a declarative, functional style. It leads to more concise and often more readable code for data transformations and filtering.
    • Example:
      List<String> names = ...;
      long count = names.stream()
                        .filter(name -> name.startsWith("A"))
                        .map(String::toUpperCase) // Method reference
                        .count();
  • Optional<T>:

    • Preferred for: Representing optional values that may or may not be present. It helps eliminate NullPointerExceptions by forcing you to explicitly handle the "absent" case.

    • Example:

      Optional<String> optionalName = findUserName();
      // Preferred: provides a default value if empty
      String name = optionalName.orElse("Guest");
      // Or perform an action if present
      optionalName.ifPresent(n -> System.out.println("Hello, " + n));

Summary Table of Preferences

Category Preferred Method Why?
Storing User Settings java.util.prefs.Preferences Simple, platform-independent, built-in.
Local Variable Type var (when initializer is clear) Reduces boilerplate, improves readability.
Building Strings StringBuilder Performance: avoids creating many intermediate String objects.
Creating Immutable Collections List.of(), Set.of() Concise, type-safe, guarantees immutability.
Handling Resources try-with-resources Prevents resource leaks, code is cleaner and safer.
Processing Collections Streams (stream(), filter(), map()) Declarative, functional, concise, parallelizable.
Representing Absent Values Optional<T> Explicitly handles nullability, prevents NullPointerException.
Exception Handling Specific exceptions over Exception More precise error handling and easier debugging.
Class Design Composition over Inheritance More flexible, less fragile, promotes better design.
分享:
扫描分享到社交APP
上一篇
下一篇