This is the 21st day of my participation in the August More Text Challenge
Which do you prefer, Monday, for or foreach?
public static void removeFor(a) {
List<Integer> list = new ArrayList<>(Arrays.asList(1.2.3.4));
for (int i = 0; i < list.size(); i++) {
list.remove(list.get(i));
}
System.out.println(list);
}
public static void removeForeach(a) {
List<Integer> list = new ArrayList<>(Arrays.asList(1.2.3.4));
for (Integer integer : list) {
list.remove(integer);
}
System.out.println(list);
}
Copy the code
Q: What are the two outputs above? What’s the difference between the two? A: The first question is meaningless. Delete elements. It is not possible to delete elements like this. [2, 4] and ConcurrentModificationException exception to the second question foreach traversal collection types, and array type of the underlying implementation
- 1. The traversal nature of collection types is implemented using iterators
- 2. Arrays are traversed by a for loop
For is flexible, but not bad for reading. Foreach is good for reading. There are two kinds of ArrayLists. For is greater than foreach. LinkedList, foreach is greater than for
Tuesday, pit dad field (old project legacy field trouble)
@RestController
public class Test {
// The first port is 8080
@GetMapping("test")
public Demo test(a){
Demo demo = new Demo();
demo.setXID("1");
demo.setXName("420");
demo.setPowerWord("123456");
return demo;
}
/ / the second
public static void main(String[] args) {
Demo demo = new Demo();
demo.setXID("1");
demo.setXName("420");
demo.setPowerWord("123456");
RestTemplate restTemplate = new RestTemplate();
String s = restTemplate.postForObject("xxx", demo, String.class);
}
@Data
static class Demo{
private String XID;
private String XName;
privateString powerWord; }}Copy the code
Example:
// This project is a Springboot project automatically generated by IDEA
// If you think this is all, please write this down below,
// If not, write down your understanding, or how to get the following results
{"powerWord":"123456"."XID":"123"."XName":"420"}
Copy the code
Q: What are the Json strings that write the request I get and the request I get? Adapt from the example. Answer: Yes
{"powerWord":"123456"."xid":"123"."xname":"420"}
Copy the code
To get
{"powerWord":"123456"."XID":"123"."XName":"420"}
Copy the code
It is necessary
@JsonProperty("XID")
private String XID;
Copy the code
The same goes for the rest
On Wednesday, an interview question tested your understanding of Java
public static void main(String[] args) {
Integer a = 4;
Integer b = 2;
System.out.printf("a = %s, b = %s\n", a, b);
// Swap a and b
swap(a, b);
System.out.printf("a = %s, b = %s\n", a, b);
}
public static void swap(Integer a, Integer b) {
/ / TODO
}
Copy the code
Q: Please complete the above code to exchange the values of a and B. A: To solve this problem, know that value passing: passes real values, like primitive datatype reference passing: passes a reference to an object as an argument. It is possible that you can create a temporary variable temp to exchange two values or use ^, but the result will be wrong. This looks at Java reflection and the gap between Integer 127 and 128
public static void swap(Integer a, Integer b) {
int temp = a.intValue();
try {
Field value = Integer.class.getDeclaredField("value");
value.setAccessible(true); value.set(a, b);
value.set(b, new Integer(temp));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch(IllegalAccessException e) { e.printStackTrace(); }}Copy the code
Thursday, don’t cry!!
public static void main(String[] args) {
System.out.println(test());
}
public static String test(a) {
String ret = "";
try{
throw new Exception();
}
catch(Exception e){
ret = "Cry";
System.out.println("Small");
return ret;
}
finally{
ret = "A very big cry.";
System.out.println("No more questions."); }}Copy the code
Q: Write down the result of the above output: the correct answer is that if you cry, you will first return the value, but not return it.
Friday, this meeting is over, and I think you can graduate Java8 Lambda.
@Data
@Builder
static class Order{
private Integer id;
// User name
private String userName;
// Product name
private String productName;
}
public static void main(String[] args) {
Order order1 = Order.builder().id(1).userName("zy").productName("Small fish").build();
Order order2 = Order.builder().id(2).userName("ss").productName("Shrimp").build();
Order order3 = Order.builder().id(3).userName("zy").productName("Shrimp").build();
Order order4 = Order.builder().id(4).userName("zy").productName("Small fish").build();
Order order5 = Order.builder().id(4).userName("ss").productName("Shrimp").build();
// All orders
List<Order> orderList = new ArrayList<>(Arrays.asList(order1, order2, order3, order4, order5));
// Now the fish can only stay with the fish, the shrimp can only stay with the shrimp,
// So there are several orders to pack
// The name is zy, the commodity is minnows that can only appear once and count as a package
// Write the List in a single line of code
// The last size of the list is 3
// They are zy fish, SS shrimp, zy shrimp
List<Order> collect =
}
Copy the code
Ask: in a word, solve double layer to answer: the first kind
List<Order> collect =
orderList
.stream()
.collect(Collectors
.collectingAndThen(Collectors
.toCollection(() -> new TreeSet<>(Comparator
.comparing(s -> s.getUserName() + s.getProductName()))), ArrayList::new));
Copy the code
The second,
List<Order> collect =
orderList
.stream()
.collect(Collectors
.groupingBy(order -> "u:" + order.getUserName() + "p:" + order.getProductName()))
.values()
.stream()
.map(list-> list.get(0))
.collect(Collectors.toList());
Copy the code
Saturday, Saturday rest, this problem anyway I can not, you can?
There are now2A glass of water. A cup of13Rose, a glass of7L. There's another one19Liters of empty cups. How to pour out2A cup of10Litres of water.Copy the code
Q: Turn your little head to answer this question. A: (0,13,7) – > (13,0,7) – > (19, 1) – > (19, 0) – > (12,1,7) – > (12,8,0) – > (5,8,7) – > (5,13,2) – > (18,0,2) – > (18 0) – > (11,2,7) – > (11,9,0) – > ( 4,9,7) – > (4,13,3) – > (17,0,3) – > (17,3,0) – > (10,3,7) – > (10,10,0)