Java 集合

集合类

    出现原因 //存储对象
    与数组区别 //数组长度固定 集合长度可变 数组可存储基本数据类型 集合只能存储对象
    特点 //可存储不同类型对象 长度可变

集合框架
集合容器因为内部的数据结构不同,有多种具体容器。
不断的向上抽取,就形成了集合框架。

框架的顶层Collection接口:

Collection的常见方法:

1,添加。
    boolean add(Object obj):
    boolean addAll(Collection coll):
    

2,删除。
    boolean remove(object obj):
    boolean removeAll(Collection coll);
    void clear();
    
3,判断:
    boolean contains(object obj):
    boolean containsAll(Colllection coll);
    boolean isEmpty():判断集合中是否有元素。

4,获取:
    int size():
    Iterator iterator():取出元素的方式:迭代器。
    该对象必须依赖于具体容器,因为每一个容器的数据结构都不同。
    所以该迭代器对象是在容器中进行内部实现的。
    对于使用容器者而言,具体的实现不重要,只要通过容器获取到该实现的迭代器的对象即可,
    也就是iterator方法。
    
    Iterator接口就是对所有的Collection容器进行元素取出的公共接口。
    其实就是抓娃娃游戏机中的夹子!
    
    
5,其他:
    boolean retainAll(Collection coll);取交集。
    Object[] toArray():将集合转成数组。

Demo1 CollectionDemo

public class CollectionDemo {
  public static void main(String[] args) {
    Collection coll = new ArrayList();

    // show(coll);

    Collection c1 = new ArrayList();
    Collection c2 = new ArrayList();
    show(c1, c2);

  }

  public static void show(Collection c1, Collection c2) {

    // 给c1添加元素。
    c1.add("abc1");
    c1.add("abc2");
    c1.add("abc3");
    c1.add("abc4");

    // 给c2添加元素。
    c2.add("abc1");
    c2.add("abc2");
    c2.add("abc3");
    c2.add("abc4");
    c2.add("abc5");

    System.out.println("c1:" + c1);
    System.out.println("c2:" + c2);

    // 演示addAll

    // c1.addAll(c2);//将c2中的元素添加到c1中。

    // 演示removeAll
    // boolean b = c1.removeAll(c2);//将两个集合中的相同元素从调用removeAll的集合中删除。
    // System.out.println("removeAll:"+b);

    // 演示containsAll
    // boolean b = c1.containsAll(c2);
    // System.out.println("containsAll:"+b);

    // 演示retainAll
    boolean b = c1.retainAll(c2);// 取交集,保留和指定的集合相同的元素,而删除不同的元素。
    // 和removeAll功能相反 。
    System.out.println("retainAll:" + b);
    System.out.println("c1:" + c1);

  }

  public static void show(Collection coll) {

    // 1,添加元素。add.
    coll.add("abc1");
    coll.add("abc2");
    coll.add("abc3");
    System.out.println(coll);

    // 2,删除元素。remove
    // coll.remove("abc2");//会改变集合的长度

    // 清空集合.
    // coll.clear();

    System.out.println(coll.contains("abc3"));

    System.out.println(coll);

  }
}

Demo2 迭代器

public class IteratorDemo {
  public static void main(String[] args) {

    Collection coll = new ArrayList();
    coll.add("abc1");
    coll.add("abc2");
    coll.add("abc3");
    coll.add("abc4");

    // System.out.println(coll);

    // 使用了Collection中的iterator()方法。 调用集合中的迭代器方法,是为了获取集合中的迭代器对象。
    // Iterator it = coll.iterator();
    // while(it.hasNext()){
    // System.out.println(it.next());
    // }

    for (Iterator it = coll.iterator(); it.hasNext();) {
      System.out.println(it.next());
    }

    // System.out.println(it.next());
    // System.out.println(it.next());
    // System.out.println(it.next());
    // System.out.println(it.next());
    // System.out.println(it.next());//java.util.NoSuchElementException

  }
}

