最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

将HashSet转换成字符串

互联网 admin 0浏览 0评论

将HashSet转换成字符串

1)方法

/*** hashSet转换成字符串** @param hashSet* @return*/public String getStr(HashSet<String> hashSet) {List<String> list = new ArrayList<>();Iterator<String> iterator = hashSet.iterator();while (iterator.hasNext()) {list.add(iterator.next());}String str = "";for (int i = 0; i < list.size(); i++) {if (i == list.size() - 1) {str += list.get(i);} else {str = list.get(i) + ",";}}if (str.startsWith(",")) {str = str.substring(1);}return str;}

2)测试类

@Testpublic void test1(){HashSet<String> nameList = new HashSet<>();nameList.add("张三");
//        nameList.add("里斯");nameList.add("张三");System.out.println("转换前:"+nameList); //转换前:[张三]String str = getStr(nameList);System.out.println("转换后:"+str); //转换后:张三}

按需修改

将HashSet转换成字符串

1)方法

/*** hashSet转换成字符串** @param hashSet* @return*/public String getStr(HashSet<String> hashSet) {List<String> list = new ArrayList<>();Iterator<String> iterator = hashSet.iterator();while (iterator.hasNext()) {list.add(iterator.next());}String str = "";for (int i = 0; i < list.size(); i++) {if (i == list.size() - 1) {str += list.get(i);} else {str = list.get(i) + ",";}}if (str.startsWith(",")) {str = str.substring(1);}return str;}

2)测试类

@Testpublic void test1(){HashSet<String> nameList = new HashSet<>();nameList.add("张三");
//        nameList.add("里斯");nameList.add("张三");System.out.println("转换前:"+nameList); //转换前:[张三]String str = getStr(nameList);System.out.println("转换后:"+str); //转换后:张三}

按需修改

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论