You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在List中存储多类型变量?如何用字符串关联XSSFSheet?

Answers to Your Java Collection Questions

Q1: Can I store two different types of variables in a List collection?

Absolutely, but there are important caveats and better practices to follow:

  • If you use a raw List (like List list = new ArrayList();), you can technically store any object type—say, a String alongside an Integer. However, this skips all compile-time type checks. When retrieving elements, you’ll have to cast them explicitly, which risks ClassCastException and makes your code harder to maintain. This approach is not recommended for production code.
  • For a safer, type-aware approach, consider these options:
    • Use a common parent class as the generic type. For example, if your two types are subclasses of Number, use List<Number>. If they share no common parent besides Object, this is still better than a raw List since it makes the intentional type flexibility explicit.
    • Create a custom wrapper class to hold both types. For example:
      class SheetDataWrapper {
          private String identifier;
          private XSSFSheet sheet;
          // Add constructors, getters, and setters as needed
      }
      
      Then use List<SheetDataWrapper> to store your data—this keeps everything type-safe and self-documenting.
    • If you’re using Java 8+, you could use List<?>, but this is a read-only structure (you can only add null to it) and is only useful for specific, limited scenarios.

In short: it’s technically possible, but always prioritize type-safe solutions to avoid bugs and improve code readability.


Q2: How to associate XSSFSheet objects with strings, and retrieve them directly as XSSFSheet (not Object)?

It sounds like you tried using a List for this (which doesn’t support key-value pairs) and ended up with incorrect generic syntax. The right tool for this job is a generic Map—it’s designed explicitly for key-value associations, and it will give you type-safe access to your XSSFSheet objects without casting.

Here’s a straightforward implementation:

// Initialize the map with String keys and XSSFSheet values
Map<String, XSSFSheet> sheetMap = new HashMap<>();

// Store sheets with their string IDs as keys
XSSFSheet userSheet = workbook.createSheet("User Data");
sheetMap.put("user_sheet_101", userSheet);

XSSFSheet productSheet = workbook.createSheet("Product Inventory");
sheetMap.put("product_sheet_202", productSheet);

// Retrieve a sheet by ID—no casting needed, returns XSSFSheet directly
XSSFSheet targetSheet = sheetMap.get("user_sheet_101");

Why this works better than a List:

  • It’s fully type-safe: the get() method returns exactly XSSFSheet, no Object type headaches.
  • Map provides O(1) lookup time, which is way faster than iterating through a List to find a matching element.
  • It’s idiomatic Java—Map is the standard collection for key-value mapping, so other developers will immediately understand your code’s intent.

内容的提问来源于stack exchange,提问作者saka978

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:53:56