集合类的总结
Collection
|-----Set
|-----HashSet
|-----TreeSet
|-----List
|-----LinkedList
|-----ArrayList
Collection:
1.增
add(Object);
2.删
Remove(Object)
3.改
RetainAll()去交集
removeAll 去交集
4.查
isEmpty()
size()
Contains()
迭代器(取出元素)
ArrayList al=new ArrayList();
Iterator it=al.iterator();
List:
list的特有方法
凡事带角标都是List的特有方法
1.增
add(int index,Object obj)
2.删
remove(int index)
3.改
set(int index,Object obj)
4.查
get(int index)
//ArrayList al=new ArrayList();
al.index(Object obj)
al.subList(from,to)//包含头不包含尾
迭代器
ListIterator li=al.listIterator()
//这个迭代器的好处就是扩展了功能,在我们取元素的时候,我们可以用这个迭代器进行增删改查操作
List获取获取元素(带角标)
第一种:
for(int x=0;x<al.size();!al.isEmpty())
{
al.get(x);
}
第二种:迭代器
LinkedList特有方法:
(凡事带有first,last)
1.增
addFirst()
addLast()
2.删
removeFirst();//这个方法在删除集合中的元素的同时,能获取元素(要注意)
removeLast();
3.查
getFrist()
getLast()
jdk后面升级:
增
offerfirst()
offerlast()
删
peekfirst()
peekfirst()
查
pollfirst()
pollfirst()
LinkedList取出元素的方式
1.迭代器
2.List的get方法
3.removeFirst方法
这么写
while(!al.isEmpty)
al.removeFirst()
List 因为有索引 有序的元素是可重复的
Set 无序的(存进去和取出的顺序是不一样的),元素不可重复
下面我们来说说底层的数据结构
ArrayList 底层数据机构是数组 那么查询速度快 ,增删速度慢
LinkedList 底层数据结构是链表 增删速度快,查询速度慢
HashSet 底层数据结构是哈希表
TreeSet 底层数据结构是二叉树
由于带角标的特点 List里面比较两个对象是不是同一个对象,我们比较的是内容,所以我们要重写对象的equals方法
import java.util.*;
class Person
{
private String name;
private int age;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
//list中比较元素都要重写equals方法 因为底层contains remove都是调用equals
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
return false;
Person p=(Person)obj;
return (this.name.equals(p.name)&&this.age==p.age);
}
}
class ArrayListTest2
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Person("zhangsan",34));
al.add(new Person("lisi",30));
al.add(new Person("wangwu",23));
al.add(new Person("chenliu",34));
al.add(new Person("chenliu",34));
al.add(new Person("lisi",30));
al=singleElement(al);
Iterator it=al.iterator();
while(it.hasNext())
{
Person p=(Person)it.next();
System.out.println(p.getName()+"....."+p.getAge());
}
}
public static ArrayList singleElement(ArrayList al)
{
ArrayList newAl=new ArrayList();
for(Iterator it=al.iterator();it.hasNext();)
{
Object obj=it.next();
if(!newAl.contains(obj))//这里的contains自动调用equals方法
newAl.add(obj);
}
return newAl;
}
}
HashSet 那就要求我们在先比较 对象自身的hashcode 如果两个对象的hashcode然后再equals
import java.util.*;
class Person
{
private int age;
private String name;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
return false;
Person p=(Person)obj;
//sop(this.equals+".....equals"+p.name);
return this.name.equals (p.name)&&this.age==p.age;
}
/*
public int hashCode()
{
return 60;
}
*/
//hasCode优化
public int hashCode()
{
return name.hashCode()+age;
}
}
class HashSetTest
{
public static void main(String[] args)
{
HashSet hs=new HashSet();
hs.add(new Person("zhangsan",21));
hs.add(new Person("lisi",22));
hs.add(new Person("wangwu",19));
hs.add(new Person("zhangsan",21));
//sop(hs);
for(Iterator it=hs.iterator();it.hasNext();)
{
Person p=(Person)it.next();
sop(p.getName()+"......"+p.getAge());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
TreeSet
第一种让元素自身具备比较性 实现Comparable接口
覆盖里面的compareTo()方法
import java.util.*;
import java.lang.*;
import java.io.*;
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet();
ts.add(new Student("zhangsan",19));
ts.add(new Student("li",20));
ts.add(new Student("wangwu",21));
ts.add(new Student("zhaoliu",22));
ts.add(new Student("wangwu",21));
for(Iterator it=ts.iterator();it.hasNext();)
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"......."+stu.getAge());
}
}
}
//TreeSet 往里面添加元素要实现比较器哦
class Student implements Comparable
{
private int age;
private String name;
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int compareTo(Object obj)
{
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student stu=(Student)obj;
if(this.age>stu.age)
return 1;
else if(this.age==stu.age)
{
return this.name.compareTo(stu.name);
}
return -1;
}
}
第二种方法就是 让容器具有比较性
实现Comparator接口 覆盖里面的compare方法,然后传给容器
import java.util.*;
class TreeSetDemo2
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet(new MyComparator());
ts.add(new Student("zhangsan",12));
ts.add(new Student("zhangsan02",15));
ts.add(new Student("zhangsan03",12));
ts.add(new Student("zhangsan33",12));
ts.add(new Student("zhangsan33",16));
for(Iterator it=ts.iterator();it.hasNext();)
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"....."+stu.getAge());
}
}
}
class Student
{
private int age;
private String name;
public Student(String name ,int age)
{
this.age=age;
this.name=name;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
int num= s1.getName().compareTo (s2.getName());
if(num==0)
{
return new Integer(s1.getAge()).compareTo (new Integer(s2.getAge()));
}
return num;
}
}
浙公网安备 33010602011771号