List集合
一.ArraryList:
最基本的集合不多做介绍
二.Vector
Vector cn=new Vector();
A:有特有功能
a:添加
public void addElement(E obj) -- add()
cn.addElement(object e);
b:获取
public E elementAt(int index) -- get()
cn.elementAt(int index) ;
public Enumeration<E> elements() -- iterator()
B:案例
a:Vector存储字符串并遍历
Vector cn=new Vector();
cn.addElement("a");
cn.addElement("b");
cn.addElement("s");
Iterator cnlist = cn.iterator();
while (cnlist.hasNext())
{
System.out.println(cnlist.next());
}
b:Vector存储自定义对象并遍历
//学生对象 重写toString
public class Student {
private String name;
private String age;
public Student(String name,String age)
{
this.name=name;
this.age=age;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
//主类
Vector cn=new Vector();
//创建student对象
Student s=new Student("fan","15");
Student s1=new Student("fan1","15");
cn.addElement(s);
cn.addElement(s1);
//使用遍历器来进行遍历
Iterator cnlist = cn.iterator();
while (cnlist.hasNext())
{
System.out.println(cnlist.next().toString());
}
三:LinkedList
A:有特有功能
a:添加
addFirst()
addLast()
b:删除
removeFirst()
removeLast()
c:获取
getFirst()
getLast()
B:案例
a:LinkedList存储字符串并遍历
LinkedList ll=new LinkedList();
ll.addFirst("a");
ll.addFirst("b");
ll.addFirst("c");
ll.addLast("s");
Iterator iterator = ll.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
输出:
c
b
a
s
b:LinkedList存储自定义对象并遍历
Student s=new Student("sss","123");
Student s1=new Student("sss1","12");
Student s2=new Student("sss2","1");
LinkedList ll=new LinkedList();
ll.addFirst(s);
ll.addFirst(s1);
ll.addLast(s2);
Iterator iterator = ll.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
输出:
Student [name=sss1, age=12]
Student [name=sss, age=123]
Student [name=sss2, age=1]
Set集合
Set集合的特点
无序,唯一
一.HashSet集合
A:底层数据结构是哈希表(是一个元素为链表的数组)
B:哈希表底层依赖两个方法:hashCode()和equals()
执行顺序:
比较哈希值是否相同
相同:继续执行equals()方法
返回true:元素重复了,不添加
返回false:直接把元素添加到集合
不同:就直接把元素添加到集合C:如何保证元素唯一性的呢?
由hashCode()和equals()保证的
D:开发的时候,代码非常的简单,自动生成即可。
a:HashSet存储字符串并遍历
b:HashSet存储自定义对象并遍历(对象的成员变量值相同即为同一个元素)
//student新增方法 实际开发自动生成
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;
}
//主类
Student s=new Student("sss",1);
Student s1=new Student("sss",2);
Student s2=new Student("sss",3);
HashSet hs=new HashSet();
hs.add(s);
hs.add(s1);
hs.add(s2);
Iterator iterator = hs.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
输出:
Student [name=sss, age=1]
Student [name=sss, age=2]
Student [name=sss, age=3]
总结 :没有进行排序 无序 hashSet输出顺序不是按照插入顺序实现的 LinkedHashSet可以解决这个缺陷
(3)TreeSet集合
A:底层数据结构是红黑树(是一个自平衡的二叉树)
B:保证元素的排序方式
a:自然排序(元素具备比较性)
让元素所属的类实现Comparable接口
b:比较器排序(集合具备比较性)
让集合构造方法接收Comparator的实现类对象
(4)案例:
A:获取无重复的随机数
TreeSet hs=new TreeSet();
Random rd=new Random();
for (int i = 0; i < 30; i++)
{
int s = rd.nextInt(10);
hs.add(s);
}
Iterator iterator = hs.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
B:学生按照年龄从高到底输出
1.让类实现Comparable接口
Student implements Comparable<Student>
2.实现接口
public int compareTo(Student s) {
int num = s.age-this.age;
int num2 = num == 0 ? this.name.compareTo(s.name) : num;
return num2;
}
3.主类
Student s1=new Student("sss1",1);
Student s2=new Student("sss2",6);
Student s3=new Student("sss3",2);
Student s4=new Student("sss4",9);
Student s5=new Student("sss5",8);
Student s6=new Student("sss6",7);
Student s7=new Student("sss7",3);
Student s8=new Student("sss8",5);
TreeSet<Student> hs=new TreeSet<Student>();
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
hs.add(s5);
hs.add(s6);
hs.add(s7);
hs.add(s8);
Iterator iterator = hs.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
输出结果:
Student [name=sss4, age=9]
Student [name=sss5, age=8]
Student [name=sss6, age=7]
Student [name=sss2, age=6]
Student [name=sss8, age=5]
Student [name=sss7, age=3]
Student [name=sss3, age=2]
Student [name=sss1, age=1]
注意:
一: 修改方法:
public int compareTo(Student s) {
return 1;
}
输出结果:
Student [name=sss1, age=1]
Student [name=sss2, age=6]
Student [name=sss3, age=2]
Student [name=sss4, age=9]
Student [name=sss5, age=8]
Student [name=sss6, age=7]
Student [name=sss7, age=3]
Student [name=sss8, age=5]
二: 修改方法:
public int compareTo(Student s) {
return 1;
}
输出结构:
Student [name=sss8, age=5]
Student [name=sss7, age=3]
Student [name=sss6, age=7]
Student [name=sss5, age=8]
Student [name=sss4, age=9]
Student [name=sss3, age=2]
Student [name=sss2, age=6]
Student [name=sss1, age=1]