集合.Collection接口详解
一、常用方法
- boolean add(E e)
确保此集合包含指定的元素(可选操作)。 - int size()
返回此集合中的元素数。 - void clear()
从此集合中删除所有元素(可选操作)。 - Object[ ] toArray()
返回一个包含此集合中所有元素的数组。 - boolean isEmpty()
如果此集合不包含元素,则返回 true 。 - Iterator iterator()
返回此集合中的元素的迭代器。
通过迭代器中方法完成集合的迭代(遍历)。 -
在之前的这张图中左上角部分描述过迭代器
注意:这种方式是所有的集合通用的便利方式。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
public class CollectionTest2 {
public static void main(String[] args) {
//创建集合
Collection<Comparable> c = new LinkedList<>();
//Collection<Comparable> c = new ArrayList<>();
c.add(10);
c.add(3.14);
c.add(true);
c.add("chwen");
//创建迭代器对象
Iterator<Comparable> i = c.iterator();
System.out.println(i);
//java.util.AbstractList$Itr是ArrayList集合所依赖的迭代器
//java.util.LinkedList$ListItr是LinkedList集合所依赖的迭代器
//Itr 、ListItr是内部类
//迭代器是面向接口编程,it是引用,保存了内存地址,指向堆中的"迭代器对象"
//Iterator
//hasNext()
//nest()
//remove()
while (i.hasNext()){
Object o = i.next();
System.out.println(o);
}
}
}
-
boolean contains(Object o)
如果此集合包含指定的元素,则返回 true 。 -
boolean remove(Object o)
从该集合中删除指定元素的单个实例(如果存在)(可选操作)。 -
存储在集合中的元素应该去重写equals方法
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if (obj instanceof Student){
Student s = (Student) obj;
if(s.name.equals(this.name)&&s.age==this.age){
return true;
}
}
return false;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest3 {
public static void main(String[] args) {
Collection c =new ArrayList();
Student s1 = new Student("李四",18);
Student s2 = new Student("王二",20);
Student s3 = new Student("张三",20);
Student s4 = new Student("张三",20);
//添加s1和s2
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
System.out.println(c.contains(s1));
System.out.println(c.contains(s2));
//迭代器遍历,输出数组的内容
Iterator it = c.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//删除s2
c.remove(s2);
System.out.println(c.contains(s1));
System.out.println(c.contains(s2));
//删除s4
c.remove(s4);
System.out.println("s4 had been deleted");
System.out.println(c.contains(s4));
}
}
二、深入remove方法
1、迭代器的remove方法
2、集合本身的remove方法
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest4 {
public static void main(String[] args) {
Collection c =new ArrayList();
for (int i = 0; i < 10; i++) {
c.add(i);
}
System.out.println(c.size());
//创建迭代器
Iterator it = c.iterator();
while (it.hasNext()){
//使用迭代器的remove方法
it.next();
it.remove();
//使用集合本身的remove方法
/*Object o = it.next();
c.remove(o);*/
//这种方法每删除一次就会重新获取迭代器
}
System.out.println(c.size());
}
}
三、Collection与Collections的比较
java.util.Collections; 集合工具类
java.util.Collection; 集合接口
1.Collections 是集合工具类,用来对集合进行操作。部分方法如下
-public static <T> boolean addAll(Collection<T>c,T...elements)//往集合中添加一些元素
-public static void shuffle(List<?> list) //打乱集合顺序
-public static <T> void sort(List<T> list) //对集合进行排序,默认为升序
注意:
- sort使用前提:被排序的集合里边存储的元素,必须实现Comparable,重写接口中的方法compareTo定义排序的规则
- sort使用范围
适用于List,若为其他类型,需要将其强制类型转换
2.Comparable接口的排序规则:
// 自己(this)-参数:升序
public static <T>void sort(List<T> list, Comparator<? super T>):
//将集合中元素按照指定规则排序
3.Comparator和Comparable的区别
Comparable:自己(this)和别人(参数)比较,自己需要实现Comparable接口,重写比较的规则compareTo方法
Comparator:相当于找一个第三方的裁判,比较两个
Comparator定义排序规则: o1-o2 升序,反之降序
import java.util.*;
public class CollectionTest5 {
public static void main(String[] args) {
List c = new ArrayList();
c.add(10);
c.add(40);
c.add(30);
c.add(50);
c.add(20);
for (int i = 0; i < c.size(); i++) {
System.out.println(c.get(i));
}
System.out.println("after sorting");
Collections.sort(c);
for (int i = 0; i < c.size(); i++) {
System.out.println(c.get(i));
}
//
Set s = new HashSet();
s.add(10);
s.add(40);
s.add(30);
s.add(50);
s.add(20);
//Collections.sort(s);
//报错,该方法只允许类型为List的使用
List l = new ArrayList(s);
Collections.sort(l);
System.out.println("===================");
for (int i = 0; i < l.size(); i++) {
System.out.println(c.get(i));
}
}
}
集合.List接口详解
一、List说明
List接口对Collection接口扩充了更多方法,这些方法也更加简便。如果需要使用次接口,则需要对其子类进行实例化。在实际开发中List常用的子类接口有ArrayList、Vector、LinkedLIst。
二、ArratList
2.1、添加元素
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
//创建集合
Collection<String> c = new ArrayList<>();
List<String> l = new ArrayList<>();
//添加元素
c.add("张三");
c.add("李四");
c.add("王二");
System.out.println(c);
l.add("B");
l.add("C");
l.add("D");
System.out.println(l);
l.add(0,"A");
System.out.println(l);
//可以看出add()方法是直接添加到集合后面
l.addAll(c);
System.out.println(l);
l.addAll(0,c);
System.out.println(l);
}
}
2.2、删除元素
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ListTest2 {
public static void main(String[] args) {
//创建集合
Collection<String> c = new ArrayList<>();
List<String> l = new ArrayList<>();
//添加元素
c.add("张三");
c.add("李四");
c.add("王二");
System.out.println(c);
l.add("B");
l.add("C");
l.add("D");
System.out.println(l);
//删除元素
c.remove("王二");
l.remove(2);
System.out.println(c);
System.out.println(l);
}
}
2.3、输出全部内容
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ListTest3 {
public static void main(String[] args) {
//创建集合
Collection<String> c = new ArrayList<>();
List<String> l = new ArrayList<>();
//添加元素
c.add("张三");
c.add("李四");
c.add("王二");
System.out.println(c);
l.add("B");
l.add("C");
l.add("D");
System.out.println(l);
System.out.println("============");
//for-each
for (String s : c) {
System.out.println(s);
}
System.out.println("============");
for (String s : l) {
System.out.println(s);
}
System.out.println("=============");
//for循环正序输出
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}
System.out.println("=============");
//for逆序输出
for (int i = l.size()-1; i >= 0 ; i--) {
System.out.println(l.get(i));
}
}
}
2.4、将集合变为对象数组
import java.util.ArrayList;
import java.util.List;
public class ListTest4 {
public static void main(String[] args) {
List<String > l = new ArrayList<>();
l.add("A");
l.add("B");
l.add("C");
l.add("D");
Object[] o = l.toArray();
for (int i = 0; i < o.length; i++) {
String str = (String)o[i];
System.out.println(str);
}
}
}
三、LinkedLIst
import java.util.LinkedList;
public class ListTest5 {
public static void main(String[] args) {
LinkedList<Integer> l = new LinkedList<>();
l.add(1);
l.add(2);
l.add(3);
System.out.println(l);
l.addFirst(0);//在链表头加数据
l.addLast(4);//在链表尾加数据
System.out.println("分别在表头加上0,表尾加上4");
System.out.println(l);
System.out.println("element找到表头"+l.element());
System.out.println(l);
System.out.println("peek找到不删除表头"+l.peek());
System.out.println(l);
System.out.println("poll找到并删除表头"+l.poll());
System.out.println(l);
//先进先出的方式清空链表
while (!l.isEmpty()){
l.poll();
}
System.out.println("表已经清空");
System.out.println(l);
}
}
集合.Set接口详解
一、Set接口简介
Set接口有三个实现类,分别是HashSet,TreeSet,LinkedHashSet。
Set接口的特点:
- Set接口是无序的
- Set接口中的数据不允许重复
- Set接口无法通过下标访问数据
- 查找慢,插入删除快(底层数据结构是哈希表和红黑树)
- Set集合使用equals()和hashCode()方法实现元素去重
二、HashSet特点
HashSet是Set接口的实现类
线程不安全
1.1、HashSet确定储存唯一性原则
1)如果对象的hashCode值不同,则不用判断equals方法,就直接存到HashSet中。
2)如果对象的hashCode值相同,需要用equals方法进行比较,如果结果为true,则视为相同元素,不存储。如果结果为false,视为不同元素,进行存储。
如果对象元素要存储到HashSet中,必须覆盖hashCode方法和equals方法。才能保证从对象中的内容的角度保证唯一。
三、TreesSet特点:
- 有序
- 不重复
- 添加、删除、判断元素存在性效率比较高
- 线程不安全
1.1、TreeSet对元素进行排序的方式:
1) 如果是基本数据类型和String类型,无需其它操作,可以直接进行排序。
2) 对象类型元素排序,需要实现Comparable接口,并覆盖其compareTo方法。
3) 自己定义实现了Comparator接口的排序类,并将其传给TreeSet,实现自定义的排序规则。
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class SetTest {
public static void main(String[] args) {
Set<String> s1 = new HashSet<>();
//添加元素
s1.add("张三");
s1.add("王二");
s1.add("李四");
s1.add("张三");//由于HashSet里面的元素是不允许重复的,所以这里有警告
s1.add(null);
s1.add(null);
System.out.println(s1);//[null, 李四, 张三, 王二]
//观察运行结果可以发现,元素都是唯一的,没有重复元素,而且元素读出的顺序与存入顺序不同。
//其他操作
System.out.println("张三的键是"+"张三".hashCode());
System.out.println("李四的键是"+"李四".hashCode());
System.out.println("王二的键是"+"王二".hashCode());
System.out.println(s1);
System.out.println(s1.contains("李四"));
s1.remove("李四");
System.out.println(s1);
System.out.println(s1.contains("李四"));
System.out.println("================");
Object[] objs = s1.toArray();
for(Object o:objs){
System.out.println(o);
}
System.out.println("================");
Set<String> s2 = new TreeSet<>();
s2.add("apple");
s2.add("banana");
s2.add("pear");
//s2.add(null);//这里报错,我的理解是这里创建了一个null的对象的,但是未初始化,相当于系统被要求特意留出空间,即占着茅坑不拉屎。
s2.add("apple");
System.out.println(s2);
//其他操作类似
}
}
集合.Map接口
一、Map接口简介
Map与List、Set接口不同,它是由一系列键值对组成的集合,提供了key到Value的映射。
同时它也没有继承Collection。
在Map中它保证了key与value之间的一一对应关系。也就是说一个key对应一个value,所以它不能存在相同的key值,当然value值可以相同。
常用实现类:HashMap、TreeMap、LinkedHashMap和Properties。其中,HashMap是 Map 接口使用频率最高的实现类
二、Map常用方法
import java.util.*;
public class MapTest1 {
public static void main(String[] args) {
Map m1 = new HashMap();
//2.添加键值对
m1.put("100001", "张三");
m1.put("100002", "李四");
m1.put("100003", "王五");
m1.put("100004", "赵六");
m1.put("100001", "张三儿");//当键值重复时,会覆盖之前的Key-value值
System.out.println(m1);
System.out.println("键值对个数"+m1.size());
//判断集合中是否包含某个键
System.out.println(m1.containsKey("100001"));//true
System.out.println(m1.containsValue("张三"));//false
m1.remove("100001");
System.out.println(m1);
//获取Value
Collection c = m1.values();//将m1中值都放入集合c
Iterator it = c.iterator();//迭代器遍历集合c
while (it.hasNext()){
System.out.println(it.next());
}
//获取Key
Set s1 = m1.keySet();
Iterator it2 = s1.iterator();
while (it2.hasNext()){
Object key =it2.next();
Object value = m1.get(key);
System.out.println("m1中键值对:"+key+"-->"+value);
}
Set set =m1.entrySet();
Iterator it3 = set.iterator();
while (it3.hasNext()){
System.out.println(it3.next());
}
}
}