假设我有以下方法:
public void change(Map<String, String> map)
Run Code Online (Sandbox Code Playgroud)
如果map是的话null,我想抛出一个例外,如果不是的话,我会制作一个防御性的副本.
这会是首选:
public void change(Map<String, String> map)
{
Map<String, String> temp = null;
synchronized (map) {
if (map == null)
throw new NullPointerException("map is null");
temp = new HashMap<String, Object>(map);
}
Run Code Online (Sandbox Code Playgroud)
要么
public void change(Map<String, String> map)
try {
Map<String, String> temp = new HashMap<String, Object>(map);
}
catch (NullPointerException e) {
throw new NullPointerException("map is null");
}
Run Code Online (Sandbox Code Playgroud)
或者,还有更好的方法?为什么不呢.
编辑:
我修改了一些错字:
properties 应该 map
(String,Object)应该是 (String, String)
谢谢!
我很开心,让自己陷入困境.
我需要在文件中组织多线程查找:
用户输入find(路径)和find(word);
.txt在文件夹中查找文件并将结果添加到队列中 Qestions:
MultiThreadingSearch(或更好地使用组合)吗?码:
import java.util.*;
import java.io.*;
class ArrayListOfFiles {
private Node first, last;
private class Node {
String item;
Node next;
}
public boolean isEmpty() {
return first == null;
}
public synchronized void enqueue(String item) {
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty())
first = last;
else
oldlast.next = last;
}
public synchronized String dequeue() {
String …Run Code Online (Sandbox Code Playgroud) 我想知道我们如何更好地使用else-if运算符来反映这部分代码.何时执行ifuals检查不同的扩展?
码:
private void findFiles(String path) {
try {
File root = new File(path);
File[] list = root.listFiles();
for (File currentFile : list) {
if (currentFile.isDirectory()) {
findFiles(currentFile.getAbsolutePath());
} else {
if (currentFile.getName().toLowerCase().endsWith((".txt"))) {
queue.put(currentFile);
} else if (currentFile.getName().toLowerCase()
.endsWith((".pdf"))) {
queue.put(currentFile);
} else if (currentFile.getName().toLowerCase()
.endsWith((".doc"))) {
queue.put(currentFile);
} else if (currentFile.getName().toLowerCase()
.endsWith((".docx"))) {
queue.put(currentFile);
} else if (currentFile.getName().toLowerCase()
.endsWith((".html"))) {
queue.put(currentFile);
} else if (currentFile.getName().toLowerCase()
.endsWith((".htm"))) {
queue.put(currentFile);
} else if (currentFile.getName().toLowerCase()
.endsWith((".xml"))) {
queue.put(currentFile);
} else if …Run Code Online (Sandbox Code Playgroud) 我创建了类Word.Word有一个构造函数,它接受一个字符串参数和一个方法getSubstrings,它返回一个包含word的所有子字符串的String,按长度排序.
例如,如果用户提供输入"rum",则该方法返回一个字符串,其打印方式如下:
r
u
m
ru
um
rum
Run Code Online (Sandbox Code Playgroud)
我想连接一个String中的子串,用换行符分隔它们("\n").然后返回字符串.
码:
public class Word {
String word;
public Word(String word) {
this.word = word;
}
/**
* Gets all the substrings of this Word.
* @return all substrings of this Word separated by newline
*/
public String getSubstrings()
{
String str = "";
int i, j;
for (i = 0; i < word.length(); i++) {
for (j = 0; j < word.length(); j++) {
str = word.substring(i, i + j);
str …Run Code Online (Sandbox Code Playgroud) 我想知道是否存在某种方式:
git merge --no-ff mybranche
git merge的默认行为?
该--no-ff标志使合并始终创建新的提交对象,即使可以使用快进执行合并.这样可以避免丢失有关功能分支历史存在的信息,并将所有一起添加功能的提交组合在一起.
我是斯卡拉的新人.
我想运行在我的文件中写的函数while.scala:
def whileLoop {
var i = 1
while(i <= 3) {
println(i)
i += 1
}
}
whileLoop
Run Code Online (Sandbox Code Playgroud)
这是它如何看待REPL:
scala> scala /home/nazar/Downloads/while.scala
<console>:1: error: illegal start of simple expression
Run Code Online (Sandbox Code Playgroud)
如果我从书中理解的话.我需要指定.scala文件位置并运行:
scala /path/to/file.scala
为什么失败?
这是书中的片段:
You can run the code like this:
batate$ scala code/scala/while.scala
1
2
3
Run Code Online (Sandbox Code Playgroud)
更新:
我尝试了:load它的工作选项,但是当我想运行另一个文件时for_loop.scala:
def forLoop {
println( "for loop using Java-style iteration" )
for(i <- 0 until args.length) {
println(args(i))
}
}
forLoop
Run Code Online (Sandbox Code Playgroud)
它失败: …
我必须以另一种风格编写代码。我想知道如何为此目的配置 IntelliJ 吗?
这是在查看代码之前:
@Override
public void generateTestObjectsInDb(Object... objects) {
try {
for (Object obj : objects) {
DataBaseObjectService.createObjectInDB(obj);
}
} catch (Exception e) {
Logger.fatal("Error occured while generating data in product Database.", e);
}
}
Run Code Online (Sandbox Code Playgroud)
它应该是这样的:
@Override
public void generateTestObjectsInDb(Object... objects)
{
try
{
for (Object obj : objects)
{
DataBaseObjectService.createObjectInDB(obj);
}
}
catch (Exception e)
{
Logger.fatal("Error occured while generating data in product Database.", e);
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用标准键绑定Ctrl + Alt + L来实现这个新代码?
我想只使用与Intellij一起使用的键盘.
我可以Project view用捷径轻松打开:
Alt + 1
如何再次转向编辑并离开Project view?
当然,不使用鼠标.
有什么建议?
文件明显位于jar中时,我正在获取FileNotFoundException。为什么会这样呢?
java.io.FileNotFoundException: file:/Users/serviceuser/project/coolApp/target/coolApp-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/ssl_certs/mysslstore.jks (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method) ~[na:na]
at java.base/java.io.FileInputStream.open(FileInputStream.java:219) ~[na:na]
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) ~[na:na]
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112) ~[na:na]
Run Code Online (Sandbox Code Playgroud)
但是,我看到文件打包在jar中。
jar -tf coolApp-1.0-SNAPSHOT.jar | grep ssl
编辑 我加载文件如下
new FileInputStream(CoolApp.class.getClassLoader().getResource("ssl_certs/mysslstore.jks").getFile())
Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring Boot 3.2.1。
我不想将敏感数据作为application.yml. 所以他们指的是环境变量。
在应用程序的 IntelliJ 配置设置(在Environment variables部分)中执行工作正常。
但是,从控制台执行 Maven 失败:
./mvnw -Ddemo-api-key=all56 -Ddemo-host=https://demo.api spring-boot:run
Run Code Online (Sandbox Code Playgroud)
以下是异常详情:
java.lang.IllegalArgumentException: Not enough variable values available to expand 'demo-host'
at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:367) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:262) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents$PathSegmentComponent.expand(HierarchicalUriComponents.java:960) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:434) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:52) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.util.UriComponents.expand(UriComponents.java:172) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
Run Code Online (Sandbox Code Playgroud)
这是来自的片段application.yml:
car-parks-url: ${demo-host}/cap-ws/getCarParks?apiKey=${demo-api-key}
Run Code Online (Sandbox Code Playgroud)
如果我将它放在更高的位置,application.yml它可以正常工作:
demo-api-key: all56
demo-host: https://demo.api
Run Code Online (Sandbox Code Playgroud)
URL 格式正确,所有数据都被正确提取。
无法理解将它作为环境变量传递给 maven 会错过什么?
java ×6
maven ×2
spring-boot ×2
algorithm ×1
find ×1
git ×1
if-statement ×1
keyboard ×1
merge ×1
queue ×1
refactoring ×1
scala ×1
shortcut ×1
substring ×1
synchronized ×1
yaml ×1