A small story for debugging. How to track when a value is added to a List.
public class Foo {
public List<String> list = new ArrayList<>();
}
If you want to monitor when a value is added to a List like the one above, you can rewrite it to an anonymous class and override the ʻadd` method as shown below to output a log when the value is added or process at a breakpoint. You can stop it.
public class Foo {
public List<String> list = new ArrayList<String>() {
@Override
public boolean add(String string) {
//If you add a breakpoint here, you can stop the process when the value is added.
System.out.println("added:" + string);
return super.add(string);
}
};
}
Not only ʻadd but also other processing such as
remove` can be extended.
Just for a little debugging.
You can initialize a List using an anonymous class and an initialization block.
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
The above can be written as follows.
List<String> list = new ArrayList<String>() {{
add("1");
add("2");
add("3");
}};
Although the number of types is slightly reduced, it is not friendly to beginners, so it is not recommended to use it in team development (I do not think that this is usually done ...)
Recommended Posts