Java代码优化咨询:合并属性列表与嵌入方法逻辑
嗨,作为Java初学者能写出功能代码已经很棒了!针对你提出的两个核心问题,还有一些代码简化的实践,我整理了以下建议:
Java代码简化与优化建议
1. 用实体类替代多个独立List(比HashMap更优雅)
你现在用多个平行List存储房产属性,不仅容易出现索引对应错误,代码可读性也很差。不推荐用HashMap,因为HashMap更适合键值对不固定的场景,而你的房产属性是固定的(ID、地址、业主信息等),自定义一个Property实体类是更合适的选择——类型安全、结构清晰,还能彻底解决冗长拼接的问题:
// 定义房产实体类,封装所有相关属性 class Property { private String id; private String propertyAddress; private String firstName; private String lastName; private String email; private String ownerAddress; private String price; // 建议改成BigDecimal,金额用String易出格式/计算问题 private String dateSold; // 全参构造方法 public Property(String id, String propertyAddress, String firstName, String lastName, String email, String ownerAddress, String price, String dateSold) { this.id = id; this.propertyAddress = propertyAddress; this.firstName = firstName; this.lastName = lastName; this.email = email; this.ownerAddress = ownerAddress; this.price = price; this.dateSold = dateSold; } // 生成需要用到的Getter方法(可以用IDEA/Lombok自动生成) public String getId() { return id; } public String getPropertyAddress() { return propertyAddress; } public String getPrice() { return price; } // ... 其他属性的Getter // 重写toString方法,一键生成标准格式的属性字符串 @Override public String toString() { return String.join(", ", id, propertyAddress, firstName, lastName, email, ownerAddress, price, dateSold); } }
然后把原来的多个List替换成一个统一的列表:
private static List<Property> properties = new ArrayList<>();
这样你的getPropertybyId方法能大幅简化,再也不用拼接一堆get(n)了:
public static String getPropertyById() { // 用try-with-resources自动关闭Scanner,避免资源泄漏 try (Scanner reader = new Scanner(System.in)) { System.out.println("Enter an id number to search properties: "); int index = reader.nextInt(); // 先判断索引合法性,避免数组越界异常 if (index < 0 || index >= properties.size()) { System.out.println("Invalid index!"); return ""; } Property property = properties.get(index); System.out.println(property); // 直接调用toString打印所有信息 return property.toString().replace(", ", " ,"); // 匹配你原来的返回格式 } }
注意:如果你的
id不是列表索引,而是房产的唯一标识(比如字符串ID),那需要遍历properties列表找到匹配ID的对象,而不是直接用get(n)。
2. 将shrinkTo逻辑嵌入getTopProperties方法
你只需要把shrinkTo的代码直接整合到getTopProperties里,去掉外部方法调用即可。同时还能简化循环逻辑,优化资源管理:
public static void getTopProperties() { price.remove(0); // 把while循环简化为更易读的for循环 for (int n = 0; n < price.size(); n++) { price.set(n, price.get(n).substring(1)); } Collections.sort(price); try (Scanner reader = new Scanner(System.in)) { System.out.println("Enter a number for the list size of top properties: "); int userInput = reader.nextInt(); // 直接嵌入shrinkTo的逻辑,无需外部方法 int currentSize = price.size(); if (userInput < currentSize) { // 从末尾移除元素,直到达到目标大小 while (price.size() > userInput) { price.remove(price.size() - 1); } } System.out.println(price); } }
提示:如果用了上面的
Property实体类,这里的price列表可以改成从properties中提取并处理,更符合面向对象思想:List<String> prices = properties.stream() .map(Property::getPrice) .map(p -> p.substring(1)) // 去掉价格前缀,比如$符号 .sorted() .collect(Collectors.toList());
3. 其他值得注意的优化细节
- 资源管理:始终用
try-with-resources处理Scanner(或其他实现AutoCloseable的类),避免手动关闭遗漏导致资源泄漏。 - 类型安全:价格字段建议用
BigDecimal替代String,金额涉及比较、计算时,String容易出现格式错误,也不方便排序。 - 异常处理:当前代码未处理用户输入非数字的情况,会抛出
InputMismatchException,可以添加try-catch块捕获异常,提升程序健壮性。 - 命名规范:Java方法和变量名遵循小驼峰规则,比如
getPropertybyId改成getPropertyById,property_address改成propertyAddress。
内容的提问来源于stack exchange,提问作者Kabira Suleman
相关产品推荐
相关产品推荐

