集合主要分为两组(单列集合,双列集合)
单列集合:存放单个元素
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("jack");
arrayList.add("tom");
双列集合:键值对的形式存元素K-V
HashMap<String, Object> hashMap = new HashMap<>(64);
hashMap.put("NO1", "北京");
hashMap.put("NO2", "上海");
/**
* @author java小豪
* @date 2022/6/8
*/
public class CollectionMethod {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
// add:添加单个元素
list.add("jack");
// list.add(new Integer(10))
list.add(10);
list.add(true);
System.out.println("List=" + list);
// remove:删除指定元素
// 通过下标删除元素
list.remove(0);
// 指定删除的元素
list.remove(true);
System.out.println("List=" + list);
// contains:查找元素是否存在
System.out.println(list.contains("jack"));
// size:获取元素的个数
System.out.println(list.size());
// isEmpty:判断集合是否为空
System.out.println(list.isEmpty());
// clear:清空集合
//list.clear();
//System.out.println(list);
// addAll:添加多个元素
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add("红楼梦");
arrayList.add("三国演义");
list.addAll(arrayList);
System.out.println(list);
// containsAll:查找多个元素是否都存在
System.out.println(list.containsAll(arrayList));
// removeAll:删除多个元素
list.add("聊斋");
list.removeAll(arrayList);
System.out.println(list);
}
}
/**
* @author java小豪
* @date 2022/6/8
*/
public class CollectionIterator {
public static void main(String[] args) {
Collection<Object> collection = new ArrayList<>();
collection.add(new Book("三国演义", "罗贯中", 50));
collection.add(new Book("小李飞刀", "古龙", 70.0));
collection.add(new Book("红楼梦", "曹雪芹", 60.0));
// System.out.println("collection = " + collection);
// 得到迭代器
Iterator<Object> iterator = collection.iterator();
// 使用while循环遍历集合
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println("obj = " + obj);
}
// 重置迭代器
iterator = collection.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println("obj = " + obj);
}
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
增强for循环:只能用来遍历集合和数组
/**
* @author java小豪
* @date 2022/6/8
*/
public class CollectionFor {
public static void main(String[] args) {
Collection<Object> collection = new ArrayList<>();
collection.add(new Book("三国演义", "罗贯中", 50));
collection.add(new Book("小李飞刀", "古龙", 70.0));
collection.add(new Book("红楼梦", "曹雪芹", 60.0));
// 增强for
// 增强for底层仍然是迭代器
for (Object book : collection) {
System.out.println("book = " + book);
}
// 增强for用在数组上
int[] nums = {1, 6, 7, 9, 10};
for (int i : nums) {
System.out.println("i = " + i);
}
}
}
List集合类中元素有序、且可重复
List集合中的每个元素有其对应的顺序索引,即支持索引。
List容器中的元素对应一个整数的序号记载在容器中的位置,可以根据序号取出容器中的元素.
/**
* @author java小豪
* @date 2022/6/9
*/
public class List_ {
public static void main(String[] args) {
// List集合类中元素有序、且可重复
List<Object> list = new ArrayList<>();
list.add("jack");
list.add("mary");
list.add("tom");
list.add("chen");
System.out.println("list = " + list);
// List集合中的每个元素有其对应的顺序索引,即支持索引。
System.out.println(list.get(2));
}
}
/**
* @author java小豪
* @date 2022/6/9
*/
public class ListMethod {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
//void add(int index, Object ele):在index位置插入ele元素
list.add("张三丰");
list.add("李小龙");
// 在index = 1 的位置插入一个对象
list.add(1, "宋江");
System.out.println("list = " + list);
//boolean addAll(int index, Collection eles):从index位置开始将eles中的元素插入
List<Object> list2 = new ArrayList<>();
list2.add("jack");
list2.add("tom");
list.addAll(1, list2);
System.out.println("list = " + list);
//Object get(int index):获取指定index位置的元素
System.out.println(list.get(3));
//int indexOf(Object obj):返回obj在当前集合中首次出现的位置
System.out.println(list.indexOf("tom"));
//int lastIndexOf(Object obj):返回obj在当前集合中末次出现的位置
list.add("tom");
System.out.println("list = " + list);
System.out.println(list.lastIndexOf("tom"));
//Object remove(int index):移除指定index位置的元素,并返回此元素
list.remove(0);
System.out.println("List = " + list);
//Object set(int index, Object ele):设置指定index位置元素为ele,相当于是替换
list.set(1, "玛丽");
System.out.println("List = " + list);
// List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
// 返回子集合范围[fromIndex, toIndex)
List returnList = list.subList(0, 2);
System.out.println("returnList = " + returnList);
}
}
/**
* @author java小豪
* @date 2022/6/10
*/
public class ListExercise {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
// 添加十个以上的 String "hello"
for (int i = 0; i < 12; i++) {
list.add("hello" + i);
}
System.out.println("list = " + list);
// 在2号位插入一个元素"chen"
list.add(1, "chen");
System.out.println("list = " + list);
// 获取第6个元素
System.out.println("第6个元素 = " + list.get(5));
// 删除第7个元素
list.remove(6);
System.out.println("list = " + list);
// 修改第8个元素
list.set(7, "三国演义");
System.out.println("list = " + list);
// 使用迭代器遍历集合
Iterator<Object> iterator = list.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println("obj = " + obj);
}
}
}
/**
* @author java小豪
* @date 2022/6/10
*/
public class ListFor {
public static void main(String[] args) {
// List 接口的实现子类 Vector LinkedList
// List