Collection
    |--List:有序(存入和取出的顺序一致),元素都有索引(角标),元素可以重复。
    |--Set:元素不能重复,无序。

List:特有的常见方法:有一个共性特点就是都可以操作角标。
    
1,添加
    void add(index,element);
    void add(index,collection);

2,删除;
    Object remove(index):

3,修改:
    Object set(index,element);
   
4,获取:
    Object get(index);
    int indexOf(object);
    int lastIndexOf(object);
    List subList(from,to);
   
list集合是可以完成对元素的增删改查。

Demo3

public class ListDemo {
  public static void main(String[] args) {
    List list = new ArrayList();
    show(list);
  }

  public static void show(List list) {

    // 添加元素
    list.add("abc1");
    list.add("abc2");
    list.add("abc3");
    System.out.println(list);

    // 插入元素。
    // list.add(1,"abc9");

    // 删除元素。
    // System.out.println("remove:"+list.remove(2));

    // 修改元素。
    // System.out.println("set:"+list.set(1, "abc8"));

    // 获取元素。
    // System.out.println("get:"+list.get(0));

    // 获取子列表。
    // System.out.println("sublist:"+list.subList(1, 2));

    System.out.println(list);

  }
}

Demo4

public class ListDemo2 {
  public static void main(String[] args) {

    List list = new ArrayList();
//  show(list);
    
    list.add("abc1");
    list.add("abc2");
    list.add("abc3");
    
    System.out.println("list:"+list);
    ListIterator it = list.listIterator();//获取列表迭代器对象
    //它可以实现在迭代过程中完成对元素的增删改查。
    //注意:只有list集合具备该迭代功能.
    
    while(it.hasNext()){
      
      Object obj = it.next();
      
      if(obj.equals("abc2")){
        it.set("abc9");
      }
    }
//    System.out.println("hasNext:"+it.hasNext());
//    System.out.println("hasPrevious:"+it.hasPrevious());
    
    
    while(it.hasPrevious()){
      System.out.println("previous:"+it.previous());
    }
    System.out.println("list:"+list);
    
    /*Iterator it = list.iterator();
    while(it.hasNext()){
      
      Object obj = it.next();//java.util.ConcurrentModificationException
              //在迭代器过程中,不要使用集合操作元素,容易出现异常。
            //可以使用Iterator接口的子接口ListIterator来完成在迭代中对元素进行更多的操作。 
      
      if(obj.equals("abc2")){
        list.add("abc9");
      }
      else
        System.out.println("next:"+obj);
    }
    System.out.println(list);
    */

  }

  public static void show(List list) {
    
    list.add("abc1");
    list.add("abc2");
    list.add("abc3");
    list.add("abc4");
    
    Iterator it = list.iterator();
    while(it.hasNext()){
      System.out.println("next:"+it.next());
    }
    
    //list特有的取出元素的方式之一。
    
    for(int x=0; x<list.size(); x++){
      System.out.println("get:"+list.get(x));
    }
  }

}

List:
    |--Vector:内部是数组数据结构,是同步的。增删,查询都很慢!
    |--ArrayList:内部是数组数据结构,是不同步的。替代了Vector。查询的速度快。
    |--LinkedList:内部是链表数据结构,是不同步的。增删元素的速度很快。

Demo5 Vector中的elements()方法

public class VectorDemo {
  public static void main(String[] args) {

    Vector v = new Vector();
    
    v.addElement("abc1");
    v.addElement("abc2");
    v.addElement("abc3");
    v.addElement("abc4");
    
    Enumeration en = v.elements();
    while(en.hasMoreElements()){
      System.out.println("nextelment:"+en.nextElement());
    }
    
    Iterator it = v.iterator();
    
    while(it.hasNext()){
      System.out.println("next:"+it.next());
    }
    
  }
}

