有一个Person对象列表.
List<Person> persons = generatePersons();
使用它创建一个unmodifiableList.
List<Person> unmodifiableList = Collections.unmodifiableList(persons);
据我所知,unmodifiableList这不支持添加/删除/设置操作.同时它不是不可变的,因为它引用了现有的可修改列表,persons并且每当对persons列表进行更改时,更改也会反映出来unmodifiableList.
以这种方式创建不可变列表.
List<Person> immutableList = Collections.unmodifiableList(new ArrayList<>(persons));
这会创建一个不可变列表,因为正在使用转换构造函数.不能执行添加/删除/设置操作,immutableList也不能persons反映原始列表中的任何更改immutableList.让我们假设Person对象也是不可变的.
现在,我想使用流创建这两个列表.
第一个,我创建使用:
List<Person> unmodifiablePersons = persons.stream()
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
我迷失了通过流创建等效的immutableList.
我怎样才能做到这一点?
编辑:
我Person在原始列表中添加了一个新对象persons并打印了persons列表的大小和unmodifiablePersons.两者都给我相同的大小.因此,变化正在反映unmodifiablePersons,因此它不是一成不变的.我在这里错过了什么吗?
编辑2
愚蠢.本应该通过文档.unmodifiablePersons确实是一个不可改变的清单.此外,新Person对象在unmodifiablePersons创建之前添加,因此上述观察.超级傻.
class Person implements Cloneable {
String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Person clone() throws CloneNotSupportedException {
return (Person) super.clone();
}
}
Person p1 = new Person();
p1.setFirstName("P1 Sara");
Person p3 = new Person();
try {
p3 = (Person) p1.clone();
} catch (CloneNotSupportedException e) {
}
p3.setFirstName("cloned Sara");
System.out.println("P3 : " + p3.getFirstName());
System.out.println("P1: " + p1.getFirstName());
Run Code Online (Sandbox Code Playgroud)
我读过 clone() 方法实际上是一种浅拷贝。因此,我假设当 P3 中某个字段的值发生变化时,P1 中的值也会发生变化。但是,那没有发生。我在这里缺少什么?
public class HelloWorld{
public static void main(String []args){
int orig=103, reverse=0, mod;
int numOfDigits=0;
int n = orig;
while (n>0){
n /= 10;
numOfDigits++;
}
n = orig;
while (n > 0){
mod = n % 10;
reverse = reverse + (int)(mod * java.lang.Math.pow(10, numOfDigits-1));
numOfDigits--;
n /= 10;
}
System.out.println("Reversed is : " + reverse);
}
Run Code Online (Sandbox Code Playgroud)
}
我知道reverse = reverse + (int)(mod * java.lang.Math.pow(10, numOfDigits-1));可以替换为reverse = mod + (reverse*10).
想知道我是否通过计算总位数和施加功率来增加简单程序的复杂性?
PS:请假设orig可以作为用户的输入,并且可以是任意数量的数字.我的硬编码仅用于实验.
我编写了一个简单的Spring Boot应用程序,稍后我将扩展它以构建Spring REST客户端.我有一个工作代码; 我正在玩弄更好地理解这些概念.代码如下.
@SpringBootApplication
public class RestClientApplication {
public static void main(String[] args) {
SpringApplication.run(RestClientApplication.class, args);
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
RestClientApplication.class)) {
System.out.println(" Getting RestTemplate : " + ctx.getBean("restTemplate"));
}
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
@Bean
public CommandLineRunner run(RestTemplate template) {
return args -> {
System.out.println("Rest Template instance from CLR is : " + template);
};
}
}
Run Code Online (Sandbox Code Playgroud)
意见
Rest Template instance from CLR is : org.springframework.web.client.RestTemplate@1e53135d
Getting RestTemplate : org.springframework.web.client.RestTemplate@5aa6202e
Run Code Online (Sandbox Code Playgroud)
问题 …
java ×3
algorithm ×1
clone ×1
cloneable ×1
collections ×1
collectors ×1
java-8 ×1
java-stream ×1
reverse ×1
spring ×1
spring-boot ×1