[Java small story] Monitor when a value is added to the List

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.

by the way

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

[Java small story] Monitor when a value is added to the List
[Small story] I tried to make the java ArrayList a little more convenient
How to output the value when there is an array in the array
Replace with a value according to the match with a Java regular expression
[Java] How to search for a value in an array (or list) with the contains method
The story of forgetting to close a file in Java and failing
A story I was addicted to when testing the API using MockMVC
A story about the JDK in the Java 11 era
[Java] Various methods to acquire the value stored in List by iterative processing
Initialize Ruby array with 0 like Java, that is, set the default value to 0
A story about trying to operate JAVA File
A story confirming the implementation of the SendGrid Java library when mail delivery fails
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
(Java) How to implement equals () for a class with value elements added by inheritance
How to save a file with the specified extension under the directory specified in Java to the list
When inserting from Java to MySQL, get Auto_Increment_ID (automatic numbering value) as the return value
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
java: How to write a generic type list [Note]
A small story that is sometimes useful in Maven
[Java] How to get the maximum value of HashMap
[Java] When writing the source ... A memorandum of understanding ①
Null value is entered when assigning to an array
The story that .java is also built in Unity 2018
A story about taking an HTTP trace using Charles to find out what requests the Java library is making to Slack
How to perform a specific process when the back button is pressed in Android Fragment
How to solve the problem when the value is not sent when the form is disabled in rails and sent
When a Java file created with the Atom editor is garbled when executed at the command prompt
A story that ended up taking a break when using the Linked List with a light feeling
[Docker] Is it good enough to call it a multi-stage build? → The story that became so good
How to reduce the load on the program even a little when combining characters with JAVA
[Rails] What to do when the view collapses when a message is displayed with the errors method
Failed to create the Java Virtual Machine when installing Eclipse.
When is the default value automatically entered without explicit initialization?
(Small story) Numerical value → simple conversion method of alphabet (Java)
20190803_Java & k8s on Azure The story of going to the festival
I want to make a list with kotlin and java!
Create a method to return the tax rate in Java
A validation error occurred when saving to the intermediate table.
About the behavior when doing a file map with java
[Swift] Get the timing when the value of textField is changed
A memorandum to reach the itchy place for Java Gold
The story of pushing Java to Heroku using the BitBucket pipeline
A story about misunderstanding how to use java scanner (memo)
The story I was addicted to when setting up STS
The story of making a binding for libui, a GUI library for Ruby that is easy to install
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
9 Corresponds to the return value
An example of a small work when you want to divide the definition value according to the environment but do not want to be aware of it
What is a Java collection?
Java is the 5th day
Input to the Java console
A solution to the problem of blank spaces at the beginning of textarea
How to identify the path that is easy to make a mistake
A program that determines whether the entered integer is close to an integer
[Java small story] Monitor when a value is added to the List
How to change the value of a variable at a breakpoint in intelliJ
[Ubuntu 20.04] What to do if the external monitor is not recognized
When requested access to the resource is denied when pushing with Docker
A story that I struggled to challenge a competition professional with Java
When the character string passed to C ++ by JNA is garbled
A funny story stuck in a mess when trying to import fx-clj
Countermeasures for forgetting to specify the font when titleTextAttributes is specified
[Java] Adding an element to the Collection causes a compile error
[Java Servlet] Senri no Michi is one step to the second
Connecting to a database with Java (Part 1) Maybe the basic method