集合( LinkedList,HashSet,Collection ) 总结

来源:互联网 发布:鼠标编程是什么意思 编辑:程序博客网 时间:2024/05/16 14:22

List集合存储数据的结构:数据存储的常用结构有:堆栈队列数组链表

特点:

1.堆栈先进后出(即,存进去的元素,要在后面的元素依次取出后,才能取出该元素) 例如:子弹压弹夹;栈的入口/出口都在栈的顶端位置弹栈:就是取元素(即把栈的顶端位置取出,栈中已有的元素向栈的顶方向移动)

2.队列先进先出(即,存进去的元素,要在后面的元素依次取出后,才能取出该元素)例如:安检,依次检查;队列的入口、出口各占一侧.

3.数组:查找元素快,增删慢。

 * 数组: * 查询快:数组是有首地址,我们通过首地址可以快速找到数组对象,通过数组的索引快速找到数组中的每一个元素;数组地址是连续的arr[0] arr[1] * 增删慢:数组的长度是固定的,我们想要给数组添加一个元素或减少一个元素,必须新建一个新数组,把原数组的元素复制到新的数组中 *    在内存中,频繁的建数组,复制数组,销毁数组,效率低,速度慢 *      底层原理的实现 *      public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { *        T[] copy = ((Object)newType == (Object)Object[].class) *           ? (T[]) new Object[newLength] *          : (T[]) Array.newInstance(newType.getComponentType(), newLength); *        System.arraycopy(original, 0, copy, 0,Math.min(original.length, newLength)); *        return copy; *      }

4.链表:多个节点之间,通过地址进行连接,查找慢,增删快

链表的每一个元素,称之为一个节点(由两个指针域和一个数据域组成)
单向链表:只有一条链子,元素是无序的,存储元素和取出的元素的顺序可能不一致;存储a,b,c 取出可能b,c,a
使用单向链表的集合:HashSet,HashMap
双向链表:有两条链子,上一个的元素会记住下一个元素的地址;下一个元素也会记住上一个元素的地址;是有序的,存和取都是一致的
使用双向链表的集合:LinkedHashSet,LinkedHashMap

public class LinkedListDemo {public static void main(String[] args) {show3();}/** E removeFirst() 移除并返回此列表的第一个元素。* E pop() 移除并返回此列表的第一个元素。 此方法等效于 removeFirst()。* E removeLast() 移除并返回此列表的最后一个元素。*/private static void show3() {LinkedList<String> linked = new LinkedList<String>();//往集合的末尾追加元素linked.add("aaa");linked.add("bbb");linked.add("ccc");linked.add("dddd");System.out.println(linked);String removeFirst = linked.removeFirst();System.out.println("返回移除元素:" + removeFirst);System.out.println(linked);String removeLast = linked.removeLast();System.out.println("返回移除元素:" + removeLast);System.out.println(linked);}/** E getFirst() 返回此列表的第一个元素。* E getLast() 返回此列表的最后一个元素。* boolean isEmpty() 如果列表不包含元素,则返回 true。 */private static void show2() {//创建LinkedList对象LinkedList<String> linked = new LinkedList<String>();//往集合的末尾追加元素linked.add("aaa");linked.add("bbb");linked.add("ccc");linked.add("dddd");//linked.clear();if(!linked.isEmpty()){String first = linked.getFirst();System.out.println("first:" + first);String last = linked.getLast();System.out.println("last:" + last);}}/** void addFirst(E e) 将指定元素插入此列表的开头。* void push(E e) 将该元素插入此列表的开头。 此方法等效于 addFirst(E)。* void addLast(E e) 将指定元素添加到此列表的结尾。相当于 add(E e)*/private static void show1() {LinkedList<String> linkedList01 = new LinkedList<String>();linkedList01.add("aaa");linkedList01.add("bbb");linkedList01.add("ccc");System.out.println(linkedList01);//void addFirst(E e) 将指定元素插入此列表的开头。linkedList01.addFirst("ddd");linkedList01.push("eee");//void push(E e) 将该元素插入此列表的开头。 此方法等效于 addFirst(E)。System.out.println(linkedList01);System.out.println("--------------------");LinkedList<String> linkedList02 = new LinkedList<String>();linkedList02.addLast("b");linkedList02.addLast("c");linkedList02.addLast("d");linkedList02.addFirst("3");linkedList02.addFirst("2");linkedList02.addFirst("1");System.out.println(linkedList02);}}
HashSet接口特点:

  1.底层是个哈希表(数组+链表); 查询快,又增删快

  2.是一个无序的集合,储存元素和取出的可能不一致 

  此类为基本操作提供了稳定性能,这些基本操作包括 add、remove、contains 和 size,

  假定哈希函数将这些元素正确地分布在桶中。对此 set 进行迭代所需的时间与 HashSet 实例的大小(元素的数量)和底层 HashMap 

  实例(桶的数量)的“容量”的和成比例。因此,如果迭代性能很重要,则不要将初始容量设置得太高(或将加载因子设置得太低)。 
public class HashSetDemo {public static void main(String[] args) {HashSet<String> hashSet = new HashSet<String>();hashSet.add("张三");hashSet.add("李四");hashSet.add("王五");hashSet.add("赵六");//使用迭代器Iterator<String> iterator = hashSet.iterator();while (iterator.hasNext()) {String string = (String) iterator.next();System.out.println(string);}System.out.println("-----------");for (String string : hashSet) {System.out.println(string);}//赵六//张三//李四//王五}}
java.util.LinkedHashSet集合 extends HashSet implments Set接口
  特点:
   1.不允许重复元素
   2.底层是哈希表(数组+单向链表) + 单向链表组成,就是一个双向链表,新添加的链表就可以保证迭代顺序
public class LinkedHashSetDemo {public static void main(String[] args) {LinkedHahSetM();//保证ArrayList集合中元素不重复ArrayList<Integer> arrayList = new ArrayList<Integer>();arrayList.add(1);arrayList.add(1);arrayList.add(1);System.out.println(arrayList);//[1, 1, 1]LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>();for (Integer integer : arrayList) {hashSet.add(integer);}System.out.println(hashSet);//[1]}private static void LinkedHahSetM() {//不允许重复,无序的HashSet<String> hashSet = new HashSet<String>();hashSet.add("杨贵妃");hashSet.add("貂蝉");hashSet.add("貂蝉");hashSet.add("西施");System.out.println(hashSet);//[西施, 杨贵妃, 貂蝉]System.out.println("--------");//不允许重复,有序的LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();linkedHashSet.add("杨贵妃");linkedHashSet.add("貂蝉");linkedHashSet.add("貂蝉");linkedHashSet.add("西施");System.out.println(linkedHashSet);//[杨贵妃, 貂蝉, 西施]}}
5.哈希值

  哈希值:是一个十进制的整数,由操作系统随机给的
  注意:如果给出的这个哈希值,没有其它使用,那么会重复给出
  Object类中有一个方法叫 HashCode
intHashCode  返回该对象的哈希码值

public class HashCodeDemo {public static void main(String[] args) {personHashCode();}private static void personHashCode() {Person person = new Person();int hashCode = person.hashCode();System.out.println(hashCode);//1321522194/* *    xinfei.code.data.Demo_03.Person@5c3f1224      把十进制的整数转换为十六进制 * 反射return getClass().getName()  + @ + Interger.toHexString(HashCode()); */System.out.println(person);}}
HashSet储存数据不重复原理
/* * HashSetc存储数据不重复的原理 */public class StringHashCodeDemo {public static void main(String[] args) {String str01 = new String("abc");String str02 = new String("abc");String str03 = "abc";System.out.println(str01.hashCode());//96354System.out.println(str02.hashCode());//96354System.out.println(str03.hashCode());//96354}}
String类重写Object类的HashCode方法;String类底层是一个字符数组:用数组存储多个字符
private final char value[];private int hash; // Default to 0public int hashCode() {        int h = hash;        if (h == 0 && value.length > 0) {            char val[] = value;            for (int i = 0; i < value.length; i++) {                h = 31 * h + val[i];            }            hash = h;        }        return h; }
//第一次遍历:h = 31*h+val[0] = 31*0+'a' = 97//第二次遍历:h = 31*h+val[1] = 31*97+'b' = 3007 + 98 = 3105/第三次遍历:h = 31*h+val[2] = 31*3105+'c' = 96255 + 99 = 96354

阅读全文
0 0
原创粉丝点击