我知道这是一个非常基本的问题,但我想明确这个概念.我想知道==运算符在原始类型和对象类型的情况下如何工作.例如
Integer a = 1;
int b = 1;
System.out.println(a == b)
Run Code Online (Sandbox Code Playgroud)
如何a与之比较b,而a包含包含值1的对象的引用.有人可以向我清楚它是如何在内部工作的吗?
我试图理解数据结构和不同的算法,然后我很困惑地测量冒泡排序时间的复杂性.
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在每个Big O都会告诉Best case O(n),Avg case(n2)和Worst Case(n2).但是当我看到代码时,发现第一阶段内部循环运行n次然后是第二阶段n - 1,n - 2等等.这意味着在每次迭代中它的值都会下降.例如,如果我有[] = {4,2,9,5,3,6,11},那么比较的总数将是 -
1st Phase - 7 time
2nd phase - 6 time
3rd Phase - 5 time
4th …Run Code Online (Sandbox Code Playgroud) 我正在使用java,spring jdbc模板将n条记录插入到两个表中.有些像这样
假设正确配置了daos.xml.
ApplicationContext ctxt = new ClassPathXmlApplicationContext("daos.xml");
JdbcTemplate template = (JdbcTemplate) ctxt.getBean("jdbcTemplate");
final List<Person> list = new ArrayList<>();
final List<Role> roles = new ArrayList<>();
for(int i =1; i<=100; i++){
Person item = new Person();
item.setFirstName("Naveen" + i);
item.setLastName("kumar" + i);
item.setDescription("D" + i);
list.add(item);
Role role = new Role();
role.setName("Admin");
role.setCode("c" + i);
roles.add(role);
}
String sql = "insert into person(first_name, last_name, description) values(?,?,?)";
int[] arr = template.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws …Run Code Online (Sandbox Code Playgroud) 我最近开始使用 java 流并编写一个用户服务,它返回一个用户流。使用该用户流,我处理其他逻辑。
以下是我正在处理流的一段代码,它工作正常
try (Stream<User> users = userService.getStream()) {
users.forEach(user -> {
});
Run Code Online (Sandbox Code Playgroud)
但是当我开始编写 Junit 时,它失败并显示以下错误消息。
java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.sourceStageSpliterator(AbstractPipeline.java:279)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:647)
at com.test.UserService.sendBulkNotification(UserService.java:47)
at com.test.UserServiceTest.sendNotificationTest(UserServiceTest.java:64)
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试代码:
List<User> users = new ArrayList<>();
for(long i = 1; i <= 5; i++) {
User user = new User();
user.setId(i);
users.add(user);
}
when(userService.getStream()).thenReturn(users.stream());
userService.sendNotification("user", 1, "test.com");
Run Code Online (Sandbox Code Playgroud)
你能帮我用流编写测试用例/帮我解决这个问题吗?
我们在 AWS 上有一个 ecr 存储库。其中包含所有舵图。此 ecr 受到保护,有人为我分配了一个角色。这个角色允许我从 aws cli 控制台获取所有图像。
现在我正在使用 helm 来部署图表。所以对于我使用的以下代码。当我运行 helm dep update 命令时,这只拉取 postgres 图像和测试图表请求失败,并出现错误 401。
我知道我需要在某个地方提到 aws 凭证,但不知道应该在哪里使用它。还有一件事,如果有人能告诉我如何使用 AWS 访问令牌来访问它,那就太好了。
dependencies:
- name: postgresql
version: 9.2.1
repository: https://charts.bitnami.com/bitnami
condition: postgresql.enabled
- name: createdb
version: latest
repository: https://111.ecr.eu-central-1.amazonaws.com/test-chart
Run Code Online (Sandbox Code Playgroud) 我正在研究 Springboot 和 Kubernetes,并且我有一个连接到 Postgres 数据库的非常简单的应用程序。我想从 configmap 中获取数据源的值,从机密中获取密码作为挂载文件。
配置映射文件:
apiVersion: v1
kind: ConfigMap
metadata:
name: customer-config
data:
application.properties: |
server.forward-headers-strategy=framework
spring.datasource.url=jdbc:postgresql://test/customer
spring.datasource.username=postgres
Run Code Online (Sandbox Code Playgroud)
秘密档案:
apiVersion: v1
kind: Secret
metadata:
name: secret-demo
data:
spring.datasource.password: cG9zdGdyZXM=
Run Code Online (Sandbox Code Playgroud)
部署文件:
spec:
containers:
- name: customerc
image: localhost:8080/customer
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8282
volumeMounts:
- mountPath: /workspace/config/default
name: config-volume
- mountPath: /workspace/secret/default
name: secret-volume
volumes:
- name: config-volume
configMap:
name: customer-config
- name: secret-volume
secret:
secretName: secret-demo
items:
- key: spring.datasource.password
path: password
Run Code Online (Sandbox Code Playgroud)
如果我将 spring.datasource.password prop …
我正在编写一个自定义 Gradle 插件来为所有项目添加公共资源。例如,所有微服务都使用 SpringBoot Web、sleuth 和其他常见依赖项。
因此决定创建一个独立项目并将插件导出为 jar,然后将插件应用于其他项目。
以下是 build.gradle 的一段代码。test-common 中是另一个插件,其中包含有关存储库和发布的信息。
plugins {
id 'java-gradle-plugin'
id 'test-common'
}
gradlePlugin {
plugins {
simplePlugin {
id = 'test.resource-plugin'
implementationClass = 'com.test.CommonResource'
}
}
}
public class CommonResourcesPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
var dependencySet= project.getConfigurations().getByName("compile").getDependencies();
log.info("Applying depdencies");
project.getDependencies().add("implementation", "org.springframework.boot:spring-boot-starter-web");
project.getDependencies().add("implementation", "org.springframework.boot:spring-boot-starter-actuator");
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我构建并发布这个 gradle 项目时,它会在 Maven 存储库中创建一个 jar,其中包含 META-INF/gradle.plugins/test.resource-plugin.properties 并且源文件夹包含 java 文件。
现在是第二部分,将该插件应用到其他项目。
plugins {
id "test.resource-plugin" version '1.0-SNAPSHOT'
}
dependencies {
implementation "com.test:test-starter-plugin:1.0-SNAPSHOT"
} …Run Code Online (Sandbox Code Playgroud) 我正在使用 aws cdk 库来创建资源,并且能够成功创建资源。现在想要测试这个生成的statck。同样,如果它的单个资源能够很好地测试它,但是当堆栈有多个资源时,那么不知道如何休息。
以下是创建资源的代码。
constructor(scope: cdk.Construct, id: string, props: CIAMSQSProps = {} ) {
super(scope, id);
const queue = new sqs.Queue(this, id, {
queueName: props.queueName!,
});
let ssmParamters = new CreateSSMParamaters(this, id, {
envName: props.envName!,
envValue: sqsQueue.queueUrl
});
}
Run Code Online (Sandbox Code Playgroud)
以下是测试代码,但由于堆栈包含引用而失败。
test('Test SSM Parameter', () => {
const app = new App();
const sqsStack = new TestSQS(app, 'test-sqs-stack', {
queueName: 'TestQueue',
envName: 'TestQueue',
});
const template = Template.fromStack(sqsStack);
template.hasResourceProperties(ResourceTypes.SSM_PARAM_TYPE, "{ Name: 'TestQueue', Value: 'TestQueue', Type: 'String' }");
});
Run Code Online (Sandbox Code Playgroud)
以下是错误。
Template …Run Code Online (Sandbox Code Playgroud) 我正在尝试将复杂的Java对象转换为JSON.但我无法这样做.我正在使用Java泛型.
这是存储页面输出的通用对象.我没有发布JsonTest对象结构.它与任何带有property和setter和getter的普通java类相同.
class ListRow<T>{
private int total;
private int currentRow;
private List<T> test;
public ListRow(int total, int currentRow, List<T> test) {
this.total = total;
this.currentRow = currentRow;
this.test = test;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCurrentRow() {
return currentRow;
}
public void setCurrentRow(int currentRow) {
this.currentRow = currentRow;
}
public List<T> getTest() {
return test;
}
public void setTest(List<T> test) {
this.test = test;
}
JsonTest …Run Code Online (Sandbox Code Playgroud) package MavenWeb.MavenWeb;
import java.util.HashMap;
import java.util.Map;
public class StringEquality {
public static void main(String[] args) {
Person p1 = new Person("Naveen", 22, 1000);
Person p2 = new Person("Naveen", 21, 2000);
Person p3 = new Person("Naveen", 23, 3000);
if(p1.equals(p2)){
System.out.println("P1 and p2 :" + p1.equals(p2));
} else{
System.out.println("P1 and p2 :" + p1.equals(p2));
}
if(p1.equals(p3)){
System.out.println("P1 and p3 :" + p1.equals(p3));
}
Map<Person, Object> map = new HashMap<Person, Object>();
map.put(p1, p1);
map.put(p2, p2);
System.out.println(map.get(new Person("Naveen", 21, 2000)));
}
}
Run Code Online (Sandbox Code Playgroud)
...
class …Run Code Online (Sandbox Code Playgroud) 我面临着龙目岛和杰克逊的非常奇怪的问题。以下是我正在处理的代码片段。
@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
public class Order{
//@JsonIgnore
@Getter(onMethod = @__(@JsonIgnore))
private boolean userPresent;
}
Run Code Online (Sandbox Code Playgroud)
所以我想要的是,这个 dto 应该序列化为 Json,那么这个 userPresent 属性不应该作为响应属性。我虽然@JsonIgnore 会为我工作。但我认为根据 /sf/answers/3998364611/文章,这是 Lombok 的一些问题。然后我改变了使用OnMethod的方法。
现在,在 Eclipse 上编译得很好,但是当我尝试使用 mvn 进行编译时,它给出了以下错误。
有人可以帮我解决它无法与 Maven 一起使用的问题吗?
我正在使用 kubernetes 及其资源(例如秘密)。在部署过程中,已创建一个机密(例如测试机密),其中包含一些值。现在我需要在同一命名空间内重命名此秘密(dev-secret)。如何重命名机密或如何将 test-secret 值复制到 dev-secret。
请让我知道正确的方法。
我问的是非常基本的问题,但是真的很困惑equals方法在java中是如何工作的.让我举一个例子,我在类级别声明类型为String的三个变量.
String a = "abc";
String b = "abc";
String c = new String("abc");
Run Code Online (Sandbox Code Playgroud)
然后根据java拇指规则编写一个方法来比较它们.
public void compare(){
System.out.println("a.equals(c) :" + a.equals(c));
System.out.println("a == b :" + (a == b));
System.out.println("a == c :" + (a == c));
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行程序时,它给了我下面的输出.
a.equals(c) :true
a == b :true
a == c :false
Run Code Online (Sandbox Code Playgroud)
现在我很困惑,因为我知道写成文字的字符串总是被创建到StringPool中.这意味着变量a和b将被创建为StringPool,并且根据stringPool,将只有一个实例,变量a和b将指向此变量.变量c将被创建到堆内存中.当我比较a.equals(c)时,它给了我真实的可能性,因为equals的默认实现总是比较内存分配而不是内容.它应该返回false.
我对整数也做了同样的事情
Integer m = 1;
Integer n = 1;
Integer o = new Integer(1);
public void compareInt(){
System.out.println("m.equals(o) :" + m.equals(o));
System.out.println("m == n :" + (m == n)); …Run Code Online (Sandbox Code Playgroud) java ×7
kubernetes ×2
maven ×2
algorithm ×1
amazon-ecr ×1
aws-cdk ×1
equality ×1
gradle ×1
gson ×1
jackson ×1
java-8 ×1
java-stream ×1
json ×1
lombok ×1
mysql ×1
sorting ×1
spring ×1
spring-boot ×1
spring-jdbc ×1
typescript ×1
unboxing ×1
unit-testing ×1