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 回答