LinkedList:

    addFirst();
    addLast():
    jdk1.6
    offerFirst();
    offetLast();
   
    getFirst();.//获取但不移除,如果链表为空,抛出NoSuchElementException.
    getLast();
    jdk1.6
    peekFirst();//获取但不移除,如果链表为空,返回null.
    peekLast():
    
    removeFirst();//获取并移除,如果链表为空,抛出NoSuchElementException.
    removeLast();
    jdk1.6
    pollFirst();//获取并移除,如果链表为空,返回null.
    pollLast();

Demo6 LinkedListDemo

public class LinkedListDemo {
  public static void main(String[] args) {

    LinkedList link = new LinkedList();
    
    link.addFirst("abc1");
    link.addFirst("abc2");
    link.addFirst("abc3");
    link.addFirst("abc4");
//    System.out.println(link);
//    System.out.println(link.getFirst());//获取第一个但不删除。
//    System.out.println(link.getFirst());
    
//    System.out.println(link.removeFirst());//获取元素但是会删除。
//    System.out.println(link.removeFirst());
    
    while(!link.isEmpty()){
      System.out.println(link.removeLast());
    }
    
    
    System.out.println(link);
//    Iterator it = link.iterator();
//    while(it.hasNext()){
//      System.out.println(it.next());
//    }
  }
}

Demo7 请使用LinkedList来模拟一个堆栈或者队列数据结构

堆栈:先进后出 First In Last Out  FILO
队列:先进先出 First In First Out FIFO

public class LinkedTest {
  public static void main(String[] args) {
    
    DuiLie dl = new DuiLie();
    
    dl.myAdd("abc1");
    dl.myAdd("abc2");
    dl.myAdd("abc3");
    dl.myAdd("abc4");
    
    while(!dl.isNull()){
      System.out.println(dl.myGet());
    }
    
    
  }
}

class DuiLie {
  private LinkedList link;

  public DuiLie() {
    link = new LinkedList();
  }

  /**
   * 队列的添加元素的功能。
   */
  public void myAdd(Object obj) {
    link.addLast(obj);
  }

  public Object myGet() {
    return link.removeFirst();
  }

  public boolean isNull() {
    return link.isEmpty();
  }
}

 

 

Set:元素不可以重复,是无序。
    Set接口中的方法和Collection一致。
    |--HashSet: 内部数据结构是哈希表 ,是不同步的。
        如何保证该集合的元素唯一性呢?
        是通过对象的hashCode和equals方法来完成对象唯一性的。
        如果对象的hashCode值不同,那么不用判断equals方法,就直接存储到哈希表中。
        如果对象的hashCode值相同,那么要再次判断对象的equals方法是否为true。
        如果为true,视为相同元素,不存。如果为false,那么视为不同元素,就进行存储。
        
        记住:如果元素要存储到HashSet集合中,必须覆盖hashCode方法和equals方法。
        一般情况下,如果定义的类会产生很多对象,比如人,学生,书,通常都需要覆盖equals,hashCode方法。
        建立对象判断是否相同的依据。

if(this.hashCode()== obj.hashCode() && this.equals(obj))
哈希表确定元素是否相同
1,判断的是两个元素的哈希值是否相同。
    如果相同,在判断两个对象的内容是否相同。

2,判断哈希值相同,其实判断的是对象的hashCode的方法。判断内容相同,用的是equals方法。

注意:如果哈希值不同,是不需要判断equals。

Demo1

public class HashSetDemo {
  public static void main(String[] args) {

    HashSet hs = new HashSet();
    
    hs.add("hehe");
//    hs.add("heihei");
    hs.add("hahah");
    hs.add("xixii");
    hs.add("hehe");
    
    Iterator it = hs.iterator();
    
    while(it.hasNext()){
      System.out.println(it.next());
    }
  }
}

Demo2

