下面的图片是从网上的找来的,虽然仅从图上来看,集合的实现类很少,但其实java中集合的实现类有很多个,因为我们一般用得最多的就是下面这几种,因此我们就来先了解集合的这几种实现类
一,Collection接口
Collection接口实例化后常用的方法如下:
- add()---添加单个元素
- add()---添加另外一个集合对象
- remove()---删除指定元素
- removeAll()---从指定集合中删除(移除)包含在另外一个集合中的元素,返回值boolean(即,我有你居然有,删除相同元素)
- retainAll()---仅保留该集合中同包含在指定中的对象(即你有我也有,保留相同元素)
- contains()---查找单个元素是否存在,返回值boolean
- containsAll()---查找在该集合中是否存在指定集合中的所有对象,返回值boolean
- toArray()---将一个集合转变成数组【参数必须为数组类型的实例,并且已经被实例化。】
- size()---获取集合中元素的个数(即得到集合的长度)
- isEmpty()---判断集合是否为空
- clear()---清空集合内的所有元素
add()和addAll()方法
现在来我们创建Collection接口的实例化对象,并向该集合添加数据类型和对象。
使用加强for循环遍历Collection集合中的元素:
相信大家也看到了我们使用的是Object类的变量来接收集合中的元素,之所以使用Object类是因为集合就像个容器一样,啥都可以往里面放(即上面类型的元素都有),在我们不知道从集合(泛型集合)中取出来的元素是什么类型时,就可以使用Object类的变量来接收。在java中Object类是所有子类的超类。当然了,我们也可以指定集合的类型,也就可以使用确定的数据类型变量来接收集合中的元素。如下:
我们在Collection接口名的后面加上双尖括号,里面写上我们要存放的数据类型,于是我们可以看到第七行代码爆红了,那是因为我们添加的是对象,而不是String类型的数据。提示如下:
The method add(String) in the type Collection<String> is not applicable for the arguments (AAA)
我们虽然指定了集合的add()方法的添加数据类型,但是我们可以使用addAll()方法添加其他集合对象,当然了,遍历时的接收变量类型就要改为Objet类。如下:
remove与removeAll方法
接下来我们remove与removeAll方法在Collection接口实现类中的使用:
从上图中我们可以看到,因为对collection使用了remove方法删除了元素“张三”,此时collection中的元素为[托马斯沃森,马丁奥德斯基,尼古拉斯沃斯,666,true],之后使用了removeAll方法删除了在collection与collection01中都出现的元素666和true,因此结果就为上图所示。
retainAll方法
retainAll方法的作用和上面removeAll方法刚刚好相反,该方法保留了两个集合中都有的元素。
contains和containsAll方法
接下来我们看看contains和containsAll方法的使用。 在下图中,我们先执行了remove操作将‘张三’删除,之后才使用contains方法判断是否有“张三”这个元素,因此返回为false。
collection集合中的元素不包含collection01集合中的对象,因此返回值为false。
import java.util.*;
public class ForCollection01 {
public static void main(String[] args) {
Collection<String> collection=new ArrayList();
Collection collection01=new ArrayList();
collection.add("托马斯沃森"); //在只支持添加String数据类型的集合collection中添加String数据类型的字符串
collection.add("马丁奥德斯基");
collection.add("尼古拉斯沃斯");
collection.add("张三");
collection01.add(true); //在支持泛型的集合collection01中添加布尔型的数据类型
collection01.add(666); //在支持泛型的集合collection01中添加整型的数据类型
collection.addAll(collection01); //在collection集合中添加另外一个集合collection01
collection.remove("张三"); //删除指定元素
collection.removeAll(collection01); //保留collection中独有的元素
System.out.println(collection.contains("张三"));
System.out.println("collection:"+collection);
System.out.println("collection01:"+collection01);
System.out.println(collection.containsAll(collection01));
collection.retainAll(collection01); //保留两个集合中都有的元素
for(Object obj:collection) { //使用Iterator遍历collection
System.out.println(obj);
}
}
}
class AAA{
String name;
int age;
public AAA(String name,int age) {
this.name=name;
this.age=age;
}
public String toString() {
return "姓名是:"+name+",年龄为:"+age;
}
1,List接口
- set()---修改集合中的元素
- get()---得到对应索引号的元素
set和get方法
接下来我们使用代码来查看两个方法具体的实现效果:
ArrayList实现类
案例一:使用ArrayList创建集合,添加姓名,性别,出生日期,并分别打印输出。
首先创建集合
接着为了使main函数看起来清爽(干净)我们就在main函数外写一个处理list集合的方法。
如上我们通过get,split等方法将一个集合转换成数组,并将里面的字符串分开打印输出。
LinkedList实现类
public class TestLinkedList {
public static void main(String[] args) {
Node node1=new Node("node1");
Node node2=new Node("node2");
Node node3=new Node("node3");
node1.next=node2;
node2.next=node3;
node3.prev=node2;
node2.prev=node1;
Node first=node1;
Node last=node3;
System.out.println("========正向打印======");
while(true) {
if(first==null) {
break;
}
System.out.println(first);
first=first.next;
}
System.out.println("========反向打印======");
while(true) {
if(last==null) {
break;
}
System.out.println(last);
last=last.prev;
}
System.out.println("===在node1与node2之间插入node12节点===");
Node node12=new Node("node12");
node12.next=node2;
node12.prev=node1;
node2.prev=node12;
node1.next=node12;
first =node1;
while(true) {
if(first==null) {
break;
}
System.out.println(first);
first=first.next;
}
}
}
class Node{
Object item;
public Node next;
public Node prev;
public Node(Object name) {this.item=name;}
public String toString() {
return "Node's name is "+item;
}
}
从上面代码实现我们可以看出链表的对象添加和删除都比较方便。添加只需要当前对象的prev指向上一个对象,next指向下一个对象,以及上一个对象的next指向当前对象,下一个对象的prev指向当前对象,四行代码即可。
Vector(向量,矢量)实现类
2,Set接口
HashSet实现类
重写两个方法后,属性值相同的两个对象只会出现一个。【重写hash值后,如果对象的属性值相同,则返回相同的hash值】
这个有点难,我们不用一行一行的敲出来,可以鼠标右击,找到‘source’之后点击,找的‘generate hashCode() and equals()’选择即可快速生成重写方法。
import java.awt.*;
import java.util.*;
public class TestHashSet {
public static void main(String[] args) {
Set set=new HashSet();
set.add(new Employee("马丁奥德斯基",63));
set.add(new Employee("尼古拉斯沃斯",87));
set.add(new Employee("马丁奥德斯基",63));
System.out.println(set);
}
}
class Employee{
private String name;
private int age;
public Employee(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(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + "]";
}
@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;
Employee other = (Employee) 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;
}
}
二,Map接口
1,HashMap实现类
案例一:打印工资在18000以上的对象。
import java.util.*;
public class TestHashMap_Employee {
public static void main(String[] args) {
Map map=new HashMap();
map.put(new Employee01("001","张三",5000).id,new Employee01("001","张三",5000));
map.put(new Employee01("002","李四",19000).id,new Employee01("002","李四",19000));
Set mapKeySet=map.keySet();
for(Object obj:mapKeySet) {
Employee01 emp=(Employee01)map.get(obj);
if(emp.wage>18000) {
System.out.println(emp);
}
}
}
}
class Employee01{
String id;
String name;
int wage;
public Employee01(String id,String name,int wage){
this.id=id;
this.name=name;
this.wage=wage;
}
@Override
public String toString() {
return "Employee01 [id=" + id + ", name=" + name + ", wage=" + wage + "]";
}
}