为什么这段代码返回错误的值?
int i=Integer.MAX_VALUE+1;
long l=Integer.MAX_VALUE+1;
System.out.println(l);
System.out.println(i);
Run Code Online (Sandbox Code Playgroud) 我们知道:
ArrayList;
LinkedList;
TreeMap
Run Code Online (Sandbox Code Playgroud)
和其他...以及CamelCase格式的所有名称,但为什么Hashtable
,不是HashTable
?
这是无原则的问题,只是想知道:)
现在,我正在努力了解如何构建Hashtable
.
最有趣的 - 作为对象添加到Hashtable
?
我在一本书中读过:
第一步:计算hashCode()
对象.
接下来,我们确定此对象在Hashtable
:中的位置obj.hashCode() % Hashtable.length
.
例如,向以下内容添加更多元素Hashtable
:
Hashtable<String, String> hm=new Hashtable<String, String>(100);
hm.put("Lee","Lee");
hm.put("lee","lee");
hm.put("eel","eel");
Run Code Online (Sandbox Code Playgroud)
定义一个放置对象的桶:
System.out.println("Lee".hashCode() % 100);
System.out.println("lee".hashCode() % 100);
System.out.println("eel".hashCode() % 100);
Run Code Online (Sandbox Code Playgroud)
如果我理解算法,则必须将对象放在表中,如下所示:
eel /*because,"eel".hashCode() % 100=0*/,
lee /*because, "lee".hashCode() % 100=20*/,
Lee /*because, "Lee".hashCode() % 100=68*/
Run Code Online (Sandbox Code Playgroud)
但是我们看到了什么?
System.out.println(hm);
{Lee=Lee, lee=lee, eel=eel}
Run Code Online (Sandbox Code Playgroud)
请告诉我,我哪里出错了?
在第一步,我运行此代码:
public class Demo {
public static void main(String[] args) {
String x = "x";
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++)
{
x = x.concat("s");
// x+="k";
}
System.out.println(System.currentTimeMillis() - start);
}
}
Run Code Online (Sandbox Code Playgroud)
出:13579.
在第二步,我运行此代码:
public class Demo {
public static void main(String[] args) {
String x = "x";
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++)
{
//x = x.concat("s");
x+="k";
}
System.out.println(System.currentTimeMillis() - start);
}
} …
Run Code Online (Sandbox Code Playgroud) 例如,我有实体类User
:
public class User
{
private long id;
private String name;
// setters and getters
}
Run Code Online (Sandbox Code Playgroud)
接下来,我添加新的实体类: Comment
public class Comment
{
private long id;
private String comment;
// setters and getters
}
Run Code Online (Sandbox Code Playgroud)
接下来,我可以添加越来越多的实体类.
而且,此时我想:我可以/必须在逻辑结构中绑定/连接我的实体类还是没有?
我的意思是说?我试着解释一下:
要点1:所有这些类:User
,Comment
和更多的其他- POJO
.
想法1:需要通过接口或抽象类对这些类进行逻辑绑定.
第2点:我看到,所有实体类都有相同的方法:getId
和setId()
.
想法2:需要避免在所有类中声明此方法.
我的解决方案
添加界面BaseEntity
:
public interface BaseEntity
{
public long getId();
public void setId(long id);
}
Run Code Online (Sandbox Code Playgroud)
添加所有实体类必须实现此接口.
结果我们逻辑连接所有实体类.我们保证每个实体类的实现getId()
和setId()
方法.
但这种解决方案不能解决多申报的问题getId
和setId
.
解决方案是创建一般BaseEntity类: …
例如,我们有Book List页面.
此页面包含书籍列表.
如果用户角色="ADMIN"显示在页面按钮"删除书籍"或类似"编辑书".
如果用户角色="SIMPLY_USER",则用户无法看到任何类似"删除.."或"编辑..."的按钮.
快速查看Spring Security 3之后 - 我无法找到适用于我的案例的任何实现.
是真的?
例如,我有处理输入/输出流的方法:
public void doSomethingWithStreams () throws FileNotFoundException, IOException
{
OutputStream out1, out2;
InputStream in1, in2;
try{
//do something with Streams: read, write, process, etc.
}
finally{
//There I try to close connections
out1.close();
out2.close();
in1.close();
in2.close();
}
}
Run Code Online (Sandbox Code Playgroud)
方法可以抛出IOException并且它是有效的行为.但如果我在这行中有异常:
out1.close();
Run Code Online (Sandbox Code Playgroud)
其他三个Stream将不会关闭.你能推荐什么解决方案?怎么样?如何关闭所有?
我只有一个:
public void doSomethingWithStreams () throws FileNotFoundException, IOException
{
OutputStream out1, out2;
InputStream in1, in2;
try{
//do something with Streams: read, write, process, etc.
}
finally{
//There I try to close connections
try{out1.close();}
finally{
try{out2.close();} …
Run Code Online (Sandbox Code Playgroud) Spring Boot
应用:
a@RestController
接收以下有效负载:
{
"cartoon": "The Little Mermaid",
"characterNames": ["Ariel", "Prince Eric", "Sebastian", "Flounder"]
}
Run Code Online (Sandbox Code Playgroud)
我需要按以下方式处理它:
转换控制器接收到的数据:将角色名称替换为上一步从“卡通人物”微服务接收到的适当 ID。
{
"cartoon": "The Little Mermaid",
"characterIds": [1, 2, 3, 4]
}
使用转换后的数据向“cartoon-db”微服务发送 HTTP POST 请求。
我遇到的问题:
Reactive Programming
我需要使用(非阻塞\异步处理)和Spring WebFlux
(Mono
| Flux
)的范例来实现所有这些步骤Spring Reactive WebClient
- 但我对该堆栈的经验为零,试图尽可能多地阅读它,再加上谷歌搜索很多但是仍然有很多未解答的问题,例如:
Q1 . 我已经配置了响应式 webClient,向“卡通人物”微服务发送请求:
public Mono<Integer> getCartoonCharacterIdbyName(String characterName) {
return WebClient.builder().baseUrl("http://cartoon-characters").build()
.get()
.uri("/character/{characterName}", characterName)
.retrieve()
.bodyToMono(Integer.class);
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有一个卡通人物名称列表,对于每个角色我需要调用getCartoonCharacterIdbyName(String name)
方法,我不确定串联调用它的正确选项,相信正确的选项:并行执行。
写了如下方法: …
reactive-programming spring-boot reactive spring-webflux spring-webclient
有以下Controller方法:
@ApiResponses(value = {@ApiResponse(responseCode = "200")})
@GetMapping(value = API_URI_PREFIX + PRODUCTS_URI, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public Flux<Product> getProducts(@Valid @NotNull PagingAndSorting pagingAndSorting) {
}
Run Code Online (Sandbox Code Playgroud)
我需要找到一种如何在对象Swagger
示例中显示的方法PagingAndSorting
。
我正在使用springdoc-api
v1.4.3。
我想执行的代码MongoTemplate
:
{
$merge: {
into: 'someCollection',
on: "_id",
whenMatched: 'merge',
whenNotMatched: 'discard'
}
}
Run Code Online (Sandbox Code Playgroud)
我没有找到任何合适的方法来描述$merge
阶段,怀疑是否Spring Data MongoDB
支持这个阶段?
spring mongodb aggregation-framework spring-data-mongodb spring-boot