List interface, Set Interface

list Interface:

An ordered set access element, which is a set with the index, there may be repeated in the collection element.

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

{class Demo01 public
  public static void main (String [] args) {
    List <String> = new new List the ArrayList <String> ();
    List.add ( "123");
    List.add ( "ABC");
    // in insert the specified position specified element
    List.add (. 1, "456");
    // delete the specified position on the element
    String list.remove S = (. 1);
    System.out.println ( "delete element is" + s);
    // the specified position on the element replacement
    String a = list.set (0, "pig Page");
    System.out.println ( "alternative elements" a +);
    // iterate
    for (int i = 0 ; I <list.size (); I ++) {
      System.out.println (List.get (I));
    }
  }
}

  Iterator concurrent modification anomalies

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

{class Demo02 public
  public static void main (String [] args) {
    List <String> new new ARR = the ArrayList <String> ();
    arr.add ( "A");
    arr.add ( "B");
    arr.add ( "C");
    // while traversing determines whether "b" element in the set, if a location is added to the "D"
    // Get iterator object
    iterator <String> it = arr.iterator ( ) ;
    the while (it.hasNext ()) {
      String it.next S = ();
      IF (s.equals ( "B")) {
        arr.add ( "D");
      }
    }
  }
}

 Storing the set of data structures List

Stack, array, queue, list

LinkedList collection

LinkedList data storage structure is a set of linked list structure

import java.util.LinkedList;
import java.util.List;

public class Demo03 {
  public static void main(String[] args) {
    LinkedList<String> arr=new LinkedList<String>();
    arr.addFirst("a");
    arr.addFirst("b");
    arr.addLast("c");
    arr.addLast("d");
    //获取集合中第一个元素
    System.out.println("集合中第一个元素为"+arr.getFirst());
    //获取集合中最后一个元素
    System.out.println("集合中最后一个元素为"+arr.getLast());
    //删除集合中第一个元素
    arr.removeFirst();
    arr.removeFirst();
    //删除集合中最后一个元素
    arr.removeLast();
    for(String s:arr){
    System.out.println(s);
  }
    if(!arr.isEmpty()){
      System.out.println("该集合不为空");
    }
  }
}

Vector集合

Vector集合数据存储的结构是数组结构,为JDK中最早提供的集合。

取出方式:枚举Enumeration。

Vector集合已被ArrayList替代,枚举Enumeration已被迭代器Iterator替代。

 

 

set接口

HashSet集合:此类实现Set接口,由哈希表支持(实际上是一个 HashMap集合)。HashSet集合不能保证的迭代顺序与元素存储顺序相同。

保证HashSet集合元素的唯一,其实就是根据对象的hashCode和equals方法来决定的。

import java.util.HashSet;

public class Demo01 {
  public static void main(String[] args) {
    HashSet<String> set=new HashSet<String>();
    set.add("a");
    set.add("a");
    set.add("b");
    //遍历
    for(String s:set){
      System.out.println(s);
    }
  }
}

public class Demo02 {
  public static void main(String[] args) {
    String a="abc";
    String b="abc";
    System.out.println(a.hashCode());
    System.out.println(b.hashCode());
  }
}

public class Demo04 {
  public static void main(String[] args) {
    LinkedHashSet<String> set=new LinkedHashSet<String>();
    set.add("abc");
    set.add("aaa");
    set.add("bcd");
    set.add("abc");
    for(String s:set){
      System.out.println(s);
    }
  }
}

 

Guess you like

Origin www.cnblogs.com/boss-H/p/10958945.html
Recommended