集合(一)
下一篇:集合(二)
1.集合概述
- 数组其实就是一个集合,集合实际上就是一个容器,一个载体,可以容纳其他类型的数据,一次容纳多个对象
- 集合不能直接存储基本数据类,也不能直接存储java对象,集合当中存储的都是java对象的内存地址(或者说集合中存储的都是引用)
不同集合对应不同的数据结构
在java中每个不同的集合,底层会对应不同的数据结构,往不同的集合中存储元素,等于将数据放到了不同的数据结构当中。
这里只需要掌握怎么用?在什么情况下选择哪一种合适的集合去使用即可
new ArrayList(); 创建一个集合对象,底层是数组
new LinkedList(); 创建一个集合对象,底层是链表
new TreeSet(); 创建一个集合对象,底层是二叉树
在java中集合分为两大类:
-
一类是单个方式存储元素
这一类集合中超级父接口:java.util.Collection;
-
一类是以键值对的方式存储元素
这一类集合中超级父接口:java.util.Map;
集合继承结构图1(图片源自视频自学摘取的)
自学视频看的动力节点老杜的JavaSE基础合集
集合继承结构图2
2.Collection
Collection接口中的常用方法
package se3.collection;
import java.util.ArrayList;
import java.util.Collection;
/*
关于java.util.Collection接口中常用的方法
boolean add(Object e) 向集合中添加元素
int size() 获取集合中元素的个数
void clear() 清空集合
boolean contains(Object o) 判断当前集合中是否包含元素o,包含返回true,不包含返回false
boolean remove(Object o) 删除集合中的某个元素
boolean isEmpty() 判断集合中元素的个数是否为空
Object[] toArray() 调用这个方法可以把集合转换成数组
*/
public class CollectionTest01 {
public static void main(String[] args) {
//创建一个集合对象
//Collection c = new Collection();//接口是抽象的,无法实例化
//多态
Collection c = new ArrayList();
//测试Collection接口中的常用方法
c.add(1200);//自动装箱,实际上是放进去了一个对象的内存地址
//Integer x = new Integer(1200);
c.add(3.14);//自动装箱
c.add(new Object());
c.add(new Student());
c.add(true);
//获取集合中元素的个数
System.out.println("集合中元素的个数是:" + c.size());//集合中元素的个数是:5
//清空集合
c.clear();
System.out.println("集合中元素的个数是:" + c.size());//集合中元素的个数是:0
//再向集合中添加元素
c.add("hello");//"hello"对象的内存地址放到了集合当中
c.add("world");
c.add("阿波");
c.add("波波");
c.add(1);
//判断集合中是否包含“阿波”
boolean flag = c.contains("阿波");
System.out.println(flag);//true
//判断集合中是否包含“阿西吧”
boolean flag2 = c.contains("阿西吧");
System.out.println(flag2);//false
//删除集合中某个元素
c.remove(1);
System.out.println("集合中元素的个数是:" + c.size());//集合中元素的个数是:4
//判断集合是否为空
System.out.println(c.isEmpty());//false
c.clear();//清空
System.out.println(c.isEmpty());//true表示集合中没有元素了
c.add("abc");
c.add("def");
c.add(100);
c.add("hello world");
c.add(new Student());
//转换成数组
Object[] obj = c.toArray();
for (int i = 0; i < obj.length; i++) {
//遍历数组
Object o = obj[i];
System.out.print(o + " ");
//System.out.println(o.toString());
}
//输出结果:abc def 100 hello world se3.collection.Student@154617c
}
}
class Student{}
Iterator方法
案例1
package se3.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 关于集合遍历/迭代专题
*/
public class CollectionTest02 {
public static void main(String[] args) {
//注意:以下的遍历方式/迭代方式,是所有Collection通用的一种方式
//在Map集合中不能用,在所有的Collection以及子类中使用
//创建集合对象
Collection c = new ArrayList();//后面的集合无所谓,主要看前面的Collection接口怎么遍历/迭代
c.add("abc");
c.add("def");
c.add(100);
c.add(new Object());
//对集合Collection进行遍历/迭代
//第一步:获取集合对象的迭代器对象Iterator
Iterator it = c.iterator();
//第二步:通过以上获取的迭代器对象开始迭代/遍历集合
/*
以下两个方法是迭代器对象Iterator中的方法:
boolean hasNext() 如果仍有元素可以迭代,则返回true
Object next() 返回迭代的下一个元素
*/
//注意:一直取,不判断会出现异常
while (it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
/*
运行结果:
abc
def
100
java.lang.Object@154617c
*/
}
}
案例2
package se3.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
public class CollectionTest03 {
public static void main(String[] args) {
//创建集合对象
Collection c1 = new ArrayList();//ArrayList集合:有序可重复
//添加元素
c1.add(1);
c1.add(2);
c1.add(3);
c1.add(4);
//迭代集合
Iterator it = c1.iterator();
while (it.hasNext()){
//存进去是什么类型,取出来还是什么类型
Object obj = it.next();
//只是这里在输出的时候会转换成字符串
//因为println会调用toSTring方法
System.out.println(obj);
}
System.out.println("#######################");
//HashSet集合:无序不重复
Collection c2 = new HashSet();
//无序:存进去和取出来的顺序不一定相同
//不可重复:存储100,不能再存储100
c2.add(100);
c2.add(200);
c2.add(300);
c2.add(90);
c2.add(400);
c2.add(50);
c2.add(60);
c2.add(100);
Iterator it2 = c2.iterator();
while (it2.hasNext()){
System.out.println(it2.next());
}
}
}
contains方法解析
深入Collection集合的contains方法
boolean contains(object o)
判断集合中是否包含某个对象o
如果包含返回true,不包含返回false
contains方法底层调用了equals方法
这个equals返回true就表示包含这个元素
package se3.collection;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionTest04 {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//向集合中存储元素
String s1 = new String("abc");//s1 = 0x1111
c.add(s1);
String s2 = new String("def");//s2 = 0x2222
c.add(s2);
//集合中元素的个数
System.out.println("元素的个数是:" + c.size());//2
//新建的对象String
String x = new String("abc");//x = ox3333
//c集合中是否包含x?
System.out.println(c.contains(x));//判断集合中是否存在“abc”,true
}
}
package se3.collection;
import java.util.ArrayList;
import java.util.Collection;
/*
测试contains方法
总结:存放在一个集合中的类型,一定要重写equals方法
*/
public class CollectionTest05 {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建用户对象
User u1 = new User("jack");
//加入集合
c.add(u1);
//判断集合中是否包含u2
User u2 = new User("jack");
//没有重写equals之前:这个结果是false
System.out.println(c.contains((u2)));//true
Integer x = new Integer(10000);
c.add(x);
Integer y = new Integer(10000);
System.out.println(c.contains(y));//true
}
}
class User{
private String name;
public User(){}
public User(String name){
this.name = name;
}
//重写equals方法,调用一定是调用重写的equals方法
public boolean equals(Object o){
if(o == null || !(o instanceof User)) return false;
if (o == this) return true;
User u = (User) o;
//如果名字一样表示同一个人(不再比较对象的内存地址了,比较内容了)
return u.name.equals(this.name);
}
}
remove方法解析
package se3.collection;
import java.util.ArrayList;
import java.util.Collection;
/*
测试remove方法
总结:存放在一个集合中的类型,一定要重写equals方法
*/
public class CollectionTest05 {
public static void main(String[] args) {
//创建集合对象
Collection cc = new ArrayList();
//创建字符串对象
String s1 = new String("hello");
cc.add(s1);
String s2 = new String("hello");
//删除s2
cc.remove(s2);//s1.equals(s2)
//java认为s1和s2是一样的,删除s2就是删除s1
System.out.println(cc.size());//0
User u1 = new User("jack");
cc.add(u1);
User u2 = new User("jack");
cc.remove(u2);
System.out.println(cc.size());//0
//若没有重写equals,u1就不是u2,删除u2也不会删除u1
}
}
class User{
private String name;
public User(){}
public User(String name){
this.name = name;
}
//重写equals方法,调用一定是调用重写的equals方法
public boolean equals(Object o){
if(o == null || !(o instanceof User)) return false;
if (o == this) return true;
User u = (User) o;
//如果名字一样表示同一个人(不再比较对象的内存地址了,比较内容了)
return u.name.equals(this.name);
}
}