集合简介:
什么是集合(Collection)?集合就是“由若干个确定的元素所构成的整体”,在程序中,一般代表保存若干个元素(数据)的某种容器类。在数学中,我们经常遇到集合的概念。例如:
- 有限集合:
- 一个班所有的同学构成的集合;
- 一个网站所有的商品构成的集合;
- 无限集合:
- 全体自然数集合:1,2,3,……
- 有理数集合;
- 实数集合;
在Java中,如果一个Java对象可以在内部持有(保存)若干其他Java对象,并对外提供访问接口,我们把这种Java对象称为集合。很显然,Java的数组也可以看作是一种集合:
String[] ss = new String[10]; // 可以持有10个String对象
ss[0] = "Hello"; // 可以放入String对象
String first = ss[0]; // 可以获取String
既然Java提供了数组这种数据类型,可以充当集合,那么,我们为什么还需要其他集合类?这是因为数组有如下限制:
- 数组初始化后大小不可变;
- 数组只能按索引顺序存取;
因此,我们需要各种不同类型的集合类来处理不同的数据,例如:
- 可变大小的顺序链表;
- 保证无重复元素的集合;
分类:
单列集合(Collection<E>接口):
List接口:有索引值、可以重复、有序
ArrayList:底层是数组,特点,查询快 增删慢
LinkedList:底层是链表,特点,查询慢 增删快
Vector(较少使用):和ArrayList类似。
Vector:线程安全,效率低。
ArrayList:线程不安全,效率高
Set接口:没有索引值,不可以重复
HashSet(无序):底层是哈希表
LinkedHashSet(有序):底层是链表+哈希表
TreeSet(可排序):底层是红黑树
Hashtable:和HashSet类似。
Hashtable:线程安全,效率低。
HashSet:线程不安全,效率高
双列集合(Map):一个元素由K,V两部分组成
特点:
1.一个元素是有一个K,一个V两部分组成
2.K V可以是任意的引用数据类型
3.一个K对应唯一的一个V。K不能重复
常用实现类:
HashMap:底层是哈希表。无序
LinkedHashMap:底层是链表+哈希表。有序
TreeMap:底层是红黑树。可排序
public TreeMap();
public TreeMap(Comparator<K> c);
TreeMap新增元素的时候,K必须是Comparable类型(可比较类型)。
Hashtable:线程安全,效率低
HashMap:线程不安全,效率高
Collection<E>接口中的方法:
boolean add(E e);
boolean remove(Object obj);
boolean contains(Object obj);
boolean isEmpty();//是否有元素,有元素false,没有元素是true
int size();
void clear();
Object[] toArray();
Iterator<E> iterator();//Iterator接口
<T> T[] toArray(T[] arr);
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("张三");
c.add("李四");
c.add("王五");
System.out.println(c);
//删除元素
boolean b = c.remove("张三");
System.out.println(b);
b = c.remove("张三");
System.out.println(b);
System.out.println(c);
//判断集合有没有元素
b = c.isEmpty();
System.out.println(b);//false
//是否包含某个元素
b = c.contains("张三");
System.out.println(b);//false
b = c.contains("李四");
System.out.println(b);//true
int size = c.size();
System.out.println(size);//2
Object[] array = c.toArray();
System.out.println("--------"+Arrays.toString(array));
c.clear();//删除集合中的所有元素
System.out.println(c);
System.out.println(c==null);//false
}
Collection集合工具类:
static <T> boolean addAll(Collection<T> c,T... array);
static void shuffle(List<?> list);
static <T extends Comparable<T>> void sort(List<T> list);
static <T> void sort(List<T> list,Comparator<T> c);
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list, "aa","bb","cc","dd");
System.out.println(list);
List<Integer> list2 = new ArrayList<>();
Collections.addAll(list2, 10,50,20,80,45,67);
System.out.println(list2);
Collections.sort(list2);
System.out.println(list2);
}
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("张三",80));
list.add(new Student("张三疯了",70));
list.add(new Student("张三丰",90));
Collections.sort(list);
System.out.println(list);
}
}
class Student implements Comparable<Student>{
private String name;
private int score;
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
@Override
public int compareTo(Student o) {
//按照成绩进行升序排序
return this.score - o.score;
}
}
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("张三",80));
list.add(new Person("张三疯了",70));
list.add(new Person("张三丰",90));
Comparator<Person> c = new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o2.getName().length()-o1.getName().length();
}
};
Collections.sort(list,c);
System.out.println(list);
}
}
class Person{
private String name;
private int age;
public String getName() {
return name;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
List<E>接口中的方法:
void add(int index,E e);
E remove(int index);
E get(int index);
E set(int index,E e);
List<E> subList(int beginIndex,int endIndex);
int indexOf(Object o);
int lastIndexOf(Object o);
LinkedList<E>独有的成员方法:
void addFirst(E e);
void addLast(E e);
String removeFirst();
String removeLast();
String getFirst();
String getLast();
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("张三");
list.add("张三");
list.add("李四");
list.add("王五");
System.out.println(list.size());
System.out.println(list);
String element = list.get(3);
System.out.println(element);//王五
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
list.add(0, "aaa");
System.out.println(list);
String removeElement = list.remove(0);
System.out.println("被删除的元素是:"+removeElement);//被删除的元素是:aaa
System.out.println(list);
String element = list.get(0);
System.out.println(element);//aa
String replaceElement = list.set(0, "aaa");
System.out.println("被替换的元素是:"+replaceElement);//被替换的元素是:aa
System.out.println(list);
List<String> list2 = list.subList(1, 3);//左闭右开
System.out.println(list2);
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
list.addFirst("aaa");
list.addLast("eee");
System.out.println(list);
String first = list.removeFirst();
System.out.println(first);//aaa
String last = list.removeLast();
System.out.println(last);//eee
System.out.println(list);
first = list.getFirst();
System.out.println(first);//aa
last = list.getLast();
System.out.println(last);//ee
}
ArrayList<E>:存储多个同一类型的数据
数组:数组的长度在运行期间不能改变
ArrayList:底层是数组,封装了数组,提供了更多的功能(方法)。
所属包:java.util;
构造方法:
public ArrayList();
成员方法:
//返回值表示新增是否成功
boolean add(E e);//E表示什么类型:创建对象的时候,尖括号中写的是什么类型,E就是什么类型
E get(int index);
E remove(int index);//根据索引值删除元素,返回被删除的元素
boolean remove(Object obj);//删除被参数元素,返回删除是否成功
int size();//获取元素的个数
//如果要找的元素不存在,则返回-1
int indexOf(Object obj);//获取元素第一次出现的索引值位置
int lastIndexOf(Object obj);//获取元素最后一次出现的索引值位置
E set(int index,E newElement);//把index位置的元素替换为newElement,返回被替换的元素
boolean contains(Object obj);//是否包含 参数元素,包含返回true,反之false
/*
* 创建ArrayList类型的对象
* ArrayList类里面做了一些特殊操作,使得打印对象名的时候不是地址值。
*/
ArrayList list = new ArrayList();
list.add(10);
list.add(10.5);
list.add("张三");
System.out.println(list);
public static void main(String[] args) {
/*
* 类名尖括号里面的这个类型,我们叫做泛型,就是限定添加元素的类型
*/
ArrayList<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
System.out.println(list);
String element = list.get(1);
System.out.println("获取的元素是:"+element);//李四
String removeElement = list.remove(0);
System.out.println("被删除的元素是:"+removeElement);
System.out.println("删除后:");
System.out.println(list);
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
System.out.println(list);
//删除成功,返回true,失败(该元素不存在)返回false
boolean b = list.remove("王五");
System.out.println(b);
System.out.println(list);
//再次删除"王五"
b = list.remove("王五");
System.out.println(b);
}
public static void main(String[] args) {
/*
* 类名尖括号里面的这个类型,我们叫做泛型,就是限定添加元素的类型
*/
ArrayList<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
//获取元素的个数
int size = list.size();
//循环
for (int i = 0; i < size; i++) {
//获取元素
String element = list.get(i);
System.out.println(element);
}
}
public static void main(String[] args) {
/*
* 类名尖括号里面的这个类型,我们叫做泛型,就是限定添加元素的类型
*/
ArrayList<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("张三");
list.add("王五");
//获取元素第一次出现的索引值位置
int index = list.indexOf("张三");
System.out.println(index);//0
//获取元素最后一次出现的索引值位置
index = list.lastIndexOf("张三");
System.out.println(index);//2
//如果该元素不存在,则返回-1
index = list.indexOf("张三丰");
System.out.println(index);//-1
}
public static void main(String[] args) {
/*
* 类名尖括号里面的这个类型,我们叫做泛型,就是限定添加元素的类型
*/
ArrayList<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("张三");
list.add("王五");
//替换对应位置的元素
String replaceElement = list.set(2, "张三丰");
System.out.println("被替换的元素是:"+replaceElement);
System.out.println(list);
boolean b = list.contains("张三丰丰");
System.out.println(b);//true
}
Set集合
TreeSet:红黑树
构造方法:
public TreeSet();
public TreeSet(Comparator<> c);
TreeSet新增元素的时候,元素的类型必须是Comparable类型
如果新增的元素不是Comparable类型则需要:创建一个Comparator类型的对象,传入到TreeSet
public static void main(String[] args) {
//Set<String> set = new HashSet<>();
Set<String> set = new LinkedHashSet<>();
boolean b = set.add("张三");
System.out.println(b);//true
b = set.add("张三");
System.out.println(b);//false
System.out.println(set.size());
set.add("李四");
set.add("王五");
System.out.println(set);
Set<String> set2 = new HashSet<>();
set2.add("aa");
set2.add("bb");
set2.add("cc");
set2.add("dd");
set2.add("ee");
System.out.println(set2);
}
Set<String> set = new HashSet<>();
//String覆盖重写了Object父类的hashCode方法,只要内容相同,哈希值就相同。
set.add(new String("张三"));
set.add(new String("张三"));
System.out.println(set.size());//1
Set<Student> set2 = new HashSet<>();
set2.add(new Student("张三",20));
set2.add(new Student("张三",20));
System.out.println(set2.size());//2
}
}
class Student{
private String name;
private int age;
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
//如果hashcode方法,认为成员变量 的值相同,则返回相同的结果
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
set.add(10);
set.add(50);
set.add(20);
set.add(40);
set.add(30);
System.out.println(set);
//TreeSet新增元素的时候,泛型必须是Comparable类型
Set<Person> set2 = new TreeSet<>();
set2.add(new Person("张三"));
set2.add(new Person("张三疯了"));
set2.add(new Person("张三小"));
System.out.println(set2);
}
}
class Person implements Comparable<Person>{
private String name;
public Person(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
/*
* 主要要说比较的规则
* 升序:当前对象-参数对象
* 降序:参数对象-当前对象
*/
@Override
public int compareTo(Person o) {
//升序
//int result = this.name.length()-o.name.length();
//降序
int result = o.name.length()-this.name.length();
return result;
}
}
Map集合常用方法:
V put(K k,V v);//如果K存在,则新的V替换旧的V,返回被替换的V。
//如果K不存在,则返回null
V remove(Object key);//删除元素,根据key删除整个元素,返回被删除元素的V
//如果Key不存在,则返回null
V get(Object key);//根据key获取V,如果key不存在则返回null
boolean containsKey(Object key);
boolean containsValue(Object value);
Set<K> keySet();//获取所有的key
Set<Entry<K,V>> entrySet();
Collection<V> values();
boolean isEmpty();
void clear();
int size();//获取集合中元素的个数
public static void main(String[] args) {
Map<String,Double> map = new HashMap<>();
Double v = map.put("张三", 90.0);
System.out.println(v);//null
v = map.put("张三丰", 99.0);
System.out.println(v);//null
v = map.put("张三丰", 99.9);
System.out.println(v);//99.0
System.out.println(map);
int size = map.size();
System.out.println(size);//2
//删除元素,根据key删除整个元素
v = map.remove("张三");
System.out.println(v);//90.0
System.out.println(map);
v = map.remove("张非");
System.out.println(v);//null
//获取
v = map.get("张三丰");
System.out.println(v);//99.9
v = map.get("张飞");
System.out.println(v);//null
//判断key是否存在
boolean b = map.containsKey("张三");
System.out.println(b);
b = map.containsKey("张三丰");
System.out.println(b);
//判断V是否存在
b = map.containsValue(99.0);
System.out.println(b);//false
b = map.containsValue(99.9);
System.out.println(b);//true
//判断集合是否为空
b = map.isEmpty();
System.out.println(b);//false
//清空元素
map.clear();
b = map.isEmpty();
System.out.println(b);//true
System.out.println(map==null);//false
}
public static void main(String[] args) {
Map<String,Double> map = new HashMap<>();
map.put("张三", 90.0);
map.put("张三丰", 99.0);
map.put("张三疯了", 90.0);
/*
* 把Map双列集合转换为单列集合
*/
//获取所有的V
Collection<Double> values = map.values();
System.out.println(values);
//获取所有的K
Set<String> set = map.keySet();
System.out.println(set);
for(String key:set) {
//根据key获取V
Double v = map.get(key);
System.out.println(key+"--"+v);
}
}
public static void main(String[] args) {
Map<String,Double> map = new HashMap<>();
map.put("张三", 90.0);
map.put("张三丰", 99.0);
map.put("张三疯了", 90.0);
//获取所有的Entry
Set<Entry<String,Double>> entrySet = map.entrySet();
Iterator<Entry<String,Double>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String,Double> entry = iterator.next();
String key = entry.getKey();
Double value = entry.getValue();
System.out.println(key+"--"+value);
}
}
public static void main(String[] args) {
Map<Student,Double> map = new HashMap<>();
map.put(new Student("张三丰","yjy0010"), 80.0);
map.put(new Student("张三丰","yjy0010"), 90.0);
System.out.println(map.size());//
System.out.println(map);
}
}
class Student{
private String name;
private String stuNo;
public String getName() {
return this.name;
}
public Student(String name, String stuNo) {
super();
this.name = name;
this.stuNo = stuNo;
}
@Override
public String toString() {
return "Student [name=" + name + ", stuNo=" + stuNo + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((stuNo == null) ? 0 : stuNo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (stuNo == null) {
if (other.stuNo != null)
return false;
} else if (!stuNo.equals(other.stuNo))
return false;
return true;
}
}
public static void main(String[] args) {
//Map<Student,Double> map = new HashMap<>();
//Map<Student,Double> map = new LinkedHashMap<>();
Comparator<Student> c = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().length()-o2.getName().length();
}
};
Map<Student,Double> map = new TreeMap<>(c);
map.put(new Student("张三丰","yjy0010"), 80.0);
map.put(new Student("张三","yjy0020"), 90.0);
map.put(new Student("张三疯了","yjy0030"), 90.0);
System.out.println(map);
}