String类有一些方法,我无法理解为什么它们是这样实现的... replace是其中之一.
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
Run Code Online (Sandbox Code Playgroud)
与更简单,更有效(快速!)的方法相比,是否有一些明显的优势?
public static String replace(String string, String searchFor, String replaceWith) {
StringBuilder result=new StringBuilder();
int index=0;
int beginIndex=0;
while((index=string.indexOf(searchFor, index))!=-1){
result.append(string.substring(beginIndex, index)+replaceWith);
index+=searchFor.length();
beginIndex=index;
}
result.append(string.substring(beginIndex, string.length()));
return result.toString();
}
Run Code Online (Sandbox Code Playgroud)
使用Java 7的统计数据:
1,000,000次迭代
将"b"替换为"abc"
结果中的"x" :"axc"
时间:
string.replace:485ms
string.replaceAll:490ms
optimize replace = 180ms
像Java 7拆分方法这样的代码经过大量优化,以尽可能避免模式编译/正则表达式处理:
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is …
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法从枚举元素创建数组:
public static enum LOGICAL {
AND ("&", "AND"),
OR ("||", "OR");
public final String symbol;
public final String label;
LOGICAL(String symbol, String label) {
this.symbol=symbol;
this.label=label;
}
}
public static final String[] LOGICAL_NAMES = new String[LOGICAL.values().length];
static{
for(int i=0; i<LOGICAL.values().length; i++)
LOGICAL_NAMES[i]=LOGICAL.values()[i].symbol;
}
public static final String[] LOGICAL_LABELS = new String[LOGICAL.values().length];
static{
for(int i=0; i<LOGICAL.values().length; i++)
LOGICAL_LABELS[i]=LOGICAL.values()[i].label;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Eclipse HELIOS来编写Rich Client应用程序.以下条目会自动添加到我的APP"PERSPECTIVES MENU":"Java,Java浏览,Java类型层次结构,团队同步".我需要摆脱他们.
我正在使用扩展点="org.eclipse.ui.activities"来禁用不需要的扩展(例如冷却栏上显示的编辑器和搜索选项)
我设法通过添加"org.eclipse.debug.ui.*"摆脱"调试".
这是我的实际配置:
<extension point="org.eclipse.ui.activities">
<activity id="rcpcolibri.disablextensions" name="Hidden activities"/>
<activityPatternBinding activityId="rcpcolibri.disablextensions" pattern="org.eclipse.debug.ui.*"/>
<activityPatternBinding activityId="rcpcolibri.disablextensions" pattern="org.eclipse.search.*"/>
<activityPatternBinding activityId="rcpcolibri.disablextensions" pattern="org.eclipse.ui.editors.*"/>
<activityPatternBinding activityId="rcpcolibri.disablextensions" pattern="org.eclipse.ui.externaltools.*"/>
</extension>
Run Code Online (Sandbox Code Playgroud)
我应该禁用哪些扩展名?
还有另一种方法可以解决这个问题吗?
Eclipse应该以另一种方式工作:我们应该添加我们需要的东西,而不是让eclipse抛出一切......
我正在使用Eclipse RCP,但主要是因为我完全控制了UI(删除了所有的贡献,从头开始制作了首选项等)我只是不能接受所包含的UPDATE MANAGER的复杂性和要求(另外,我使用PLUGINS而不是功能,应用程序插件必须提取 - 虽然我可以得到最后一期的问题).
无论如何,在第一种方法,我只想检查是否有更新版本的应用程序可用.
逻辑方法是检查服务器上的文件(xml?).
那里有一个很好的图书馆和例子吗?
谢谢.
我需要在ISO 8601中获取SS毫秒和HH:MM时区的当前日期
完成日期加上小时,分钟,秒和小数秒:YYYY-MM-DDThh:mm:ss.sTZD(例如1997-07-16T19:20:30.45 + 01:00)
请参阅:http://www.w3.org/TR/NOTE-datetime
我尝试了不同的方法,但我无法得到正确的毫秒和时区数字:
DateFormat df=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSZZ");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
df.format(new Date());
Run Code Online (Sandbox Code Playgroud)
JAVA结果: 2012-11-24T21:19:27.758 + 0000
Apache结果: 2012-11-24T21:19:27.758 + 00:00(Apache Commons FastDateFormat)
我有一个广泛的DATE-TIME转换类,但我遇到了一个我无法解决的场景:
我有一个java.util.date:Tue May 10 00:00:00 BST 2011
我有一个java.sql.time:03:58:44
我需要创建一个java.util.date:Tue May 10 03:58:44 BST 2011
我想出的唯一方法是:
public static Date getDate(Date date, Time time) {
Calendar calendar=Calendar.getInstance();
calendar.set(date.getYear(), date.getMonth(), date.getDay(), time.getHours(), time.getMinutes(), time.getSeconds());
return calendar.getTime();
}
Run Code Online (Sandbox Code Playgroud)
完全弃用的代码,不起作用:java.sql.Time.getYear(Unknown Source)中的java.lang.IllegalArgumentException
有任何想法吗?
我刚刚测试了H2数据存储区(使用Datanucleus 2.x)
表现非常缓慢.Postgres的1/3和MySQL的1/10(aprox.)
我对"规格"非常满意,但我不能在"实际使用"中看到它们. http://www.h2database.com/html/performance.html
我可能会缺少任何性能调整吗?
编辑:
H2不是很慢.是迄今为止我测试过的最快的RDBMS之一!
我需要帮助解释为什么我需要使用WEB MANAGER(H2控制台)连接到数据库以获得这个数据库引擎的奇妙速度......我在通过H2 WEB控制台"etvoilá"连接时意外测试了我的应用程序,问题解决了.
为什么?
我在Eclipse上有很多Java项目,我使用不同的SVN存储库.
当项目是共享时,我想知道它使用哪个SVN存储库.(附在项目名称前面?......)
有一个简单的方法吗?
注意:我使用"Package Explorer"视图.
SWT不支持Button中的"Text over Image".设置图像后,隐藏文本.有什么可能是实现这一目标的最佳方法?
我能想到的最好的解决方法是将文本与图像"合并"......有没有人知道一个好的Java库对此做了什么?
注意:我发现这个自定义实现只是在图像上绘制文本:https://github.com/jrobichaux/SquareButton/wiki.为什么在SWT中没有实现?(无论如何,我不能让这个班级工作......我真的不想要这种方法因为我想要NATIVE按钮看起来)
我需要一种简单的方法来contains
使用matches
. 我相信这是我的出发点:
xxx.matches("'.*yyy.*'");
Run Code Online (Sandbox Code Playgroud)
但是我需要使它成为一种通用方法,并对我搜索的任何内容进行预处理以被匹配项接受!这必须仅使用转义字符 '\' 来完成!
想象一个字符串SEARCH_FOR
,它可以包含一些必须“正则表达式转义”的特殊字符......
String SEARCH_FOR="*.\\"
xxx.matches("'.*" + SEARCH_FOR + ".*'");
Run Code Online (Sandbox Code Playgroud)
有什么问题吗?特殊情况?应该考虑任何其他“特殊字符?