我知道这是一个新手问题,但在Java中C#的字符串操作是否相同?
具体来说,我正在谈论String.Format和String.Join.
我有一个java集
private Set<String> packageCategories;
Run Code Online (Sandbox Code Playgroud)
它的值为 ['Abc','Def']。我想在 UI 中显示 packageCategories 的值,该值现在显示为 ['Abc','Def'],但我想将其简单地显示为 'Abc', 'Def'。
我试过
String.join(",", packageCategories);
context.put("packageCategories", packageCategories);
Run Code Online (Sandbox Code Playgroud)
我收到编译错误
incompatible types: java.util.Set<java.lang.String> cannot be converted to java.lang.String.
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这个目标?我在 StackOverflow 中搜索,它有将 List 转换为逗号分隔字符串的答案。但我没有找到任何将 Set 转换为逗号分隔字符串的答案。
以最快的方式将 Set<String> 的内容放入单个字符串并用空格分隔单词中给出的答案?对我不起作用。
PS:我不是JAVA开发人员。
我想将整数的哈希集转换为逗号分隔的字符串,
所以我可以在 MySQL 查询的 where 子句中使用相同的内容。
//[MySQL - Sample table Schema]
my_table
state_id INTEGER
shop_id INTEGER
Run Code Online (Sandbox Code Playgroud)
Set<Integer> uniqueShopIds = shopService.GetShopIds(); // This returns HashSet<Integer>
String inClause = ; // **How do i convert the uniqueShopIds to comma separated String**
String selectQuery = String.format("SELECT DISTINCT(state_id) FROM my_table WHERE shop_id IN (%s);", inClause);
Run Code Online (Sandbox Code Playgroud)
如果还有其他方法,我可以直接在PreparedStatment的IN CLAUSE中使用HashSet,请分享。