学习java.util包数据结构互相转换
一、Java中有几种常用的数据结构,主要分为Collection和map
首先学习Collection接口的实现类(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)以及数组之间的转化。
总结:
1.Collection的实现类(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)之间可以两两互相转换的,是通过向构造函数中传入其它数据结构实例实现的
2.数组转换为Collection接口的实现类实例(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)需要借助第三方类库org.apache.commons.collections4.CollectionUtils实现
3.Collection接口的实现类实例(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)转换为数组只需要借助它们自己的实例方法T[] toArray(T[] a)
相关代码实践:
List转set:
- String[] s = new String[]{“A”, “B”, “C”};
- List sList = Arrays.asList(s)
- Set result = new HashSet(sList);
数据转List:
- String[] s = new String[]{“A”, “B”, “C”};
- List sList = Arrays.asList(s);
----未完,持续更新中。。。。