-
刚学Java不久的时候,接到一个电面,然后问了一些java的知识,比如说Java的编码,Unicode等,但是最让我蛋疼的是怎么吗map转为set,那个时候对集合用的很少,对集合不是特别了解,map还知道,set就蒙了,然后转为set更蒙了,觉得应该有API提供吧,但是不知道怎么说。后来我一直下来再查这个问题,查到了,但是没有实践过,今天我就来一发代码。
List转Set
1.
Set set =
new
HashSet(
new
ArrayList());
Set转List
1.
List list =
new
ArrayList(
new
HashSet());
数组转为List
1.
List arr = Arrays.asList(
"1"
,
"2"
,
"3"
);
2.
//或者
3.
String[] arr = {
"1"
,
"2"
};
4.
List list = Arrays.asList(arr);
数组转为Set
1.
int
[] arr = {
1
,
2
,
3
};
2.
Set set =
new
HashSet(Arrays.asList(arr));
Map的值转化为List
1.
List list =
new
ArrayList(map.values());
Map的值转化为Set
1.
Set set =
new
HashSet(map.values());
List转数组
1.
List list = Arrays.asList(
"a"
,
"b"
);
2.
String[] arr = (String[])list.toArray(
new
String[list.size()]);
代码
01.
public
class
listsetmao {
02.
03.
private
static
List<String> arrayList;
04.
private
static
Map<String, String> hashMap;
05.
private
static
Set<String> hashSet;
06.
private
static
String[] arr = {
"11oneone"
,
"22twotwo"
};
07.
08.
public
static
void
main(String[] args) {
09.
/*
10.
* //list转set initList(); Set<String> set = new
11.
* HashSet<String>(arrayList);
12.
* System.out.println("arrayList.toString()--->"+set.toString());
13.
* System.out.println("set.toString()--->"+set.toString());
14.
*/
15.
/*
16.
* //set转list initSet(); List<String> list = new
17.
* ArrayList<String>(hashSet);
18.
* System.out.println("hashSet.toString()--->"+hashSet.toString());
19.
* System.out.println("list.toString()--->"+list.toString());
20.
*/
21.
/*
22.
// 数组转为list
23.
List<String> list = Arrays.asList(arr);
24.
System.out.println("list.toString()--->"+list.toString());
25.
*/
26.
/*
27.
//数组转set
28.
Set set = new HashSet<>(Arrays.asList(arr));
29.
System.out.println("set.toString()--->"+set.toString());
30.
*/
31.
/*
32.
//map的值转为list
33.
initMap();
34.
List<String> list = new ArrayList<String>(hashMap.values());
35.
System.out.println("list.toString()--->"+list.toString());
36.
*/
37.
/*
38.
//map的值转为set
39.
initMap();
40.
Set<String> set = new HashSet<String>(hashMap.values());
41.
System.out.println("set.toString()--->"+set.toString());
42.
*/
43.
/*
44.
//map的key转为set
45.
initMap();
46.
Set<String> set = new HashSet<String>(hashMap.keySet());
47.
System.out.println("set.toString()--->"+set.toString());
48.
*/
49.
//list转数组
50.
initList();
51.
String[] arr1 = (String[])arrayList.toArray(
new
String[arrayList.size()]);
52.
System.out.println(
"Arrays.toString(arr1)--->"
+Arrays.toString(arr1));
53.
}
54.
55.
public
static
void
initList() {
56.
arrayList =
new
ArrayList<String>();
57.
arrayList.add(
"1"
);
58.
arrayList.add(
"2"
);
59.
arrayList.add(
"3"
);
60.
arrayList.add(
"4"
);
61.
}
62.
63.
public
static
void
initMap() {
64.
hashMap =
new
HashMap<String, String>();
65.
hashMap.put(
"one"
,
"one1"
);
66.
hashMap.put(
"two"
,
"two2"
);
67.
hashMap.put(
"three"
,
"three3"
);
68.
}
69.
70.
public
static
void
initSet() {
71.
hashSet =
new
HashSet<String>();
72.
hashSet.add(
"1one"
);
73.
hashSet.add(
"2two"
);
74.
hashSet.add(
"3three"
);
75.
hashSet.add(
"4four"
);
76.
hashSet.add(
"5five"
);
77.
}
78.
79.
80.
}
我是天王盖地虎的分割线
源代码:http://pan.baidu.com/s/1dD1Qx01
listsetmap.zip
转载请注明出处:http://www.cnblogs.com/yydcdut
List,Set,Map,Array之间相互转换
最新推荐文章于 2022-06-08 09:18:12 发布