集合类
集合类是特殊的类,用来存放其他类对象的容器,主要完成数据库和数据结构的功能,使程序更强大,简单,就相当于一个动态数组
Set List有一个共同的父类Collection
Collection常用方法(子类继承父类方法)
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo1 {
public static void main(String[] args) {
Collection c = new ArrayList();//Collection接口对象
System.out.println("集合不是空的 "+c.isEmpty()+"集合的长度 "+c.size());
c.add("你好");
c.add(3.14);
c.add(new Object());
System.out.println("集合不是空的 "+c.isEmpty()+"集合的长度 "+c.size());
c.remove(3.14);//删掉3.14
System.out.println("集合不是空的 "+c.isEmpty()+"集合的长度 "+c.size());
Iterator it = c.iterator();
while (it.hasNext()){
Object o =it.next();
System.out.println(o);
}
}
}
List
Java中表示列表,可以存放很多元素的队列,可以精确查找每个位置上的元素
ArrayList和linkedList的区别
添加元素:
ArrayList要把数组拆分强行插入数组中,如果数量大的话很消耗资源
linkedList在2和3之间加入一个三,排列顺序变为1-2-a-3
查找元素
ArrayList:为数组索引0
linkedList:根元素遍历临时给一个索引
import java.util.ArrayList;
import java.util.List;
public class Demo2 {
public static void main(String[] args) {
List list = new ArrayList();//实例化
//List list = new linkList();的方法完全通用,存储内部结构不同
//list可以插入同一个内容
list.add("清明时节雨纷纷");
list.add("孤家寡人欲断魂");
list.add("借问美女何处有");
list.add("牧童遥指三里屯");
// System.out.println(list.get(2));
// System.out.println(list.get(0));
list.remove(1);
list.add(1,"孤家寡人独享乐");
list.set(1,"借问女为何为尼");
list.add("清明时节雨纷纷");
list.add("借问美女何处有");
list.add("孤家寡人欲断魂");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
System.out.println("list的索引为 "+i+"的元素: "+list.get(i));
}
// System.out.println(list.get(2));//3 下标越界抛出异常
}
}
Set接口
不放重复的对象
HashSet散列集合
TreeSet树集合
存放有序
语法: Set set = new TreeSet();
Set接口提供方法
package set;
public class Person implements Comparable{
int id;
int age;
String name;
public Person(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Person{" +"id=" + id +", age=" + age +", name='" + name + ''' +'}';
}
@Override
public int compareTo(Object o) {
//三个返回值:负数,正数,0
Person p;
if (o instanceof Person){
p=(Person) o;
}else{
return -1;//-1代表传入的参数比我本身要小
}
int diff =this.age - p.age;
if (diff!=0){
diff =diff /Math.abs(diff);
}
return diff;
}//接口Cloneable
}
package set;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
Set set = new TreeSet();
Person p1 =new Person(1,18,"小明");
Person p2 =new Person(2,5,"大壮");
Person p3 =new Person(3,20,"阿强");
set.add(p1);
set.add(p2);
set.add(p3);
set.add(p3);//重复的元素不会加到集合中
//set.add(null);//tree集合不能保存null值
System.out.println(set.size());
Iterator it=set.iterator();//迭代器
while (it.hasNext()){
System.out.println(it.next());
}
}
}
package hashset;
import java.util.Objects;
public class Person {
int id;
String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person{" +"id=" + id +", name='" + name + ''' +'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return id == person.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
package hashset;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
//创建集合对象
Set set = new HashSet();
Person p1 = new Person(1,"小明");
Person p2 = new Person(2,"大壮");
Person p3 = new Person(3,"阿强");
set.add(p1);
set.add(p2);
set.add(p3);
//set.add(null);
//set.add(p3);不能有重复的元素
p2.id=5;
set.remove(p2);//id=5的hascode
//一个元素添加到集合中,最好不要修改,hascode值改变
p2.id=5;
set.add(p2);
System.out.println(set.size());
Iterator it =set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
Map集合
map
HashMap TreeMap
map m = new HashMap(); map m = new TreeMap();
package Map;
import java.util.*;
public class Demo {
public static void main(String[] args) {
Map map = new HashMap();
//Map map = new TreeMap();使用方法相同
map.put("String","这是一个字符串");
map.put("Object",new Object());
map.put(null,null);
map.put(1,1);
System.out.println("map中元素个数"+map.size());
Set set =map.keySet();//取出所有key值,存到Set集合
Iterator it=set.iterator();//迭代器,查看值
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println(map.get(1));
}
}
List结构集合类 ArrayList linkedList Map结构集合类 HashMap HashTable Set结构集合类 HashSet TreeSet Queue结构集合类和Queue接口



