Most existing lossless data hiding (LDH) methods for JPEG bitstream embed data by constructing the code mapping between used codes and unused codes. However, previous studies only apply to the JPEG bitstream encoded with the standard Huffman table. For the JPEG bitstream encoded with the optimized Huffman table, all the codes are used, so the traditional code mapping strategy does not work. With this concern in mind, we propose a generic LDH method that is applicable to any JPEG bitstream by using a table-independent code mapping strategy, in which the mapped codes are redefined by customizing a Huffman table rather than selected from the unused codes in the original bitstream. Furthermore, we formulate the code mapping construction as a combinatorial optimization problem. The optimization problem can be solved by using some optimization algorithms. For example, in this paper, we use the genetic algorithm to search for the nearly optimal solution. Consequently, the high embedding capacity can be achieved, and the file-size increment in the marked bitstream is also well alleviated. Finally, we embed additional data and modify the file header to guarantee that the marked bitstream is compatible with popular JPEG decoders. Experimental results demonstrate that the proposed method significantly outperforms state-of-the-art methods.