collection
-
collection 集合的创建
collection coll=new arraylist(); -
接口的常用方法
- add() 添加元素
- addall() 添加另外一个collection集合
- isempty(); 判断集合是否为空
- size() 获取集合长度
- clear() 清除集合所有元素
- contains() 是否包含某个元素
- containsall() 判断一个集合是否包含另外一个集合元素
- removeall() 清楚集合内和另外一个集合的共同元素
- retainsall() 获取一个集合和另外一个集合交集
- equals() 只需判断另外一个集合元素值是否一样
- hascode() 求集合的哈希值
- toarray() 将集合转化为数组
- iterator() 创建集合迭代器
- collection集合和数组之间的转换
集合到数组 toarray()
数组到集合 list list=arrays.aslist(new int[]{123,456});
- 会用
- 熟悉源码
遍历collection 的两种遍历
Collection collection=new ArrayList();
collection.add(123);
collection.add(124);
collection.add(788);
collection.forEach(System.out::println);
- 使用iterator迭代器
Collection collection=new ArrayList();
collection.add(123);
collection.add(124);
collection.add(788);
//collection.forEach(System.out::println);
Iterator iterator=collection.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
迭代器remove的使用
set
数据存储特点
|----collection 接口:单例集合,用来存储一个一个对象
|----list接口:存储有序的可重复的数据–》动态数组
|----arraylist:作为list的主要实现类,线程不安全效率高
|----linkedlist 对于频繁的插入删除操作,使用效率比arraylist高,使用双向列表存储
|----vector作为list接口的老实现类,线程是安全的,执行效率低
常用方法
- add
- remove
- set
- get
- add(index,object )
- size()
- 遍历 iterator
存储元素的要求
添加的对象所在的类要重写equals方法
面试题
arraylist lickedlist vector异同
同:三个类都实现了list接口 存储数据的特点相同 存储有序的可重复数据
不同:略
set 接口
数据存储特点
存储无序的 不可重复的 具体的数据-
- 无序 不可重复
hashset添加过程
set常见方法
- set中没有提供的额外方法 都是collection继承的
常用实现类
arraylist| linkedlist vector
treeset
- 自然排序中,比较两个对象是否相同的标准为compareto() 返回值0不再是equals
- 定制排序中 比较两个对象是否相同的标准为compareto() 返回值0不再是equals
使用说明1
- 自然排序
package treeset练习;
import java.util.Iterator;
import java.util.TreeSet;
public class start {
public static void main(String[] args) {
TreeSet treeSet=new TreeSet();
Employee e1=new Employee("liudehua", 56,new Mydate(1987, 2, 21));
Employee e2=new Employee("ehoujielun", 46,new Mydate(1947, 2, 21));
Employee e3=new Employee("phangxueyou", 52,new Mydate(1937, 2, 21));
Employee e4=new Employee("guofucheng", 54,new Mydate(1987, 2, 25));
Employee e5=new Employee("niangchaowei", 50,new Mydate(1985, 2, 21));
treeSet.add(e1);
treeSet.add(e2);
treeSet.add(e3);
treeSet.add(e4);
treeSet.add(e5);
Iterator iterator =treeSet.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
// 存放日期
class Mydate {
private int year;
private int month;
private int day;
public Mydate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
// public Mydate() {
// super();
// }
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
class Employee implements Comparable{
@Override
public int compareTo(Object o) {
// // TODO Auto-generated method stub
// return 0;
if(o instanceof Employee) {
Employee employee=(Employee)o;
return this.name.compareTo(employee.name);
}
else {
return 0;
}
}
private String name;
private int age;
private Mydate birthday;
public Employee(String name, int age, Mydate birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}
// public Employee() {
// super();
// }
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;
}
public Mydate getBirthday() {
return birthday;
}
public void setBirthday(Mydate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
}
}
- 定制排序