我知道你可以创建一个匿名函数,让编译器推断它的返回类型:
val x = () => { System.currentTimeMillis }
Run Code Online (Sandbox Code Playgroud)
仅为了静态类型,是否可以指定其返回类型?我认为这会让事情变得更加清晰.
我需要从Java对象层次结构构建XML文档.Java类和XML格式都是固定的.所以我不能使用像XStream这样的XML序列化器:它将XML格式基于Java类.同样,像JAXB这样的Java XML绑定技术也不起作用,因为它从XML模式创建Java类[编辑:但见下文].我需要一个手动方法.
低技术的StringBuilder路由导致脆弱和错误的代码(至少对我而言!).
像JAXP或JDOM这样的API 可以提供更强大的代码,但这些代码非常冗长.
Groovy有一个优雅的MarkupBuilder:
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.records() {
car(name:'HSV Maloo', make:'Holden', year:2006) {
country('Australia')
record(type:'speed', 'Production Pickup Truck with speed of 271kph')
}
car(name:'P50', make:'Peel', year:1962) {
country('Isle of Man')
record(type:'size', 'Smallest Street-Legal Car at 99cm wide and 59 kg')
}
}
Run Code Online (Sandbox Code Playgroud)
其他语言(例如Ruby)甚至更好,但我希望保留纯Java.似乎有一些新的Java构建器用于Java,例如practicalxml和James Murty的xmlbuilder.
在Java中构建XML文档有哪些更优雅的方法?
摘要:
无论如何,CurtainDog推荐使用JAXB,并且jherico告诉我这是一个相关的建议:然后你可以使用Dozer在我当前的JavaBeans和JAXB …
我正在尝试单元测试(JUnit)我创建的DAO.我使用Spring作为我的框架,我的DAO(JdbcPackageDAO)扩展了SimpleJdbcDaoSupport.测试类(JdbcPackageDAOTest)扩展了AbstractTransactionalDataSourceSpringContextTests.我已经覆盖了configLocations,如下所示:
protected String[] getConfigLocations(){
return new String[] {"classpath:company/dc/test-context.xml"};
}
Run Code Online (Sandbox Code Playgroud)
我的test-context.xml文件定义如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataPackageDao" class="company.data.dao.JdbcPackageDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>company/data/dao/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我使用HSQL作为我的后端,它以独立模式运行.我选择的IDE是eclipse.当我作为JUnit测试运行类时,这是我的错误(下面).我不知道为什么会发生这种情况.根据Eclipse,hsql.jar在我的构建路径上.
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: No suitable driver found for jdbc:hsqldb:hsql://localhost
at … 在我使用System.Threading.TimerWindows服务项目之前,我正在做一个小型测试项目.它工作得非常好,但计时器会在一两分钟后自行停止.
测试项目的完整来源是:
using System;
using System.Windows.Forms;
using System.Threading;
namespace studyTimers {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
TimerCallback timerDelegate = new TimerCallback(tick);
System.Threading.Timer testTimer = new System.Threading.Timer(timerDelegate, null, 1000, 1000);
}
void tick(Object obj) {
if (label1.InvokeRequired) {
label1.Invoke(new MethodInvoker(() => tick(obj)));
} else {
label1.Text = DateTime.Now.ToString();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然,目标是用当前时间更新标签.我注意到稍后更新停止了.为什么会这样?
经过剖析后,我发现这种方法占用了大部分计算时间.我真的没有看到优化的方法,因为它是一个可怕的功能.(它是......)也许有人能给我一些好主意吗?
public static double perceivedLoudness(double L_G, double L_ETQ, double a0) {
double t1 = 1d + 1 / 4d * Math.pow(10d, 0.1d * (L_G - a0 - L_ETQ));
double t2 = Math.pow(t1, 0.25);
return 0.064d * Math.pow(10, 0.025 * L_ETQ) * (t2 - 1);
}
Run Code Online (Sandbox Code Playgroud)
这是改进版本:
public static double perceivedLoudness(double L_G, double L_ETQ, double a0) {
double x = L_G - a0 - L_ETQ;
double t1 = 0.25 * Math.exp(0.230259 * x) + 1;
double t2 = Math.sqrt(Math.sqrt(t1));
return ltqFactors[(int)L_ETQ] …Run Code Online (Sandbox Code Playgroud) 我有一个无状态会话bean,它包含一个公共方法,几个私有方法和一些实例级变量.下面是一个伪代码示例.
private int instanceLevelVar
public void methodA(int x) {
this.instanceLevelVar = x;
methodB();
}
private void methodB() {
System.out.println(instanceLevelVar);
}
Run Code Online (Sandbox Code Playgroud)
我所看到的是,methodB正在打印未传递给MethodA的值.最好我可以告诉它从同一个bean的其他实例打印值.什么会导致这个?
我应该指出代码在99.9%的时间内按预期工作.但是,.01%对我来说是一个严重的问题/担忧.
我明白,如果我有不同的公共方法,那么我可能不会在调用之间获得相同的bean,这会导致这种行为.但是,在这种情况下,唯一的调用是单个公共方法.容器(在这种情况下是Glassfish)是否仍会在私有方法调用之间交换bean?
(编辑)我将"类级别"重命名为"实例级别",因为这引起了一些混乱.
下面我有一个Person接口,一个实现类和一个驱动程序类,它使用一个名称初始化Person并再次输出它.使用的优点是什么
Person person = new PersonImpl();
Run Code Online (Sandbox Code Playgroud)
代替
PersonImpl person = new PersonImpl();
Run Code Online (Sandbox Code Playgroud)
该接口应该隐藏实现?这是使用接口的正确方法吗?
public class Driver {
public static void main(String [] args)
{
Person person = new PersonImpl();
person.setName("test name");
System.out.println("Name is "+person.getName());
}
}
public interface Person {
public void setName(String name);
public String getName();
}
public class PersonImpl implements Person{
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个列表迭代器,它通过一个列表并删除所有偶数.我可以使用list迭代器来打印出数字,但是我不能使用list的remove()并传入dereferenced迭代器.
我注意到当remove()语句生效时,*itr被破坏了吗?有人可以解释一下吗?
#include <iostream>
#include <list>
#define MAX 100
using namespace std;
int main()
{
list<int> listA;
list<int>::iterator itr;
//create list of 0 to 100
for(int i=0; i<=MAX; i++)
listA.push_back(i);
//remove even numbers
for(itr = listA.begin(); itr != listA.end(); ++itr)
{
if ( *itr % 2 == 0 )
{
cout << *itr << endl;
listA.remove(*itr); //comment this line out and it will print properly
}
}
}
Run Code Online (Sandbox Code Playgroud) 我使用LinkedHashMap了accessOrdertrue,随时允许最多500个条目作为数据的LRU缓存.但由于可扩展性问题,我想转向一些线程安全的替代方案.ConcurrentHashMap在这方面似乎很好,但缺乏的特点accessOrder和removeEldestEntry(Map.Entry e)发现LinkedHashMap.任何人都可以指向某些链接或帮助我简化实施.
java performance multithreading concurrenthashmap linkedhashmap