public class LinkedHashSetDemo {
  public static void main(String[] args) {

    HashSet hs = new LinkedHashSet();
    
    hs.add("hahah");
    hs.add("hehe");
    hs.add("heihei");
    hs.add("xixii");
//    hs.add("hehe");
    
    Iterator it = hs.iterator();
    
    while(it.hasNext()){
      System.out.println(it.next());
    }
  }
}

Demo3

public class HashSetTest {

  public static void main(String[] args) {

    HashSet hs = new HashSet();

    /*
     * HashSet集合数据结构是哈希表,所以存储元素的时候,
     * 使用的元素的hashCode方法来确定位置,如果位置相同,在通过元素的equals来确定是否相同。
     */

    hs.add(new Person("lisi4", 24));
    hs.add(new Person("lisi7", 27));
    hs.add(new Person("lisi1", 21));
    hs.add(new Person("lisi9", 29));
    hs.add(new Person("lisi7", 27));

    Iterator it = hs.iterator();

    while (it.hasNext()) {
      Person p = (Person) it.next();
      System.out.println(p);
      // System.out.println(p.getName()+"...."+p.getAge());
    }
  }

}

class Person implements Comparable {

  private String name;
  private int age;

  public Person() {
    super();

  }

  public Person(String name, int age) {
    super();
    this.name = name;
    this.age = age;
  }

  @Override
  public int hashCode() {
    // System.out.println(this+".......hashCode");

    return name.hashCode() + age * 27;
    // return 100;
  }

  @Override
  public boolean equals(Object obj) {

    if (this == obj)
      return true;
    if (!(obj instanceof Person))
      throw new ClassCastException("类型错误");

    // System.out.println(this+"....equals....."+obj);
    Person p = (Person) obj;

    return this.name.equals(p.name) && this.age == p.age;
  }

  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 String toString() {
    return name + ":" + age;
  }

  @Override
  public int compareTo(Object o) {

    Person p = (Person) o;

    int temp = this.age - p.age;
    return temp == 0 ? this.name.compareTo(p.name) : temp;

    // int temp = this.name.compareTo(p.name);
    // return temp==0?this.age-p.age:temp;

    /*
     * if(this.age>p.age) return 1; if(this.age<p.age) return -1;
     * 
     * else{
     * 
     * return this.name.compareTo(p.name); }
     */

  }

}

 

    |--TreeSet:可以对Set集合中的元素进行排序。是不同步的。
                判断元素唯一性的方式:就是根据比较方法的返回结果是否是0,是0,就是相同元素,不存。
                
                TreeSet对元素进行排序的方式一:
                让元素自身具备比较功能,元就需要实现Comparable接口。覆盖compareTo方法。
                
                如果不要按照对象中具备的自然顺序进行排序。如果对象中不具备自然顺序。怎么办?
                可以使用TreeSet集合第二种排序方式二:
                让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。
                将该类对象作为参数传递给TreeSet集合的构造函数。

Demo4

public class TreeSetDemo {
  public static void main(String[] args) {

    TreeSet ts = new TreeSet(new ComparatorByName());

    /*
     * 以Person对象年龄进行从小到大的排序。
     */

    ts.add(new Person("zhangsan", 28));
    ts.add(new Person("lisi", 21));
    ts.add(new Person("zhouqi", 29));
    ts.add(new Person("zhaoliu", 25));
    ts.add(new Person("wangu", 24));

    Iterator it = ts.iterator();

    while (it.hasNext()) {
      Person p = (Person) it.next();

      System.out.println(p.getName() + ":" + p.getAge());
    }

  }

  /**
   * 
   */
  public static void demo1() {
    TreeSet ts = new TreeSet();

    ts.add("abc");
    ts.add("zaa");
    ts.add("aa");
    ts.add("nba");
    ts.add("cba");

    Iterator it = ts.iterator();

    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }

}

/**
 * 创建了一个根据Person类的name进行排序的比较器。
 */
class ComparatorByName implements Comparator {

