在这之前先说下List去重复:
public static void removeDuplicate(ArrayList arlList) {
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
}
public static void removeDuplicateWithOrder(ArrayList arlList) {
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = arlList.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
arlList.clear();
arlList.addAll(newList);
}
看了List去重复后再理解之间的互转就明白了,下面之间的互转:
1.list转set
Set set = new HashSet(new ArrayList());
List list = new ArrayList(new HashSet());
3.数组转为list
List stooges = Arrays.asList("Larry", "Moe", "Curly");
4.数组转为set
Set set = new HashSet(Arrays.asList(new String[]{"1","2","3"}));
5.list转数组
String[] arr = (String[])list.toArray(new String[list.size()]);
6.map的相关操作
Map map = new HashMap();
map.put("1", "a");
map.put('2', 'b');
map.put('3', 'c');
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
图一
图二