一、集合(容器)
可以动态保存任意多个对象,提供操作对象的方法如add,remove,set,get等;
Map(接口):实现类HashMap,LinkedHashMap,Hashtable,Properties,TreeMap
Collection(接口):List(接口):实现类Vector,ArrayList,LinkedList Set(接口):实现类TreeSet,HashSet
二、Collection(接口)
方法:add,remove(删),contains(查boolean),size,isEmpty(boolean),clear,addAll(集合/列表),containsAll,removeAll;
遍历集合,首先要得到它的迭代器
Iterator iterator = col.iterator();
while (iterator.hasNext()) { //while循环遍历
Object next = iterator.next();
}
或者这样遍历集合(我比较喜欢)
for (Object x:a)
System.out.print(x+" ");
@SuppressWarnings({"all"})
@Test
public void test(){
Dog dog = new Dog("a1",10);
Dog dog1 = new Dog("a2",11);
Dog dog2 = new Dog("a3",12);
ArrayList arrayList = new ArrayList();
arrayList.add(dog);
arrayList.add(dog1);
arrayList.add(dog2);
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
Object d = iterator.next();
System.out.println(((Dog)d).toString());
}
for (Object x:arrayList){
System.out.println(((Dog)x).toString());
}
}
class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二、List(接口)
实现了 List接口的方法类的容器都是有序的且可重复,支持下标索引搜索。
除了实现了Collection(接口)的方法,这些方法可以指定位置,还有 indexOf(),lastIndexOf(),set(int ,Object)替换,
subList(int from,int to) //返回从from 到 to 位置的子集合,[from,to)
//多加一种遍历方式
for (int i=0;i<arrayList.size();i++){
System.out.println(((Dog)arrayList.get(i)).toString());
}
三、ArrayList、Vector、LinkedList (实现类)
ArrayList 不安全,效率高;无参+10,再1.5;有参,之后再1.5;
Vector 安全,效率不高;无参+10,再2;有参,之后再2;
LinkedList 不安全,没实现同步,实现了双向链表和双端队列特点
四、Set(接口)
无序,没有索引,不允许重复元素,最多能有一个null
五、HashSet、TreeSet、LinkedHashSet (实现类,无排序,除了TreeSet)
@SuppressWarnings({"all"})
@Test
public void test(){
Employee employee = new Employee("a",1,new MyDate(1,1,1));
Employee employee1 = new Employee("a",2,new MyDate(1,1,1));
Employee employee2 = new Employee("a",2,new MyDate(1,1,1));
HashSet hashSet = new HashSet();
hashSet.add(employee);
hashSet.add(employee1);
hashSet.add(employee2);
Iterator iterator = hashSet.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
System.out.println(((Employee)next).toString());
}
}
class Employee{
private String name;
private double sal;
private MyDate birthday;
public Employee(String name, double sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(name, employee.name) && Objects.equals(birthday, employee.birthday);
}
@Override
public int hashCode() {
return Objects.hash(name, birthday);
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
}
class MyDate{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyDate myDate = (MyDate) o;
return year == myDate.year && month == myDate.month && day == myDate.day;
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
@SuppressWarnings({"all"})
@Test
public void test(){
TreeSet treeSet = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
//按长度大小排序,但是因为是set,长度相同的就不能插入了,因为它的底层代码是相同的,就返回原来的值
return ((String)o1).length()-((String)o2).length();
}
});
treeSet.add("12345");
treeSet.add("123");
treeSet.add("2342");
treeSet.add("a");
System.out.println(treeSet);
}
六、Map(接口)
方法:put(Object,Object) remove,get,size,isEmpty,clear,containsKey
s.keySet(),s.values()、s.entrySet() //返回所有key,返回所有value,返回所有key-value
@SuppressWarnings({"all"})
@Test
public void test(){
HashMap hashMap = new HashMap();
hashMap.put(1,1);
hashMap.put("1",1);
hashMap.put("1","1");
hashMap.put(1,"1");
//1
Set set = hashMap.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
Object key = iterator.next();
System.out.println(key+" "+hashMap.get(key));
}
//2
for (Object key:set){
System.out.println(key+" "+hashMap.get(key));
}
//3
Collection values = hashMap.values();
for (Object x:values){
System.out.println(x);
}
//4
Set set1 = hashMap.entrySet();
for (Object entry:set1){
Map.Entry m=(Map.Entry)entry;
System.out.println(m.getKey()+" "+m.getValue());
}
}
七、HashMap、HashTable、Properties(实现类,无排序)
HashMap 线程不安全,key,value,可以为null,效率高
HashTable 线程安全,key 和 value不能为null,效率较低
Properties 读取文件,继承 HashTable
八、TreeSet、TreeMap
它们提供一个构造器,能传进一个比较器(匿名内部类),每次添加元素的时候,就会根据这个匿名内部类中的比较方法,进行比较,插入。
九、Collections 工具类
一个操作 Set、List 和 Map 等集合的工具类,提供了静态方法对集合元素的排序,查询和修改。
方法:Collections.reverse()、Collections.shuffle()、Collections.sort()、Collections.swap(List,int,int)、
Collections.max()、Collections.min()、Collections.frequency(List,Object)、Collections.replaceAll();