0

だから私はかなり大きな文字列を持っていますが、それは大きくなるだけです。現在は 1,539 文字ですが、今後さらにいくつかのクエリを追加する予定です。そのため、最初に文字列を作成したとき、昔ながらの方法で 7 つの文字列オブジェクトを作成し、それらすべてをプラスで連結しました。

しかし、先生が昔言ったことを思い出し、それを切り替えてストリングビルダーを試すことにしました。唯一の問題は、これを使用することについてオンラインで見つけることができる唯一の本当の欠点は読みやすさであり、正直言って読みやすさはそれほど悪いとは思いません。

私の質問は、1500 人以上のキャラクターを収容するためのオブジェクトを構築することです。良いアイデアか悪いアイデアか。あなたが私の状況にいたとしたら、文字列、文字列ビルダー、または文字列バッファーを使用しますか? そして、可読性についてスピーチを保存してください。それは私のコードであり、それを読んでいるのは私だけであり、それほど難しいことではないので、変数として使用しないでください。ありがとうございました!

StringBuilder builder = new StringBuilder(1600);
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, stride.timestamp, stride.recipientView, \"stride\" as notType FROM user INNER JOIN stride ON user.id = stride.sourceUserId WHERE stride.recipientId = ? AND stride.sourceUserId != ? ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, strideLike.timestamp, strideLike.recipientView, \"strideLike\" as notType FROM user INNER JOIN strideLike ON strideLike.sourceUserId = user.id INNER JOIN stride ON stride.id = strideLike.strideId WHERE strideLike.recipientId = ? AND user.id != strideLike.recipientId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideComment.timestamp, strideComment.recipientView, \"strideCommentMe\" as notType FROM user INNER JOIN strideComment ON user.id = strideComment.sourceUserId INNER JOIN stride ON stride.id = strideComment.strideId WHERE stride.sourceUserId = ? AND user.id != stride.sourceUserId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideCommentLike.timestamp, strideCommentLike.recipientView, \"strideCommentLike\" AS notType FROM user INNER JOIN strideCommentLike ON strideCommentLike.sourceUserId = user.id INNER JOIN strideComment ON strideComment.id = strideCommentLike.commentId INNER JOIN stride ON stride.id = strideComment.strideId WHERE strideComment.sourceUserId = ? AND user.id != ? ");
builder.append(" ORDER BY timestamp DESC");
4

2 に答える 2

5

文字列に対して他の操作を行う必要がない場合は、文字列連結を使用する必要があります。文字列に追加される変数がない場合、文字列の連結はコンパイル時に行われます。

StringBuilder を使用すると、追加される変数があるかどうかに関係なく、実行時に連結が強制的に実行されます。

于 2013-03-04T06:32:24.327 に答える
0

StringBuilder実行時に文字列を生成している場合は、より効果的です。

これらのアプローチのいずれを使用しても違いはなく、コンパイラーが必要な最適化を行います。

StringBuilder builder = new StringBuilder(1600);
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, stride.timestamp, stride.recipientView, \"stride\" as notType FROM user INNER JOIN stride ON user.id = stride.sourceUserId WHERE stride.recipientId = ? AND stride.sourceUserId != ? ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, strideLike.timestamp, strideLike.recipientView, \"strideLike\" as notType FROM user INNER JOIN strideLike ON strideLike.sourceUserId = user.id INNER JOIN stride ON stride.id = strideLike.strideId WHERE strideLike.recipientId = ? AND user.id != strideLike.recipientId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideComment.timestamp, strideComment.recipientView, \"strideCommentMe\" as notType FROM user INNER JOIN strideComment ON user.id = strideComment.sourceUserId INNER JOIN stride ON stride.id = strideComment.strideId WHERE stride.sourceUserId = ? AND user.id != stride.sourceUserId ");
builder.append(" UNION ALL ");
builder.append("SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideCommentLike.timestamp, strideCommentLike.recipientView, \"strideCommentLike\" AS notType FROM user INNER JOIN strideCommentLike ON strideCommentLike.sourceUserId = user.id INNER JOIN strideComment ON strideComment.id = strideCommentLike.commentId INNER JOIN stride ON stride.id = strideComment.strideId WHERE strideComment.sourceUserId = ? AND user.id != ? ");
builder.append(" ORDER BY timestamp DESC");

String query = "SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, stride.timestamp, stride.recipientView, \"stride\" as notType FROM user INNER JOIN stride ON user.id = stride.sourceUserId WHERE stride.recipientId = ? AND stride.sourceUserId != ? " +
" UNION ALL " +
"SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, stride.content, strideLike.timestamp, strideLike.recipientView, \"strideLike\" as notType FROM user INNER JOIN strideLike ON strideLike.sourceUserId = user.id INNER JOIN stride ON stride.id = strideLike.strideId WHERE strideLike.recipientId = ? AND user.id != strideLike.recipientId " + 
" UNION ALL " +
"SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideComment.timestamp, strideComment.recipientView, \"strideCommentMe\" as notType FROM user INNER JOIN strideComment ON user.id = strideComment.sourceUserId INNER JOIN stride ON stride.id = strideComment.strideId WHERE stride.sourceUserId = ? AND user.id != stride.sourceUserId " +
" UNION ALL " +
"SELECT stride.id AS link, user.userName, user.displayName, user.currentDefault, strideComment.content, strideCommentLike.timestamp, strideCommentLike.recipientView, \"strideCommentLike\" AS notType FROM user INNER JOIN strideCommentLike ON strideCommentLike.sourceUserId = user.id INNER JOIN strideComment ON strideComment.id = strideCommentLike.commentId INNER JOIN stride ON stride.id = strideComment.strideId WHERE strideComment.sourceUserId = ? AND user.id != ? " + 
" ORDER BY timestamp DESC" +
于 2013-03-04T06:36:21.880 に答える