多语言展示
当前在线:576今日阅读:168今日分享:49

java中List、Array、Map、Set等集合相互转

在java中,我们经常需要对List、Array等做一些转换操作,当然转换方法有很多种,但哪种方法既方便又高效呢?在这里向大家介绍一下集合间的最佳转换方法。
方法/步骤
1

List转换为ArrayList list = new ArrayList<>();list.add('AAAA');list.add('BBBB');list.add('CCCC');list.add('DDDD');String [] array = list.toArray(new String[list.size()]);

2

Array转换为ListString[] countries = {'AAAA', 'BBBB', 'CCCC', 'DDDD'};List list = Arrays.asList(countries);

3

Map的Key值转换为ListMap map = new HashMap<>();map.put(1,'AAAA');map.put(2,'BBBB');map.put(3,'CCCC');map.put(4,'DDDD');List list = new ArrayList(map.keySet());

4

Map的Value值转换为ListMap map = new HashMap<>();map.put(1,'AAAA');map.put(2,'BBBB');map.put(3,'CCCC');map.put(4,'DDDD');List list = new ArrayList(map.values());

6

Map的Key值转换为SetMap map = new HashMap<>();map.put(1,'AAAA');map.put(2,'BBBB');map.put(3,'CCCC');map.put(4,'DDDD');Set set = new HashSet<>(map.keySet());

7

Map的Value值转换为SetMap map = new HashMap<>();map.put(1,'AAAA');map.put(2,'BBBB');map.put(3,'CCCC');map.put(4,'DDDD');Set set = new HashSet(map.values());

推荐信息