Java基础知识之集合操作
2017-03-26 18:10
567 查看
1.将数组转换为List
使用Arrays.asList 反之将集合转换为数组使用 list.toArray()
或者
2.将元素插入List的指定位置
3.将List反转
4.将List乱序
5.删除List中符合指定条件的元素
错误示例:
错误异常:java.util.ConcurrentModificationException 因为元素在使用的时候发生了并发的修改,导致异常抛出
可以换成以下方法
或者直接使用for循环
还可以使用迭代器删除指定数据
6.复制List
先创建list1
复制方式一:直接使用构造方法
7.Map集合遍历的方法
添加集合
方法一
方法二
方法三
方法四
使用Arrays.asList 反之将集合转换为数组使用 list.toArray()
String[] array = {"1","2","3","4","5"}; //Arrays.asList转List List<String> list = Arrays.asList(array);
或者
//遍历数组 往集合中添加数据 List list = new ArrayList<>(); for (int i = 0; i < array.length; i++) { list.add(array[i]); }
2.将元素插入List的指定位置
list.add(int index, Object element); //index 指定位置坐标; element 插入的数据
3.将List反转
Collections.reverse(list);
4.将List乱序
Collections.shuffle(list);
5.删除List中符合指定条件的元素
错误示例:
String[] array = { "1", "2", "3", "4", "5", "6" ,"1", "2", "3", "4", "5", "6"}; List<String> list = new ArrayList<String>(); for (int i = 0; i < array.length; i++) { //添加数据到集合中 list.add(array[i]); } for (String str : list) { if(str.equals("1")){ //删除集合中等于1 的数据 list.remove(str); } } System.out.println(list); }
错误异常:java.util.ConcurrentModificationException 因为元素在使用的时候发生了并发的修改,导致异常抛出
可以换成以下方法
//创建一个新的集合用来存储需要删除的数据 List<String> removeList = new ArrayList<String>(); for (String str : list) { if(str.equals("1")){ //往集合中添加需要删除的数据 removeList.add(str); } } //最后删除数据 if (removeList.size() > 0) { list.removeAll(removeList); }
或者直接使用for循环
for (int i = 0; i < list.size(); i++) { if (list.get(i).equals("1")) { list.remove(i); // 删除某个元素后,list的大小发生了变化,而索引也在变化, // 所以会导致在遍历的时候漏掉某些元素,即当前索引应该后退一位 i--; } }
还可以使用迭代器删除指定数据
Iterator<String> iterator = list.iterator(); while(iterator.hasNext()) { String str = iterator.next(); if (str.equals("1")) { //此处应该使用iterator.remove();而不是list.remove(str) iterator.remove(); } }
6.复制List
先创建list1
String[] array = { "1", "2", "3", "4", "5", "6", "1", "2", "3", "4", "5", "6" }; List<String> list1 = new ArrayList<String>(); for (int i = 0; i < array.length; i++) { // 添加数据到集合中 list1.add(array[i]); }
复制方式一:直接使用构造方法
List<String> list2 = new ArrayList<String>(list1);复制方式二:for循环添加
List<String> list2 = new ArrayList<String>(); for (int i = 0; i < list1.size(); i++) { list2.add(list1.get(i)); }复制方式三:List.addAll()方法
list2.addAll(list1);复制方法四: Java.util.ArrayList.clone()方法
list2 = (List<String>) ((ArrayList) list1).clone();复制方式五:System.araycopy()方法,集合转数组,复制后转集合,效率低
String[] array1 = new String[list1.size()]; System.arraycopy(list1.toArray(), 0, array1, 0, list1.size()); List<String> list2 =Arrays.asList(array1);
7.Map集合遍历的方法
添加集合
String[] array1 = { "1", "2", "3", "4", "5", "6" }; String[] array2 = { "a", "b", "c", "d", "e", "f" }; Map<String, String> map = new HashMap<>(); for (int i = 0; i < array1.length; i++) { map.put(array1[i], array2[i]); } System.out.println(map);
方法一
// 1.通过Map.entrySet使用iterator遍历key和value Iterator<Map.Entry<String, String>> integer = map.entrySet().iterator(); // integer.hasNext() 被迭代集合中如果还有下一个,返回true while (integer.hasNext()) { // integer.next() 返回集合里的下一个元素 Map.Entry<String, String> entry = integer.next(); // 得到key值 String key = entry.getKey(); // 得到value值 String value = entry.getValue(); }
方法二
// 2.通过Map.keySet遍历key和value for (String key : map.keySet()) { // map.keySet返回key的Set集合 // 通过 map.get(key)得到key值对应的value String value = map.get(key); }
方法三
// 3.在for-each循环中使用entries来遍历 for (Map.Entry<String, String> entry : map.entrySet()) { //map.entrySet()返回此映射中包含的映射关系的 Set 视图 String key = entry.getKey(); String value = entry.getValue(); }
方法四
//4.map.values() 返回value值对应的Collection for (String value : map.values()) { System.out.println(value); }
相关文章推荐
- Java基础知识强化之集合框架笔记77:ConcurrentHashMap之 ConcurrentHashMap的基本操作
- java基础知识记录--集合
- 【Java学习笔记】基础知识学习11【集合和列表类】
- java基础知识回顾之java集合类-Properties集合
- Java核心技术卷I:基础知识(原书第8版):13.5 遗留的集合
- java基础知识---集合
- Java:变量的赋值操作(祭奠我那悲催的基础知识)
- 黑马程序员_java基础知识总结(3)集合框架
- 黑马程序员--IO流和文件操作--java学习日记8(基础知识)
- 黑马程序员——Java集合基础知识之Map
- Java核心技术卷I:基础知识(原书第8版):13.2 具体的集合
- Java基础知识——集合体系回顾整理(List Set Map)
- java对集合和迭代的操作基础总结
- java基础知识6--集合框架,泛型,异常
- Java软件开发基础知识梳理之(11)------Java中的GC操作及相关概念
- 黑马程序员----------java基础知识加强之集合总概述
- 黑马程序员-Java基础知识预备之Java集合
- 黑马程序员学习log第五篇基础知识:JAVA的面向对象之集合总结
- Java核心技术卷I:基础知识(原书第8版):13.3 集合框架
- java基础知识回顾之javaIO类--内存操作流ByteArrayInputStream和ByteArrayOutputSteam(操作字节数组)