如果两个String相同的s实际上不相同,那么为什么我可以在HashMap不使用相同String对象的情况下将字符串用作键?
String s1 = "Test";
String s2 = "Test";
System.out.println(s1 == s2); // should be false
System.out.println(s1.equals(s2)); // should be true
HashMap<String, String> map = new HashMap();
map.put(s1, "foo");
System.out.println(map.get(s2)); // should be "foo"--but why?
Run Code Online (Sandbox Code Playgroud)
是否HashMap有一些特殊行为String的对象?如果没有,为什么可以使用两个"不同"字符串来放置和获取哈希值?
我正在使用JSP编写一个网站.我希望网站有多种语言版本,所以我HashMap为我计划支持的每种语言创建了一个,并且通过map.get("identifier")其他一些代码来查找文本.
我遇到的问题是我之前通过使用format函数解决的问题(类似于printf许多语言),但这是另一种语言.
具体问题是,文本User performed action可能会出现Action was performed by user另一种语言(即术语可能会出现故障).
在过去,我做过类似的事情#translate("Welcome to the site, %s!", {"Username"}),然后使用语言的format功能替换%s用户名.我可以简单地使用String#replace但是我不能做#translate("Welcome to the site, %s! You last visited on %s!", {"username", "last visit"})像我想做的事情.
对不起,如果这是一个不好的解释 - 只需查看printfPHP之类的东西.
在Java中复制这样的东西的最佳方法是什么?谢谢您的帮助.
我正在使用H2数据库来测试我的Grails应用程序.我有一些简单的域类,如:
package mypackage
class UserSession {
User user
String sessionTokenHash
// last seen info
String lastSeenIP
Date lastSeenTime
String lastSeenUserAgent
String lastSeenURL
}
Run Code Online (Sandbox Code Playgroud)
但是,表似乎没有正确创建.
hbm2ddl.SchemaExport Unsuccessful: create table user_session (id bigint not null auto_increment, version bigint not null, last_seenip varchar(255) not null, last_seen_time datetime not null, last_seenurl varchar(255) not null, last_seen_user_agent varchar(255) not null, session_token_hash varchar(255) not null, user_id bigint not null, primary key (id)) ENGINE=InnoDB hbm2ddl.SchemaExport Syntax error in SQL statement "CREATE TABLE USER_SESSION (ID BIGINT NOT NULL AUTO_INCREMENT, …
我们有一个像“student/create”这样的 API 命令来创建一个新的学生对象。代码如下所示:
def student = new Student(firstName: firstName, lastName: lastName, email: email)
if (! student.validate()) {
response.error = "UNKNOWN_ERROR" // in case we can't find anything better
student.errors.allErrors.each { error ->
// set response.error to an appropriate value
println error
}
} else {
student.save()
}
Run Code Online (Sandbox Code Playgroud)
我们的目标是在验证失败时提供合理的错误消息,例如“EMAIL_DUPLICATE”或“FIRSTNAME_LENGTH”,因此我们希望针对一组预期错误测试我们收到的错误,以便我们可以这样响应。
这是我们从中得到的println:
字段“电子邮件”上的对象“com.example.Student”中出现字段错误:拒绝值 [student@example.com];代码 [com.example.Student.email.unique.error.com.example.Student.email,com.example.Student.email.unique.error.email,com.example.Student.email.unique.error.java。 lang.String,com.example.Student.email.unique.error,student.email.unique.error.com.example.Student.email,student.email.unique.error.email,student.email.unique.error。 java.lang.String,student.email.unique.error,com.example.Student.email.unique.com.example.Student.email,com.example.Student.email.unique.email,com.example.Student。 email.unique.java.lang.String,com.example.Student.email.unique,student.email.unique.com.example.Student.email,student.email.unique.email,student.email.unique.java。 lang.String,student.email.unique,unique.com.example.Student.email,unique.email,unique.java.lang.String,unique]; 参数 [电子邮件,类 com.example.Student,student@example.com.org];默认消息 [具有值 [{2}] 的类 [{1}] 的属性 [{0}] 必须是唯一的]
我怎样才能知道这意味着电子邮件已在数据库中使用,以便我可以告诉 API 用户这一点?
(需要明确的是,我想提供一条计算机可读的消息,例如“EMAIL_DUPLICATE”,而不是“具有值student@example.com的Student类的属性电子邮件必须是唯一的”)
我有一个依赖于几个库的Java项目,这些库作为JAR文件分发.当我构建我的项目时,我最终myProject.jar得到一个lib文件夹,其中包含我使用的每个库的JAR文件.
为了分发我的项目,我想我可以拉上整个dist文件夹,但这不是很干净.
而不是为包含库的每个平台(.app对于Mac,.exe对于Windows,...)制作单独的可执行文件,是否有办法制作可以分发的单个跨平台文件(最好是JAR)?
谢谢!
我应该使用$("#myElement")还是$(myElement)?两者似乎都有效.
我想生成一个接近于零且具有一定范围的数字.例如,假设我要的号码落在下的时间为10 90%,但有一个小的机会,这将是15,20年甚至30数越高,接受它的机会越低.
我已经尝试用关键字"加权概率"寻找一些东西但找不到任何导致正确方向的东西.
更新:
我最终使用了Box-Muller变换(见接受的答案).这是我写的简单代码:
const E = 2.71828183;
function getRandomCurvedValue(temp.median, temp.density) {
return this.getCurvedValue(random(0, 1), temp.median, temp.density);
}
function getCurvedValue(temp.value, temp.median, temp.density) {
return temp.median + (temp.density * log(E, (temp.value / (1 - temp.value))));
}
Run Code Online (Sandbox Code Playgroud) 我正在使用最新的MySQL.
我有两张桌子:backlinks和pages.
我正在尝试更新cache_links表中pages为某个网址调用的列.该列应该等于backlinkswhere destination=中的行数url.
我可以用我的应用程序语言这样做,以防我的意思不明确:
$myURL = "http://google.com/";
$numLinksResult = mysqli_query($c, "SELECT count(*) AS `num` FROM `backlinks` WHERE `same_domain` = FALSE AND `url` = '$myURL'");
$numLinksRow = mysqli_fetch_assoc($numLinksResult);
$numLinks = $numLinksRow['num'];
mysqli_query($c, "UPDATE `pages` SET `links` = $numLinks WHERE `url` = '$myURL'");
Run Code Online (Sandbox Code Playgroud)
我一直在尝试构建的查询看起来像这样:
UPDATE `pages`, `backlinks` SET pages.`cache_links` = backlinks.count(*)
WHERE backlinks.`destination` = pages.`url`
AND backlinks.`same_domain` = FALSE
AND pages.`url` = 'http://google.com/'
Run Code Online (Sandbox Code Playgroud)
我认为,问题在于backlinks.count(*).我不知道我做错了什么.我得到的错误是:
#1064 - …Run Code Online (Sandbox Code Playgroud)