list集合
package list;
import java.util.ArrayList;
import java.util.List;
public class TestList {
public static void main(String[] args) {
List list =new ArrayList();
//添加
for (int i=1;i<=10;i++) {
list.add("a"+i);
}
System.out.print("遍历最初的集合");
for (String obj:list
) {
System.out.print(obj+"\t");
}
System.out.println();
boolean add1 = list.add("add_info");// //增加指定元素到链表尾部.
System.out.println("是否添加成功:"+add1);
System.out.print("添加指定元素到集合尾部在遍历一遍集合");
for (String obj:list
) {
System.out.print(obj+"\t");
}
System.out.println();
list.add(5,"assign_info");//增加指定元素到链表指定位置.
System.out.print("添加指定元素到指定的位置再遍历一遍集合");
for (String obj:list
) {
System.out.print(obj+"\t");
}
System.out.println();
System.out.println("将list2集合的元素全部添加到list集合中");
List list2 =new ArrayList();
//添加
for (int i=1;i<=10;i++) {
list.add("b"+i);
}
list.addAll(list2);
for (String obj:list
) {
System.out.print(obj+"\t");
}
System.out.println();
System.out.println("将list3集合的元素全部添加到list集合指定的位置中");
List list3 =new ArrayList();
for (int i=1;i<=10;i++) {
list.add("c"+i);
}
list.addAll(6,list3);
for (String obj:list
) {
System.out.print(obj+"\t");
}
//删除
System.out.println();
System.out.println("将list集合的元素从下标为10的元素删除");
list.remove(10);
for (String obj:list
) {
System.out.print(obj+"\t");
}
System.out.println();
System.out.println("将list集合中所有list3集合的元素删除");
list.removeAll(list3);
for (String obj:list
) {
System.out.print(obj+"\t");
}
System.out.println();
System.out.println("将list集合中的add_info元素删除");
list.remove("add_info");
for (String obj:list
) {
System.out.print(obj+"\t");
}
System.out.println();
System.out.println("将list集合的元素所有元素删除");
list.clear();
for (String obj:list
) {
System.out.print(obj+"\t");
}
//获取指定的元素
for (int i=1;i<=10;i++) {
list.add("a"+i);
}
System.out.println();
System.out.print("获取list集合中下标为2的元素");
String word1= list.get(2);
System.out.print(word1);
System.out.println();
System.out.print("获取list集合转换为数组");
Object[] array = list.toArray();
for (Object obj:array
) {
System.out.print(obj
+"\t");
}
System.out.println();
System.out.print("获取list集合中获取从5到8位置的元素");
List subList = list.subList(5,8);
for (String obj:subList
) {
System.out.print(obj+"\t");
}
//修改指定的元素
System.out.println();
System.out.print("修改list集合中下标为3的元素");
String update_info = list.set(3,"update_info");
for (String obj:list
) {
System.out.print(obj+"\t");
}
//判断是含有指定的元素
System.out.println();
System.out.print("判断list集合中是否含有指定的update_info元素");
boolean add2 = list.contains("update_info");
System.out.print(add2);
System.out.println();
System.out.print("判断list集合中update_info元素第一次出现的位置,如果没有该元素,返回-1");
int index = list.indexOf("update_info");
System.out.print(index);
System.out.println();
System.out.print("判断list集合中update_info元素最后一次出现的位置,如果没有该元素,返回-1");
int lastIndex = list.lastIndexOf("update_info");
System.out.print(lastIndex);
System.out.println();
System.out.print("判断list集合是否为空");
boolean empty = list.isEmpty();
System.out.print(empty);
System.out.println();
System.out.print("判断list集合的长度");
int size = list.size();
System.out.print(size);
}
}
set集合
import java.util.HashSet;
public class TestSet {
public static void main(String[] args) {
HashSet set =new HashSet<>();
//新增
for (int i=0;i<=9;i++){
set.add("a"+i);
}
System.out.print("set集合初始化数据");
System.out.println(set);
//删除
System.out.print("set集合中删除指定的a4元素");
set.remove("a4");
System.out.println(set);
//是否包含指定的元素
System.out.print("set集合中是否含有a2元素");
boolean con1 = set.contains("a2");
System.out.println(con1);
System.out.print("set集合中的长度");
int size = set.size();
System.out.println(size);
System.out.print("清空set集合");
set.clear();
System.out.print("判断set集合是否为空");
boolean empty = set.isEmpty();
System.out.println(empty);
}
}
Map集合
import java.util.HashMap;
public class TestMap {
public static void main(String[] args) {
System.out.print("数据初始化");
HashMap map =new HashMap<>();
for (int i=1;i<=5;i++){
map.put(i,"a"+i);
}
System.out.println(map);
System.out.print("取出map集合中的key为2的值");
System.out.println(map.get(2));
System.out.print("判断map集合中是否存在key=3的值");
System.out.println(map.containsKey(3));
System.out.print("判断map集合中是否存在value=a4的值");
System.out.println(map.containsValue("a4"));
System.out.print("删除map集合中的key为4的数据");
System.out.println(map.remove(4));
System.out.print("普通遍历map的方法");
for (Integer key:map.keySet()){
System.out.print(key+"="+map.get(key)+" ");
}
System.out.println();
System.out.print("修改map的中key为5的元素的值为update");
map.put(5,"update");
System.out.println(map);
}
}