Most existing lossless data hiding (LDH) methods for JPEG bitstream embed data by constructing the code mapping between used codes and unused codes. Previous studies only apply to the JPEG bitstream encoded with the standard Huffman table, in which unused codes usually exist hence the construction of code mapping can be guaranteed. However, for the JPEG bitstream encoded with the optimized Huffman table, the codes are all used so code mapping is out of work. With this concern in mind, we aim to propose a generic LDH method applicable to any JPEG bitstream. To this end, in this paper, we first design a code mapping scheme named the table-irrelevant code mapping (TCM), in which the mapped codes are generated rather than selected from the unused codes in the original bitstream so versatility is achieved. Then, We explicitly formulate the construction of TCM as a combinatorial optimization problem and subsequently solve the problem by leveraging the genetic algorithm. Consequently, the high embedding capacity can be achieved and the file size expansion in the marked bitstream is also well alleviated. Finally, we embed additional data and modify the file header to guarantee the marked bitstream is compatible with popular JPEG decoders. Experimental results demonstrate that the proposed method significantly outperforms state-of-the-art methods.