  @Override
  public int compare(Object o1, Object o2) {

    Person p1 = (Person)o1;
    Person p2 = (Person)o2;
    
    int temp = p1.getName().compareTo(p2.getName());
    
    return temp==0?p1.getAge()-p2.getAge(): temp;
//    return 1;//有序。
  }

}

class Person implements Comparable {

  private String name;
  private int age;

  public Person() {
    super();

  }

  public Person(String name, int age) {
    super();
    this.name = name;
    this.age = age;
  }

  @Override
  public int hashCode() {
    // System.out.println(this+".......hashCode");

    return name.hashCode() + age * 27;
    // return 100;
  }

  @Override
  public boolean equals(Object obj) {

    if (this == obj)
      return true;
    if (!(obj instanceof Person))
      throw new ClassCastException("类型错误");

    // System.out.println(this+"....equals....."+obj);
    Person p = (Person) obj;

    return this.name.equals(p.name) && this.age == p.age;
  }

  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 String toString() {
    return name + ":" + age;
  }

  @Override
  public int compareTo(Object o) {

    Person p = (Person) o;

    int temp = this.age - p.age;
    return temp == 0 ? this.name.compareTo(p.name) : temp;

    // int temp = this.name.compareTo(p.name);
    // return temp==0?this.age-p.age:temp;

    /*
     * if(this.age>p.age) return 1; if(this.age<p.age) return -1;
     * 
     * else{
     * 
     * return this.name.compareTo(p.name); }
     */

  }

}

 

Demo5 对字符串进行长度排序

public class TreeSetTest {
  public static void main(String[] args) {

    // 对字符串进行长度排序。
    TreeSet ts = new TreeSet(new ComparatorByLength());

    ts.add("aaaaa");
    ts.add("zz");
    ts.add("nbaq");
    ts.add("cba");
    ts.add("abc");
    
    Iterator it = ts.iterator();
    
    while(it.hasNext()){
      System.out.println(it.next());
    }
  }

}

class ComparatorByLength implements Comparator {

  @Override
  public int compare(Object o1, Object o2) {
    
    String s1 = (String)o1;
    String s2 = (String)o2;
    
    int temp = s1.length()-s2.length();
    
    return temp==0? s1.compareTo(s2): temp;
  }

}

 

集合的一些技巧:

需要唯一吗?
需要:Set
    需要制定顺序:
            需要: TreeSet
            不需要:HashSet
            但是想要一个和存储一致的顺序(有序):LinkedHashSet
不需要:List
    需要频繁增删吗?
        需要:LinkedList
        不需要:ArrayList
        
如何记录每一个容器的结构和所属体系呢?

看名字!

List
    |--ArrayList
    |--LinkedList

Set
    |--HashSet
    |--TreeSet

后缀名就是该集合所属的体系。

前缀名就是该集合的数据结构。

看到array:就要想到数组,就要想到查询快,有角标.    
看到link:就要想到链表,就要想到增删快,就要想要 add get remove+frist last的方法
看到hash:就要想到哈希表,就要想到唯一性,就要想到元素需要覆盖hashcode方法和equals方法。
看到tree:就要想到二叉树,就要想要排序,就要想到两个接口Comparable,Comparator 。

而且通常这些常用的集合容器都是不同步的。

Map:一次添加一对元素。Collection 一次添加一个元素。
    Map也称为双列集合,Collection集合称为单列集合。
    其实map集合中存储的就是键值对。
    map集合中必须保证键的唯一性。
    
    
常用方法:
1,添加。
    value put(key,value):返回前一个和key关联的值,如果没有返回null.

2,删除。
    void  clear():清空map集合。
    value remove(key):根据指定的key翻出这个键值对。

3,判断。
    boolean containsKey(key):
    boolean containsValue(value):
    boolean isEmpty();

4,获取。
    value get(key):通过键获取值,如果没有该键返回null。
                    当然可以通过返回null,来判断是否包含指定键。
    int size(): 获取键值对的个数。
    
                        
    
