我刚刚阅读了Java 7预览演示文稿(pdf),链接调用上有一张幻灯片.以下是幻灯片中使用的示例:
// Construction with setters
DrinkBuilder margarita = new DrinkBuilder();
margarita.add("tequila");
margarita.add("orange liqueur");
margarita.add("lime juice");
margarita.withRocks();
margarita.withSalt();
Drink drink = margarita.drink();
// Construction with chained invocation
Drink margarita = new DrinkBuilder()
.add("tequila")
.add("orange liqueur")
.add("lime juice")
.withRocks()
.withSalt()
.drink();
Run Code Online (Sandbox Code Playgroud)
我对此感到复杂.不应该将太多的方法调用链接到一个语句中.在另一方面,写作margarita.this()和margarita.that()不是太方便无论是.
现在,我从Delphi世界来到Java.在Delphi中有with语言结构.这是少数人所珍惜和许多人的厌恶(或者是相反的方式?).我觉得with比链式调用的想法更优雅(我认为它基于void方法返回对它被调用的对象的引用 - 这是我不喜欢的部分,因为void应该什么都不返回).
我很欣赏withJava采用的语言功能,因此示例代码可以像这样编写:
Drink margarita = null;
with (new DrinkBuilder()) {
add("tequila");
add("orange liqueur");
add("lime juice");
withRocks();
withSalt(); …Run Code Online (Sandbox Code Playgroud) 我今天已经了解到你可以使用静态方法创建一个新的ArrayList对象,如下所示:
List<String> listDummy = Arrays.asList("Coding", "is", "fun");
ArrayList<String> stringList = new ArrayList<>(listDummy);
Run Code Online (Sandbox Code Playgroud)
或者更简洁:
ArrayList<String> stringList = new ArrayList<>(Arrays.asList("Coding", "is", "fun"));
Run Code Online (Sandbox Code Playgroud)
我的问题是:与"传统"方式相比,这种表现有多贵?(下面)
ArrayList<String> stringList = new ArrayList<>();
stringList.add("Coding");
stringList.add("is");
stringList.add("fun");
Run Code Online (Sandbox Code Playgroud)
我意识到创建一个ArrayList的上层方法包括一个额外的List对象创建,但是,我更喜欢更短更紧凑的语法,以至于我愿意牺牲一些性能,但必须在某处画线.
PS.在"new ArrayList <>()"中保留类型信息(<>)为空是Java SE 7的功能,而不是错误.
先谢谢你的回答!
这是代码:
timer.schedule(new TimerTask()
{
public void run()
{
synchronized(this)
{
try
{
// System.out.println(" ITERATION = ");
pachubeCLI.update(78164);
}
catch (PachubeException e)
{
// If an exception occurs it will print the error message from the
// failed HTTP command
System.err.println(e.errorMessage);
}
catch (IOException e)
{
System.err.println(e);
}
}
}
}, 0, 5*1000);
Run Code Online (Sandbox Code Playgroud)
我可以说,代码基本上用于使用Timer类的对象来安排操作.schedule根据eclipse 传递给方法的参数是(TimerTask task,long delay, long period).但是看一下这段代码,整个代码块作为第一个参数而不是对TimerTask类的引用传递.我以前从未见过这样的方法.到底发生了什么?
一些背景:该对象的schedule方法Timer用于定期更新Xively(以前的COSM(以前的pachube))上的Feed.
此外,我不知道哪个标签描述了这里发生的事情.如果你这样做,请添加或发表评论.
我认为这会相对容易,但唉,似乎不是.
我目前正在使用Java EE 6在我的项目中为类似Facade的结构编写单元测试.
对于测试,我使用Junit 4.11,Eclipse Kepler作为IDE.
从我所看到的情况来看,双支撑初始化似乎有些"错误",但我不知道如何能够解释为什么它不起作用,因为我认为应该这样做.
为了达到目的,我使用以下Class在集中的位置进行转换:
package com.example-company.util.converters;
import java.util.HashMap;
import java.util.Map;
import com.example-company.model.Location;
import com.example-company.model.Right;
public final class ModelConverters {
private static final Map<Class<?>, ModelConverter<?, String>> modelConverterBacking = new HashMap<Class<?>, ModelConverter<?, String>>();
static {
modelConverterBacking.put(Right.class, new RightConverter());
modelConverterBacking.put(Location.class, new LocationConverter());
};
public static <T> String convert(final T input)
throws IllegalStateException {
@SuppressWarnings("unchecked")
ModelConverter<T, String> modelConverter = (ModelConverter<T, String>) modelConverterBacking
.get(input.getClass());
if (modelConverter == null) {
throw new IllegalStateException("No mapping found for "
+ input.getClass()); …Run Code Online (Sandbox Code Playgroud) 将2D数组字符串转换为HashMap的最简单方法是什么?
例如,拿这个:
final String[][] sheetMap = { /* XSD Name, XSL Sheet Name */
{"FileHeader", "FileHeader"},
{"AccountRecord", "AccountRecord"},
{"DriverCardRecord", "DriverCardRecord"},
{"AssetCardRecord", "AssetCardRecord"},
{"SiteCardRecord", "SiteCardRecord"}
};
这很可能是从文件加载而且会更大.
潜在的愚蠢:假设我有一个包含运算符的字符串,应用此运算符的最佳方法是什么?
我倾向于做的是:
if(n.getString(1).equals("<<")) {
result = tmp1 << tmp2;
}
Run Code Online (Sandbox Code Playgroud)
对于我所拥有的每种运营商.有没有更好的办法 ?
我最近发现在我看来是静态初始化ArrayList的新语法:
new ArrayList() {{
add("first");
add("second");
}};
我的问题是,那里到底发生了什么?这是定义静态块的快捷方式(我认为它需要static关键字)?或者只是一种定义默认构造函数的方法?别的什么?哪个版本的Java有效?
将非常感谢解释以及进一步阅读的链接.
编辑:我的测试类,用于显示初始化程序块是否在构造函数之前或之后执行.结果显示初始化程序块在其他构造函数代码之前执行:
import org.junit.Test;
public class InitializerBlockTest {
class InitializerTest {
{
System.out.println("Running initalizer block");
}
public InitializerTest() {
System.out.println("Running default constructor");
}
}
class SubClass extends InitializerTest {
{
System.out.println("Running subclass Initializer block");
}
public SubClass() {
System.out.println("Running subclass constructor");
}
}
@Test
public void testIt() {
new SubClass();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Running initalizer block
Running default constructor
Running subclass Initializer block
Running subclass constructor
Run Code Online (Sandbox Code Playgroud) 我倾向于在GWT中使用(甚至过度使用)双括号对象初始化.对我来说,它看起来更具可读性和更具声明性.
new FastMap<Object>(){{
put("Value", 12);
put("Unit", "Kg");
}};
Run Code Online (Sandbox Code Playgroud)
在今天之前我不知道这个语法不只是实例化对象,而是为它创建AnonymousInnerClass.现在我担心GWT如何处理它们.
我的应用程序中有数千个这样的初始化.
我有一个jersey客户端,它正在从需要获取格式正确的JSON的来源获取JSON:
通过http请求抓取时,我的JSON字符串如下所示:
{
"properties": [
{
someproperty: "aproperty",
set of data: {
keyA: "SomeValueA",
keyB: "SomeValueB",
keyC: "SomeValueC"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我遇到问题是因为json必须正确格式化,并且keyA,keB和keyC不在引号内。是否有一些有助于添加引号的库或将此字符串转换为格式正确的json的最佳方法?或者是否有一些简单的方法可以将其转换为json对象,而无需编写一堆具有与传入结构匹配的变量和列表的类?
通常,如果我事先知道地图的所有键,我会像这样实例化它:
List<String> someKeyList = getSomeList();
Map<String, Object> someMap = new HashMap<String, Object>(someKeyList.size());
for (String key : someKeyList) {
someMap.put(key, null);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法直接这样做而不需要遍历列表?有效的东西:
new HashMap<String, Object>(someKeyList)
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是直接编辑地图的键集,但不支持该操作.还有其他方式我可以俯瞰吗?