基础内容

1.set集合类型取交集/并集/差集

 1         String[] str1={"a","b","c","d","e"};
 2         String[] str2={"d","e","f","g","h"};
 3 
 4         Set<String> set1 = new HashSet<String>(Arrays.asList(str1));
 5         Set<String> set2 = new HashSet<String>(Arrays.asList(str2));
 6         Set<String> set = new HashSet<String>();
 7         System.out.println("集" + set2);
 8         // 并集
 9         set.addAll(set1);
10         set.addAll(set2);
11         System.out.println("并集" + set);
12 
13         // 交集
14         set.clear();
15         set.addAll(set1);
16         set.retainAll(set2);
17         System.out.println("交集" + set);
18         // 差集
19         set.clear();
20         set.addAll(set1);
21         set.removeAll(set2);
22         System.out.println("差集" + set);

2.给字符串中每两位字符加一个空格

1         String input = "abcdefghijk";
2         String regex = "(.{2})";
3         input = input.replaceAll (regex, "$1 ");
4         System.out.println (input);

 

上一篇:java Set接口


下一篇:算法-java实现