我有一个枚举:
public enum Persons {
CHILD,
PARENT,
GRANDPARENT;
}
Run Code Online (Sandbox Code Playgroud)
使用ordinal()方法检查枚举成员之间的"层次结构" 是否有任何问题?我的意思是 - 使用它时有什么缺点,不包括冗长,当有人可能在将来意外改变时.
或者做这样的事情会更好:
public enum Persons {
CHILD(0),
PARENT(1),
GRANDPARENT(2);
private Integer hierarchy;
private Persons(final Integer hierarchy) {
this.hierarchy = hierarchy;
}
public Integer getHierarchy() {
return hierarchy;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个学生实体.我的想法是在ArrayList中收集多个学生对象,并将该列表中的所有对象保存到数据库中.什么时候使用@ElementCollection注释?它适用于这种情况吗?
学生:
package basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + …Run Code Online (Sandbox Code Playgroud) 所以我有User类
User{
id
name
}
Run Code Online (Sandbox Code Playgroud)
我需要转换List<User>为使用流的数组,所以我正在做的一种方法是转换为列表然后转换为数组
coll.stream().map(am -> am.getId())
.collect(Collectors.<Integer>toList())
.toArray(new Integer[0])
Run Code Online (Sandbox Code Playgroud)
但我认为应该有另一种方法直接转换为数组而不是添加列表然后转换为数组.
我有规格:
final String text = "%text%";
final Specifications<PersonEntity> spec = Specifications.where(
(root, query, builder) -> builder.like(builder.lower(root.join(PersonEntity_.addresses, JoinType.LEFT).get(AddressEntity_.addressLine1)), text)
).or(
(root, query, builder) -> builder.like(builder.lower(root.join(PersonEntity_.addresses, JoinType.LEFT).get(AddressEntity_.addressLine2)), text)
).or(
(root, query, builder) -> builder.like(builder.lower(root.join(PersonEntity_.addresses, JoinType.LEFT).get(AddressEntity_.city)), text)
)
Run Code Online (Sandbox Code Playgroud)
使用后:
personRepository.findAll(spec);
Run Code Online (Sandbox Code Playgroud)
在日志中,我看到,JPA创建了一个查询,它连接地址三次而不是一次.
如何编写一个只连接一次地址的规范?
在实施细节中HashMap,我可以阅读:
When using comparators on insertion, to keep a
* total ordering (or as close as is required here) across
* rebalancings, we compare classes and identityHashCodes as
* tie-breakers.
Run Code Online (Sandbox Code Playgroud)
如果我有恒定hashCode和精细equals,我的班级没有实现Comparable它将如何打破关系以及如何构建树?
我的意思是 - 斗将变成一棵树,System.identityHashCode用来打破平局.然后我会尝试调用containsKey方法与不同的实例(这将具有相同的hashCode和a.equals(b) == true)就会有不同的identityHashCode所以是有可能,树会被错误的节点进行遍历(左不是右),它会找不到钥匙?
我错过了什么或者这是正常行为吗?
Spring Boot 2.0.0.RELEASE
我有属性类:
@Configuration
@ConfigurationProperties(prefix="person")
public class PersonProperties {
private AddressProperties addressProperties;
public AddressProperties getAddressProperties() {
return addressProperties;
}
public void setAddressProperties(final AddressProperties addressProperties) {
this.addressProperties = addressProperties;
}
public static class AddressProperties {
private String line1;
public String getLine1() {
return line1;
}
public void setLine1(final String line1) {
this.line1 = line1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并且application.yml:
person:
address:
line1: line1OfAddress
Run Code Online (Sandbox Code Playgroud)
它没有像我的AddressProperties对象那样正确绑定null。当一个类与 yml 属性具有相同的名称时AddressProperties->Address它运行良好。我试图添加Qualifier或ConfigurationProperties前缀,address …
如何为两个数字的范围设置验证。我想将第一个数字的最大值设置为第二个数字的值,将第二个数字的最小值设置为第一个数字的值。这是我的js代码:
min_num : {
validators : {
integer : {
message : 'Please enter the valid number ',
min : 1,
max : 'max_num'
},
notEmpty : {
message : 'Please enter the number'
}
}
},
max_num : {
validators : {
integer : {
message : 'Please enter the valid number ',
min : 'min_num',
max : 10000
},
notEmpty : {
message : 'Please enter the number'
}
}
}
Run Code Online (Sandbox Code Playgroud)
和 HTML:
<div class="form-group">
<label class="col-md-2 control-label">Min …Run Code Online (Sandbox Code Playgroud) 我正在使用spring创建一个异步休息调用
@GetMapping(path = "/testingAsync")
public String value() throws ExecutionException, InterruptedException, TimeoutException {
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
String baseUrl = "https://api.github.com/users/XXX";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
String value = "";
HttpEntity entity = new HttpEntity("parameters", requestHeaders);
ListenableFuture<ResponseEntity<User>> futureEntity = restTemplate.getForEntity(baseUrl, User.class);
futureEntity.addCallback(new ListenableFutureCallback<ResponseEntity<User>>() {
@Override
public void onSuccess(ResponseEntity<User> result) {
System.out.println(result.getBody().getName());
// instead of this how can i return the value to the user ?
}
@Override
public void onFailure(Throwable ex) {
}
});
return "DONE"; // instead of …Run Code Online (Sandbox Code Playgroud) 是否有可能(以简单的方式)将其更改为java8 Stream?(请不要评论/回答,如果你想告诉我两个for更好,而不是所有循环都应该改为流,这不是一个点)
final Map<String, String> map = new HashMap<>();
for(final Person person: list) {
for(final Internal internal: person.getInternals()) {
final String key = person.getName() + internal.getKey();
map.put(key, internal.getValue());
}
}
Run Code Online (Sandbox Code Playgroud)
主要问题是我无法使用,flatMap因为我将丢失以前的信息.每个创建key都是独特的.
我正在将本教程用于 multibroker kafka 集群。我建立了三个经纪人:
并使用了这个命令: bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
据我所知,如果我的一个经纪人死了,另一个仍将运行并处理消息。
问题是,如果我杀了9092我就不能使用这个命令:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
我知道该端口已被终止,但是 - 如何传递通用引导服务器以使其运行?我错过了什么?
编辑1:
后
bin/kafka-console-consumer.sh --bootstrap-server 本地主机:9092,本地主机:9093,本地主机:9094 --from-beginning --topic my-replicated-topic
它给了我信息:
警告 [Consumer clientId=consumer-1, groupId=console-consumer-82352] 无法建立到节点 -1 的连接。经纪人可能不可用。(org.apache.kafka.clients.NetworkClient)`
控制台停止。未读任何消息
java ×9
java-8 ×3
spring ×3
java-stream ×2
apache-kafka ×1
arraylist ×1
asynchronous ×1
bootstrap-4 ×1
coding-style ×1
enums ×1
hashmap ×1
hibernate ×1
javascript ×1
jpa ×1
jquery ×1
rest ×1
rest-client ×1
spring-boot ×1
validation ×1
verbose ×1