Map常用的子类:
    |--Hashtable :内部结构是哈希表,是同步的。不允许null作为键,null作为值。
        |--Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。         
    |--HashMap : 内部结构是哈希表,不是同步的。允许null作为键,null作为值。
    |--TreeMap : 内部结构是二叉树,不是同步的。可以对Map集合中的键进行排序。

Demo1

public class MapDemo {
public static void main(String[] args) {
    
    Map<Integer,String> map = new HashMap<Integer,String>();
    method_2(map);
  }
  
  public static void method_2(Map<Integer,String> map){
    
    map.put(8,"zhaoliu");
    map.put(2,"zhaoliu");
    map.put(7,"xiaoqiang");
    map.put(6,"wangcai");
    
    
    Collection<String> values = map.values();
    
    Iterator<String> it2 = values.iterator();
    while(it2.hasNext()){
      System.out.println(it2.next());
    }
    
    
    
    /*
     * 通过Map转成set就可以迭代。
     * 找到了另一个方法。entrySet。
     * 该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型(结婚证)
     * 
     * 
     */
    Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
    
    Iterator<Map.Entry<Integer, String>> it = entrySet.iterator();
    
    while(it.hasNext()){
      Map.Entry<Integer, String> me = it.next();
      Integer key = me.getKey();
      String value = me.getValue();
      System.out.println(key+"::::"+value);
      
    }

    //取出map中的所有元素。
    //原理,通过keySet方法获取map中所有的键所在的Set集合,在通过Set的迭代器获取到每一个键,
    //在对每一个键通过map集合的get方法获取其对应的值即可。
    /*
    Set<Integer> keySet = map.keySet();
    Iterator<Integer> it = keySet.iterator();
    
    while(it.hasNext()){
      Integer key = it.next();
      String value = map.get(key);
      System.out.println(key+":"+value);
      
    }
    */
  }
}

Demo2

 

public class HashMapDemo {
public static void main(String[] args) {
    /*
     * 将学生对象和学生的归属地通过键与值存储到map集合中。
     * 
     */
    
    HashMap<Student,String> hm = new HashMap<Student,String>();

    
    hm.put(new Student("lisi",38),"北京");
    hm.put(new Student("zhaoliu",24),"上海");
    hm.put(new Student("xiaoqiang",31),"沈阳");
    hm.put(new Student("wangcai",28),"大连");
    hm.put(new Student("zhaoliu",24),"铁岭");
    
//    Set<Student> keySet = hm.keySet();    
//    Iterator<Student> it = keySet.iterator();
    
    Iterator<Student> it = hm.keySet().iterator();
    
    while(it.hasNext()){
      Student key = it.next();
      String value = hm.get(key);
      System.out.println(key.getName()+":"+key.getAge()+"---"+value);
    }
    
  }
}

class Student extends Person {

  public Student() {
    super();
    
  }

  public Student(String name, int age) {
    super(name, age);
    
  }

  @Override
  public String toString() {
    
    return "Student:"+getName()+":"+getAge();
  }

  
}

 

Demo3

 

public class TreeMapDemo {
  public static void main(String[] args) {

    TreeMap<Student,String> tm = new TreeMap<Student,String>(new ComparatorByName());
    
    tm.put(new Student("lisi",38),"北京");
    tm.put(new Student("zhaoliu",24),"上海");
    tm.put(new Student("xiaoqiang",31),"沈阳");
    tm.put(new Student("wangcai",28),"大连");
    tm.put(new Student("zhaoliu",24),"铁岭");
    
    
    Iterator<Map.Entry<Student, String>> it = tm.entrySet().iterator();
    
    while(it.hasNext()){
      Map.Entry<Student,String> me = it.next();
      Student key = me.getKey();
      String value = me.getValue();
      
      System.out.println(key.getName()+":"+key.getAge()+"---"+value);
    }
    
  }
}

 

原文地址:https://www.cnblogs.com/huanyi0723/p/5012527.html