1 ArrayList
定义一个ArrayList集合 List list=new ArrayList(); List list2=new ArrayList(); |
1.1增
list.add("Hello"); list.add("World"); list.addAll(list2); |
1.2删
list.remove(1); //list.clear(); |
1.3改
list.set(0,"Java"); |
1.4查
Object o=list.get(0); System.out.println(o); |
2 LinkedList
定义一个LinkedList集合 List list =newLinkedList(); |
2.1增
list.add("Hello"); list.add("张三"); list.addAll(list2); |
2.2删
list.remove(1); list.removeFirst(); list.removeLsat(); //list.clear(); |
2.3改
list.set(0,"Java"); |
2.4查
System.out.println(list.get(1)); System.out.println(list.getFirst()); System.out.println(list.getLast(); |
3 HashSet
创建HashSet集合 HashSet hasSet=new HashSet(); |
3.1增
hashSet.add("java01"); System.out.println(hashSet); HashSet set2=new HashSet(); hashSet.addAll(set2); |
3.2删
hashSet.remove("java02"); //hashSet.clear();//清空集合容器的内容 |
3.3查
System.out.println(hashSet.isEmpty()); System.out.println(hashSet.contains("刘德华")); |
3.4HashSet的遍历
======foreach遍历======= for(Object o: hashSet){ System.out.println(o); } ======通过迭代器来遍历======= Iterator iterator=hashSet.iterator(); while(iterator.hasNext()){ // System.out.println(iterator.next()); Object next = iterator.next();//指定移动并获取当前的元素 } |
4TreeSet
TreeSet 基于TreeMap 实现。TreeSet可以实现有序集合,但是有序性需要通过比较器实现。 创建一个TreeSet集合 TreeSet treeSet=new TreeSet(); |
4.1增
treeSet.add("java05"); |
4.2存储对象类型
public class Test{ public static void main(Stirng[] args){ TreeSet treeSet=new TreeSet();
treeSet.add(new Student("李AA",20)); System.out.println(treeSet); } } class Student implements Cpmparable{
private String name; private Integer age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return this.name+" "+this.age; } @Override public int compareTo(Object o) { Student stu= (Student) o; if(this.age> stu.age || !this.name.equals(stu.name)){ return 1; }else if(this.age< stu.age || !this.name.equals(stu.name)){ return -1; } return 0; } } |
5HashMap
创建HashMap集合(Key,Value) Key是唯一的 Map map=new HashMap(); Map map2=new HashMap(); |
5.1增
map.put("k1","v1"); map.put("k2","v2"); map.put("name","张三"); map2.put("a1","b1"); map2.put("a2","b2"); map.putAll(map2); |
5.2删
map.remove("k2"); //map.clear();清空map |
5.3改
map.replace("name","李四"); //map.put("name","李四"); |
5.4查
map.containsKey("name");//返回值为true/false map.containsValue("张三"); map.get("k1"); //遍历map集合 Set set=map.keySet(); for(Object obj:set){ System.out.println(obj+"---->"+map.get(obj)); } |
集合的通用方法
.isEmpty() //判断list集合是否为空,返回true/false .contains("Java") //判断list集合中是否包含该内容,返回true/false |
6泛型
6.1 案例1
package com.edu.day0419.Test02_2; /** * @Author Li Qinghua * @Create 2022/4/19 20:18 */ public class Student<String,Integer>{ private String name; private Integer age; public Student() { } public void show(){ System.out.println(this.getName()+" "+this.getAge()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Student(String name, Integer age) { this.name = name; this.age = age; } } public static void main(String[] args) { Student<String,Integer> stu=new Student<>("张三",20); stu.show(); Student stu2=new Student<String,Integer>(); stu2.setAge(30); stu2.setName("李四"); stu2.show(); } |
6.2 案例2
package com.edu.day0419.Test02; /** * @Author Li Qinghua * @Create 2022/4/19 14:32 */ public class Point<E> { private E x; private E y; public void show(){ System.out.println("坐标为:"+"("+getX()+","+getY()+")"); } public E getX() { return x; } public void setX(E x) { this.x = x; } public E getY() { return y; } public void setY(E y) { this.y = y; } } public static void main(String[] args) { Point<Integer> p1=new Point<>(); p1.setX(5); p1.setY(263); p1.show(); } |