- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在生成一个 CSV 文件和 CTL 文件以供 sqlldr
使用。 CTL 文件需要知道我要加载的列的名称,而我的 CSV 文件需要知道这些字段的默认值。
/*
* Models a line in the CSV file
*/
public class CSVRecord {
...
}
/*
* Models the CTL file
*/
public class ControlFile {
...
}
这两个类在 CSVExportFile
中初始化和使用,我有两种方法:
public enum Columns {
ID("1"),
NAME("Bob"),
...
}
public class CSVExportFile {
private HashMap<String, String> columns;
public CSVExportFile() {
columns = new HashMap<String, String>();
columns.put("ID", "1");
columns.put("Name", "Bob");
...
}
}
HashMap
缩小了列的范围,意味着它们只能在 CSVExportFile
中使用。我不打算扩展此功能(所有类都是 final
),所以我不确定我的 enum
是否对我有任何帮助。
支持/反对每种方法的论点是什么,这是一个更优的特定案例,还是一种方法总是更优?
最佳答案
我总是会在这里使用 enum
,因为 enum
有一个天生的顺序,而 Map
没有。
通过使用 enum
,您可以从枚举本身生成 CTL 文件,并使用 enum
值作为工厂来填充您的 csv 文件。
class MyObj {
final String foreName;
final String surname;
public MyObj(String foreName, String surname) {
this.foreName = foreName;
this.surname = surname;
}
public String getForeName() {
return foreName;
}
public String getSurname() {
return surname;
}
}
enum Column {
Forename {
@Override
String fromMyObj(MyObj it) {
return it.getForeName();
}
},
Surname {
@Override
String fromMyObj(MyObj it) {
return it.getSurname();
}
},;
abstract String fromMyObj(MyObj it);
static String asSelectStatement(Set<Column> columns, String tableName) {
return join(columns, ",", "SELECT ", " FROM " + tableName);
}
static String asCSVHeader(Set<Column> columns) {
return join(columns, ",");
}
static String asCSV(Set<Column> columns, MyObj it) {
return join(columns, (Column a) -> a.fromMyObj(it), ",");
}
private static String join(Set<Column> columns, String between) {
return join(columns, new StringJoiner(between));
}
private static String join(Set<Column> columns, String between, String prefix, String suffix) {
return join(columns, new StringJoiner(between, prefix, suffix));
}
private static String join(Set<Column> columns, StringJoiner joined) {
return join(columns, (Column a) -> a.name(), joined);
}
private static String join(Set<Column> columns, Function<Column, String> as, String between) {
return join(columns, as, new StringJoiner(between));
}
private static String join(Set<Column> columns, Function<Column, String> as, String between, String prefix, String suffix) {
return join(columns, as, new StringJoiner(between, prefix, suffix));
}
private static String join(Set<Column> columns, Function<Column, String> as, StringJoiner joined) {
for (Column c : columns) {
joined.add(as.apply(c));
}
return joined.toString();
}
// Also simple to auto-populate prepared statements, build INSERT statements etc.
}
public void test() {
Set<Column> columns = EnumSet.of(Column.Forename, Column.Surname);
System.out.println("As Select: " + Column.asSelectStatement(columns, "MyTable"));
System.out.println("As CSV Header: " + Column.asCSVHeader(columns));
MyObj it = new MyObj("My Forename", "My Surname");
System.out.println("As CSV: " + Column.asCSV(columns, it));
}
关于用于静态值的 Java Enum 或 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31473613/
我不明白为什么这甚至可以编译。我尝试过不同的格式,它们似乎都有效。 为什么有一个 enum of enum of enum of.. 是合法的? interface I { enum E im
我有大型数据库(数百万行),我正在尝试为 2 个字段的数据类型做出最佳选择。我制作的大部分内容都是 varchar 或 INT。但是,我想知道 Enum 是否是最好的方法的 2 个字段。 字段 1第一
我是 C++ 新手,我想知道是否有人可以帮助我理解原因 enum difficulty { NOVICE, EASY, NORMAL, HARD, UNBEATABLE }; difficulty m
从 native 代码强制转换(在 C++/CLI 中)的正确方法是什么 enum到托管代码enum其中包含相同的 enum值(value)观?与使用 C# 的强制转换方式有什么区别,例如 (int)
我有以下枚举 [Flags] public enum WeekDays { Monday = 1, Tuesday = 2, Wednesday = 4, Thursd
我刚刚学习 Java 中的枚举。当我运行下面的代码时,我收到一个错误,我也在下面重现该错误。基本上,我的问题是:当我在枚举中定义一个方法,并且在该方法中我想检查枚举的值以便我可以根据该值执行某些操作时
我想要一些语法糖来打开 Enum .当然,一个if else块按预期工作: @enum Fruit apple=1 orange=2 kiwi=3 function talk1(fruit::Frui
我试图在 Enum.each 的函数内为变量设置一个值,但在循环结束时,变量为空,我不知道为什么会出现这种行为。 代码: base = "master" candidates = ["stream",
This question already has answers here: Is there a way to use existing structs as enum variants? (2个
我想让 Java 中的枚举器具有其他枚举作为属性。 public enum Direction { Up(Down), Down(Up), Left(Right), R
我有一个包含两种不同可能“类型”的枚举和一个可以返回其中任何一种的函数,封装在 Option 中: enum Possibilities { First(i32), Second(St
我刚刚注意到 pyhton 中 Enum+defaultdict 的一个非常奇怪的行为。我定义了一个这样的枚举,它收集了一些默认词典: from enum import Enum from colle
我想在运行时从配置文件生成一些类型。为简单起见,假设我已经将数据加载为 Python 字典: color_values = dict(RED = 1, YELLOW = 2, GREEN = 3) 我
我想创建一个方法,在传入参数的任何枚举类上实现 valueOf(并包装一些专门的错误/缺失名称代码)。所以基本上,我有几个枚举,例如: enum Enum1{ A, B, C } enum Enum2
我有一个user数据库表: CREATE TABLE IF NOT EXISTS `user` ( `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
这是我的 JPA @Repository,在这里我们可以得到 list与 findByFullNameContaining(String query) - 通过在查询中提供 fullName 的子字符
(也在这里打开了一个问题:https://github.com/square/moshi/issues/768 但被要求打开一个stackoverflow问题)我正在编写一个通用适配器来转换带有枚举值
请帮助找到以下情况的最佳方法: 有一个表,大约有 20 列。 每列都有自己的短名称、全名称和类型(数字或字符串)。 每个列类型都可以有自己的运算符 - 例如,字符串 - 包含、等于;数字 - 更多、更
如果我在 python 中按功能创建了 enum.Enum(本例中为 3.7),如何从中检索类的名称? import enum def print_the_enum_class_name(some_e
我正在编写一个与第 3 方程序交互的程序。这个第 3 方程序允许用户制作可以运行在第 3 方程序中进行的步骤记录的按钮。 但! 这些按钮还可以运行用户定义的批处理文件。因此,我使用此功能通过创建文件并
我是一名优秀的程序员,十分优秀!