A question on measure space and measurable function

First we use that $$ \int_X g\,\mathrm d\mu=\int_0^\infty \mu\left(\{g\geq t\}\right)\,\lambda(\mathrm dt) $$ for any non-negative measurable function $g$. Using this with $g=f^2$ we get $$ \int_X f^2\,\mathrm d\mu=\int_0^\infty \mu\left(\{f\geq \sqrt{t}\}\right)\,\lambda(\mathrm dt)=2\int_0^\infty x\cdot \mu\left(\{f\geq x\}\right)\,\lambda(\mathrm dx) $$ by change of variables with $x=\sqrt{t}$.

Now $x\mapsto x\cdot \mu\left(\{f\geq x\}\right)$ is measurable and $x\mapsto \mu\left(\{f\geq x\}\right)$ is decreasing, and so $$ \sum_{n=1}^\infty n\cdot \mu\left(\{f\geq n-1\}\right)1_{(n-1,n]}(x)\geq x\cdot \mu\left(\{f\geq x\}\right)\geq \sum_{n=1}^\infty (n-1)\cdot \mu\left(\{f\geq n\}\right)1_{(n-1,n]}(x) $$ and integrating this with respect to $\lambda$ yields $$ \int_0^\infty x\cdot \mu\left(\{f\geq x\}\right)\,\lambda(\mathrm d x)\leq \sum_{n=1}^\infty \left(\int_0^\infty n\cdot\mu\left(\{f\geq n-1\}\right)1_{(n-1,n]}(x)\,\lambda(\mathrm d x)\right)\\ =\sum_{n=1}^\infty n\cdot\mu\left(\{f\geq n-1\}\right)=\sum_{n=0}^\infty (n+1)\cdot\mu\left(\{f\geq n\}\right). $$ Now this last sum is finite if and only if $\sum_{n=1}^\infty n\cdot\mu\left(\{f\geq n\}\right)<\infty$ (by the limit comparison test).