我经常使用apache HashCodeBuilder和EqualsBuilder使用反射来实现对象相等,但最近我的一位同事告诉我,如果实体包含许多属性,使用反射可能会导致巨大的性能损失.担心我可能使用了错误的实现,我的问题是,您更喜欢以下哪种方法?为什么?
public class Admin {
private Long id;
private String userName;
public String getUserName() {
return userName;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Admin)) {
return false;
}
Admin otherAdmin = (Admin) o;
EqualsBuilder builder = new EqualsBuilder();
builder.append(getUserName(), otherAdmin.getUserName());
return builder.isEquals();
}
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(getUserName());
return builder.hashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
比.
public class Admin {
private Long id;
private String userName;
public String getUserName() {
return userName; …Run Code Online (Sandbox Code Playgroud) 我正在寻找的是使用的通用版本Object[] java.util.Collection.toArray()或更简洁的替代品T[] java.util.Collection.toArray(T[] array).我现在可以写:
Collection<String> strings;
String[] array = strings.toArray(new String[strings.size()]);
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是:
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> collection, Class<T> clazz) {
return collection.toArray((T[]) Array.newInstance(clazz, collection.size()));
}
Run Code Online (Sandbox Code Playgroud)
我可以用它作为:
String[] array = Util.toArray(strings, String.class);
Run Code Online (Sandbox Code Playgroud)
那么在Guava或Commons Collections中实现的是什么?
当然我可以编写自己的(上面的),这似乎和theArray(T []数组一样快).
我找不到任何解释为什么从Apache Lang3 v3.7弃用StringEscapeUtils的原因.
我们现在应该使用什么来进行HTML转义/转义
java apache-commons apache-commons-lang apache-commons-lang3
使用Java 11,我可以初始化InputStream为:
InputStream inputStream = InputStream.nullInputStream();
Run Code Online (Sandbox Code Playgroud)
但我无法理解
ie 的潜在用例InputStream.nullInputStream或类似的API .OutputStreamOutputStream.nullOutputStream
从API Javadocs,我可以弄清楚它
返回一个新的
InputStream即读取任何字节.返回的流最初是打开的.通过调用close()方法关闭流.后续调用
close()无效.虽然流是开放的,available(),read(),read(byte[]),...skip(long),和transferTo()方法的行为都好像流的末尾已到达.
我进一步详细说明了这些说明:
有很多次我想使用需要作为目标OutputStream/Writer的参数来发送输出的方法,但是想要为其他效果静默执行这些方法.
这相当于Unix将命令输出重定向到/ dev/null,或者在DOS中将命令输出附加到NUL的能力.
然而,我无法理解陈述中声明的那些方法是什么...... 为了其他效果而默默地执行这些方法.(归咎于我没有动手使用API)
有人可以帮助我理解在可能的情况下借助示例获得这样的输入或输出流有什么用处吗?
编辑:我在进一步浏览时可以找到的类似实现之一是apache-commons'NullInputStream,它更好地证明了测试用例的合理性.
我刚刚开发了自己的小型JDBC帮助程序库,我已经意识到它将成为一个维护噩梦.
我不是在寻找一个完整的ORM,比如Hibernate,只是一些简单而有用的东西可以快速地使JDBC调用传递SQL语句而不会弄乱已检查的异常,手动关闭资源等等......
从您的经验中,您能推荐一个不错的JDBC帮助程序库吗?
到目前为止,我已经看过Apache Commons DbUtils了,看起来还不错.还有其他想法吗?
示例A(使用org.apache.commons.codec.binary.Base64):
Base64.encodeBase64("foobar".getBytes());
Run Code Online (Sandbox Code Playgroud)
示例B(使用android.util.Base64):
Base64.encode("foobar".getBytes(), Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
这些产生相同的字符串吗?
使用Apache Commons CSV库解析CSV文件时出现以下错误.
Exception in thread "main" java.io.IOException: (line 2) invalid char between encapsulated token and delimiter
at org.apache.commons.csv.Lexer.parseEncapsulatedToken(Lexer.java:275)
at org.apache.commons.csv.Lexer.nextToken(Lexer.java:152)
at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:450)
at org.apache.commons.csv.CSVParser.getRecords(CSVParser.java:327)
at parse.csv.file.CSVFileParser.main(CSVFileParser.java:29)
Run Code Online (Sandbox Code Playgroud)
这个错误是什么意思?
我在这里结束了我的智慧.我确信这很简单,我很可能在理解java和流时遇到很大漏洞.我认为有这么多课程,我试图通过API来弄清楚我何时以及如何使用大量的输入/输出流,我有点不知所措.
我刚刚了解了apache commons库的存在(自学java失败),我正在尝试将我的一些Runtime.getRuntime().exec转换为使用commons - exec.已经修复了一些每6个月一次这个问题,然后消除了exec的风格问题.
代码执行perl脚本,并在GUI运行时从GUI中显示stdout.
调用代码在swingworker内部.
我迷路了如何使用pumpStreamHandler ...无论如何这里是旧代码:
String pl_cmd = "perl script.pl"
Process p_pl = Runtime.getRuntime().exec( pl_cmd );
BufferedReader br_pl = new BufferedReader( new InputStreamReader( p_pl.getInputStream() ) );
stdout = br_pl.readLine();
while ( stdout != null )
{
output.displayln( stdout );
stdout = br_pl.readLine();
}
Run Code Online (Sandbox Code Playgroud)
我想这是我得到的复制粘贴代码,我很久以前还不完全理解.上面我假设正在执行该过程,然后抓取输出流(通过"getInputStream"?),将其放入缓冲读取器,然后将循环,直到缓冲区为空.
我没有得到的是为什么这里不需要'waitfor'样式命令?是否可能有一段时间缓冲区将为空,退出循环,并在进程仍在进行时继续?当我运行它时,情况似乎并非如此.
无论如何,我试图使用commons exec获得相同的行为,基本上再次从谷歌找到的代码:
DefaultExecuteResultHandler rh = new DefaultExecuteResultHandler();
ExecuteWatchdog wd = new ExecuteWatchdog( ExecuteWatchdog.INFINITE_TIMEOUT );
Executor exec = new DefaultExecutor();
ByteArrayOutputStream out = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler( out ); …Run Code Online (Sandbox Code Playgroud) 我即将迁移某些遗留代码含有较少的过时的警告,第三次三方库.对于Apache commons-cli库(版本:1.3.1),我在官方JavaDoc中检测到GnuParser已弃用,DefaultParser应该使用:
@deprecated自1.3以来,使用
{@link DefaultParser}而不是
但是,以下代码段停止按预期方式工作:
Options options = new Options();
Option optionGSTypes = new Option(
"gst","gs-types", true,
"the supported types, comma-separated: article, category, template, all");
optionGSTypes.setArgs(3);
optionGSTypes.setValueSeparator(',');
options.addOption(optionGSTypes);
// ... other options
// parsed option values are correct, yet this is deprecated
CommandLineParser parser = new GnuParser();
CommandLine commands = parser.parse(options, args);
// ... interpret parsed 'commands' and related actual values via CLI
Run Code Online (Sandbox Code Playgroud)
请注意,setValueSeparator(',')此处用于定义自定义分隔符char , …
java command-line-interface apache-commons apache-commons-cli
决定使用Apache的Common Configuration包来解析XML文件.
我决定做一个:
XMLConfiguration xmlConfig = new XMLConfiguration(file);
Run Code Online (Sandbox Code Playgroud)
Eclipse抱怨我没有捕获异常(Unhandled exception type ConfigurationException),所以我点击了可靠性surround with try/catch并添加了以下代码:
try
{
XMLConfiguration xmlConfig = new XMLConfiguration(file);
}
catch (ConfigurationException ex)
{
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
然而现在它抱怨:
No exception of type ConfigurationException can be thrown; an exception type
must be a subclass of Throwable
Run Code Online (Sandbox Code Playgroud)
我不明白为什么当Eclipse是建议添加它时,它给了我这个错误.
apache-commons ×10
java ×10
android ×1
base64 ×1
guava ×1
helper ×1
hibernate ×1
inputstream ×1
java-11 ×1
jdbc ×1
outputstream ×1