您的位置:首页 > 编程语言 > Java开发

Java基础知识之集合操作

2017-03-26 18:10 567 查看
1.将数组转换为List

使用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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: