第十四课 集合框架

来源:互联网 发布:linux 防御ddos 编辑:程序博客网 时间:2024/05/01 16:09

1.简介

集合框架(Collections Framework ) (容器类)

    java.util包下


以下是java1.2版本之后提供的类和接口
Collection(接口)

      ---List(接口):有序可重复
           
           ----ArrayList(实现类):实现了 大小可变的数组

           ----LinkedList(实现类):链接列表实现除了实现 ,List 接口外,LinkedList   

                                 类还为在列表的开头及结尾 提供了统一的命名方法

      ---Set(接口):无序不重复

           ----HashSet(实现类):由哈希表支持  不重复

                 Object类的 hashCode():它将会给每一个对象产生一个int的数据作为散列 

                                       码。一般理想状态就是不同的对象散列码是不同的
                 判断两个对像是同一对象:
                          有个叫哈希桶 是来存放对象的地方 当我们把一个对象放入其中
                          时先会比较桶里的每一个对象的hashCode值是否与放入的对象相
                          等如果相等就进一步去比较两个对象的equals方法返回的值是否
                          为ture 如果为true 两个对象就是同一个对象。
                           所以对像在覆盖equals 方法和hashCode方法时标准要保持一致
                          (选一个能唯一代表整个对象的属性)

          ---LinkedHashSet(实现类):具有可预知迭代顺序(添加的顺序)的 Set 接口的哈希
                                   表和链接列表实 现

          ---TreeSet(实现类):使用元素的自然顺序对元素进行排序(升序)                

            
Iterator(接口):迭代器

    两个重要方法:hasNext  next

Map(接口):将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
 
     ----HashMap(实现类) 基于哈希表的 Map 接口的实现  按key无序

     ----LinkedHashMap(实现类):按照key是有序的

     ----TreeMap(实现类):按照key是升序的

jdk1.2以前的类
 
    ArrayList 和 Vector的区别

       1.当默认的初始化容量慢的情况下  ArrayList增加原来的一半
          Vector是增加原来的一倍
       2.ArrayList 是非线程安全  Vector是线程安全  ArrayList效率高一点

    HashMap和 HashTable的区别

       1.HashMap允许null的key存在 HashTable不允许

       2.HashMap是非线程安全的  HashTable是线程安全  HashMap效率高一点


    Properties:key value映射    key和value一般情况是Stirng

 

2.例子

     以下几个例子都会用到Person 所以首先给出

public class Person {
 private long id;
 private String name;
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @Override
 public int hashCode() {
  final int PRIME = 31;
  int result = 1;
  //hashCode 生成的一个算法 提高效率
  result = PRIME * result + (int) (id ^ (id >>> 32));
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)//比较地址相等 两个对象一定是同一个
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())//两个对象是否使用同一个类加载器
   return false;
  
  final Person other = (Person) obj;
  if (id != other.id)//我们自己选择的比较原则
   return false; 
  return true;
 }
}

 

1)ArrayList的例子

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestPersonArrayList {
 public static void main(String[] str){
  Person person=new Person();
  person.setId(1001);
  person.setName("yuiop");
  
  List<Person> list= new ArrayList<Person>();
  list.add(person);
      //循环迭代方式(一) 
  for(Person p:list){
   System.out.println(p.getName()+":"+p.getId());
  }
   //循环迭代方式(二)
  for(int i=0;i<list.size();i++){
   Person p=(Person)list.get(i);
   System.out.println(p.getName()+":"+p.getId());
  }
   //循环迭代方式(三)
  Iterator it=list.iterator();
  while(it.hasNext()){
   Person p=(Person)it.next();
   System.out.println(p.getName()+":"+p.getId());
  }  
 }
}

2)HashSet的例子

import java.util.HashSet;
import java.util.Set;
public class TestPersonHashSet {
 public static void main(String[] args) {  
  Person person=new Person();
  person.setId(1001);
  person.setName("yuiop");
    
  Person person1=new Person();
  person1.setId(1001);
  person1.setName("yuiop");
  
  Set<Person> set= new HashSet<Person>();
  set.add(person);
  boolean flag=set.add(person1);  
  System.out.println(flag);//false
  
  for(Person p:set){
   System.out.println(p.getName()+":"+p.getId());
  }  
 }
}

3)HashMap的例子

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class TestPersonHashMap {
 public static void main(String[] args) {
  Map<Long, Person> map = new HashMap<Long, Person>();
  Person p = new Person();
  p.setId(1001);
  p.setName("jessica");  
  Person p1 = new Person();
  p1.setId(1002);
  p1.setName("jessica");

  map.put(p.getId(), p);
  map.put(p1.getId(), p1);
  
  Set<Long> setKey = map.keySet();// 取到map集合里所有的key值

  for (Long key : setKey) {
   Person person = map.get(key);// 根据key取Value
   System.out.println("key:" + key + "  value:" + person.getName());
  }
  System.out.println("-------------------------------------");
  
  map.remove(new Long(1001));//根据key可以进行移除
  
  System.out.println(map.size());
  
  Set<Long> setKey1 = map.keySet();// 取到map集合里所有的key值
  for (Long key : setKey1) {
   Person person = map.get(key);// 根据key取Value
   System.out.println("key:" + key + "  value:" + person.getName());
  }
 }
}