集合框架的体系结构
注意其中只有最下面的一行是类,上面的都是接口。
一、List(列表)
1、ArrayList
ArrayList学习代码一:
package com.imooc.set;
import java.util.ArrayList;
public class ListDemo1
{
public static void main(String[] args)
{
//用ArrayList存储编程语言的名称,并输出
//名称包括Java,C,C++,Go和Swift
ArrayList list=new ArrayList();//List list=new ArrayList();也可以
//利用add(Object o)方法为列表添加元素,注意add方法是在末尾添加新元素
list.add("Java");
list.add("C");
list.add("C++");
list.add("Go");
list.add("Swift");
//利用size()方法输出列表中元素的个数
System.out.println("列表中元素的个数为:"+list.size());
//利用ArrayList类下的get(int index)方法获取元素,遍历输出所有的编程语言
System.out.println("********************************");
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i)+" ");
}
//利用ArrayList类下的remove(int index)方法移除列表中的C++
System.out.println();
list.remove(2);
//另一种移除的方法是remove(Object o),即list.remove("C++");
System.out.print("移除C++以后的列表元素为:");
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i)+" ");
}
}
}
ArrayList的存放方式
ArrayList学习代码二中的Notice(公告)类
package com.imooc.set;
import java.util.Date;
public class Notice //公告类
{
private int id;//ID
private String title;//标题
private String creator;//创建人
private Date createTime;//创建时间
public Notice()
{
}
public Notice(int id,String title,String creator,Date createTime)
{
this.setCreateTime(createTime);
this.setCreator(creator);
this.setId(id);
this.setTitle(title);
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getCreator()
{
return creator;
}
public void setCreator(String creator)
{
this.creator = creator;
}
public Date getCreateTime()
{
return createTime;
}
public void setCreateTime(Date createTime)
{
this.createTime = createTime;
}
}
ArrayList学习代码二中的Test(测试)类
package com.imooc.set;
import java.util.ArrayList;
import java.util.Date;
public class NoticeTest
{
public static void main(String[] args)
{
//创建Notice类的对象,生成三条公告
Notice notice1=new Notice(1, "欢迎来到慕课网", "管理员", new Date());
Notice notice2=new Notice(2, "请同学们按时提交作业", "老师", new Date());
Notice notice3=new Notice(3, "考勤通知", "老师", new Date());
//将公告类对象存放在ArrayList中
ArrayList noticeList=new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);
/*显示公告。由于ArrayList类下的get(int index)方法返回的是Object类的对象,而Object没有getTitle方法,因此在此
需要强制转换为Notice类的对象才能调用Notice类的get/set方法,并且需要加很多括号,不仅需要将Notice和noticeList.get分别括起来,还需要将他们
一起括起来*/
System.out.println("公告内容:");
for(int i=0;i<noticeList.size();i++)
{
System.out.println((i+1)+":"+((Notice)(noticeList.get(i))).getTitle());
}
//利用add的重载方法add(int index,Object o)在第一条公告后面添加一条新公告
Notice notice4=new Notice(4,"在线编辑器可以使用了","管理员",new Date());
noticeList.add(1,notice4);
System.out.println("**************************");
System.out.println("添加后的公告内容:");
for(int i=0;i<noticeList.size();i++)
{
System.out.println((i+1)+":"+((Notice)(noticeList.get(i))).getTitle());
}
//利用方法remove(int index)或remove(Object o)删除按时完成作业的公告
noticeList.remove(notice2);//noticeList.remove(2);
System.out.println("**************************");
System.out.println("删除后的公告内容:");
for(int i=0;i<noticeList.size();i++)
{
System.out.println((i+1)+":"+((Notice)(noticeList.get(i))).getTitle());
}
//将第二条公告修改为:Java在线编辑器可以使用了
//修改公告对象中的title的值再输出即可
notice4.setTitle("Java在线编辑器可以使用了");
System.out.println("**************************");
System.out.println("修改后的公告内容:");
for(int i=0;i<noticeList.size();i++)
{
System.out.println((i+1)+":"+((Notice)(noticeList.get(i))).getTitle());
}
}
}
二、set(集)
1、HashSet(哈希集)
Iterator(迭代器)接口及接口中的常用方法
HashSet学习代码一
package com.imooc.set;
import java.util.HashSet;
import java.util.Iterator;
public class HashSetDemo1
{
public static void main(String[] args)
{
//将英文单词添加到HashSet中
HashSet set=new HashSet();//Set set=new HashSet();
//利用add(Object o)方法向集合中添加元素
set.add("blue");
set.add("red");
set.add("black");
set.add("yellow");
set.add("white");
/*利用迭代器Iterator接口显示集合的内容,因为HashSet是无序的,因此一切ArrayList中有index参数的方法HashSet都没有
因此get(int index)方法也不存在与HashSet中*/
System.out.println("集合中的元素为:");
//通过set调用iterator方法,把结果存放到Iterator的引用it中,即将元素放在了迭代器中
Iterator it=set.iterator();
//遍历迭代器并输出元素,hasNext()方法判断是否存在下一个元素,next()方法输出下一个元素
while(it.hasNext())//hasNext()方法返回的是boolean值
{
System.out.print(it.next()+" ");//默认调用toString方法
}
//在集合中插入一个新的单词,由于HashSet是无序的,因此插入的位置不确定.
//set.add("green");
//由于HashSet不允许存在相同元素,因此执行以下语句后输出集合内容,集合内容不会发生任何变化
//set.add("blue");
}
}
HashSet学习代码二之Cat类
package com.imooc.set;
public class Cat
{
private String name;//名字
private int month;//年龄
private String species;//品种
public Cat()
{
}
public Cat(String name,int month,String species)
{
this.setName(name);
this.setMonth(month);
this.setSpecies(species);
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getMonth()
{
return month;
}
public void setMonth(int month)
{
this.month = month;
}
public String getSpecies()
{
return species;
}
public void setSpecies(String species)
{
this.species = species;
}
@Override
public String toString()
{
return "[姓名:" + name + ", 年龄:" + month + ", 品种:" + species + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + month;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((species == null) ? 0 : species.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if(this==obj)//先判断对象是否相等,对象相等就不用再比较内容了
return true;
if(obj instanceof Cat)//判断obj是否具有Cat类的特征
{
Cat cat=(Cat)obj;//如果是obj是Cat类的对象,先将obj强制转换为Cat类
//判断两个对象的内容是否相等
return cat.getName().equals(name)&(cat.getMonth()==month)&(cat.getSpecies()==species);
}
return false;
}
}
HashSet学习代码二之CatTest(测试)类
package com.imooc.set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CatTest
{
public static void main(String[] args)
{
//定义宠物猫对象
Cat huahua=new Cat("花花",12,"英国短毛猫");
Cat fanfan=new Cat("凡凡",3,"中华田园猫");
/*将宠物猫对象放入HashSet。注释中的Set、HashSet、Iterator后面的<Cat>表示集合中只允许存放Cat类型的对象.
* 若不加<Cat>则无限制,这种方式称为泛型
加了<Cat>之后,那么之后的it.next()返回的不再是Object类型的对象,而直接是Cat对象,不用再强制转换成Cat类型*/
Set set=new HashSet();//泛型集合:Set<Cat> set=new HashSet<Cat>();
set.add(huahua);
set.add(fanfan);
//显示宠物猫信息,next()方法返回的是Object类对象,因此也要强制转换成Cat类才能调用Cat下的方法
Iterator it=set.iterator();//泛型集合的迭代:Iterator<Cat> it=set.iterator();
while(it.hasNext())
{
System.out.println(it.next());//直接输出默认调用toString方法
}
//再添加一个与花花属性一样的猫,验证HashSet的不可重复性
/*若依以下注释代码写,发现HashSet的不可重复性验证失败,输出结果出现了两个花花,原因是这两个花花指向的栈中的地址不相
同,因此不被编译器认为是相同的对象,而之前的字符串由于定义方式决定了是存放在常量池中的,因此两个相同的字符串
指向了常量池中的同一片内存,被编译器认为是一个对象。*/
/*Cat huahua01=new Cat("花花",12,"英国短毛猫");
set.add(huahua01);
System.out.println("***********************");
System.out.println("添加重复数据后的宠物猫信息:");
it=set.iterator();
while(it.hasNext())
{
System.out.println(it.next());//直接输出默认调用toString方法
}*/
//因此如果想让HashSet中不出现内容重复的对象,需要重写HashCode和equals判断方法,重写见Cat类,一般只重写equals方法,因为HashSet判断是否有重复对象的方法利用的就是equals方法
//重写equals方法,让重写后的equals方法不再只判断对象是否指向同一片堆中的内存,而是还判断对象内容,重写后再执行以下代码,就不会出现两个名为花花的对象了
Cat huahua01=new Cat("花花",12,"英国短毛猫");
set.add(huahua01);
System.out.println("***********************");
System.out.println("添加重复数据后的宠物猫信息:");
it=set.iterator();//由于上一个迭代器it已经迭代到末尾了,因此需要重新迭代
while(it.hasNext())
{
System.out.println(it.next());//直接输出默认调用toString方法
}
//重新插入一个新宠物猫,因为HashSet是无序的,因此插入位置未知
System.out.println("**************************");
Cat huahua02=new Cat("花花二代",2,"英国短毛猫");
set.add(huahua02);
System.out.println("添加花花二代后的宠物猫信息:");
it=set.iterator();
while(it.hasNext())
{
System.out.println(it.next());//直接输出默认调用toString方法
}
//在集合中查找花花的信息并输出
//第一种方法:根据对象查找,利用的方法是contains(Object o)
System.out.println("**************************");
System.out.println("通过对象查找花花:");
if(set.contains(huahua))
{
System.out.println("花花找到了!");
System.out.println(huahua);
}
else
System.out.println("花花没找到");
//第二种方法:利用迭代器遍历集合,根据对象属性name查找
System.out.println("**************************");
System.out.println("通过对象name属性查找花花:");
boolean flag=false;
it=set.iterator();//由于上一个迭代器it已经迭代到末尾了,因此需要重新迭代
Cat c=null;//一定要先赋值null,不能单独写Cat c放在这里,否则程序会报错.
while(it.hasNext())
{
if((((Cat)(it.next())).getName()).equals("花花"))
{
c=(Cat)(it.next());
flag=true;//找到了
break;
}
}
if(flag==true)
{
System.out.println("花花找到了");
System.out.println(c);
}
else
System.out.println("花花没找到!");
//利用方法remove(Object o)删除花花二代的信息并重新输出(在用了泛型的情况下可以配合增强型for循环)
//由于前面没有用泛型,因此将在使用了泛型集合的前提下配合增强型for循环的代码写在注释中
/*for(Cat cat:set)
{
if("花花二代".equals(cat.getName()))
set.remove(cat);
}
System.out.println("删除花花二代后的数据");
for(Cat cat:set)
{
System.out.println(cat);
}
*/
//利用方法removeAll(Collection arg0)删除集合中所有宠物猫信息
/*
* boolean flag1=set.removeAll(set);
* if(flag1)
* System.out.println("删除成功");
* else
* System.out.println("删除失败");
*/
}
}