集合类

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0 }
body { font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 14px; line-height: 1.6; color: rgba(51, 51, 51, 1); background-color: rgba(255, 255, 255, 1); padding: 20px; max-width: 960px; margin: 0 auto }
body>*:first-child { margin-top: 0 !important }
body>*:last-child { margin-bottom: 0 !important }
p, blockquote, ul, ol, dl, table, pre { margin: 15px 0 }
h1, h2, h3, h4, h5, h6 { margin: 20px 0 10px; padding: 0; font-weight: bold; -webkit-font-smoothing: antialiased }
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code { font-size: inherit }
h1 { font-size: 28px; color: rgba(0, 0, 0, 1) }
h2 { font-size: 24px; border-bottom: 1px solid rgba(204, 204, 204, 1); color: rgba(0, 0, 0, 1) }
h3 { font-size: 18px }
h4 { font-size: 16px }
h5 { font-size: 14px }
h6 { color: rgba(119, 119, 119, 1); font-size: 14px }
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child { margin-top: 0; padding-top: 0 }
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 { margin-top: 0; padding-top: 0 }
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p { margin-top: 10px }
a { color: rgba(65, 131, 196, 1); text-decoration: none }
a:hover { text-decoration: underline }
ul, ol { padding-left: 30px }
ul li>:first-child, ol li>:first-child, ul li ul:first-of-type, ol li ol:first-of-type, ul li ol:first-of-type, ol li ul:first-of-type { margin-top: 0 }
ul ul, ul ol, ol ol, ol ul { margin-bottom: 0 }
dl { padding: 0 }
dl dt { font-size: 14px; font-weight: bold; font-style: italic; padding: 0; margin: 15px 0 5px }
dl dt:first-child { padding: 0 }
dl dt>:first-child { margin-top: 0 }
dl dt>:last-child { margin-bottom: 0 }
dl dd { margin: 0 0 15px; padding: 0 15px }
dl dd>:first-child { margin-top: 0 }
dl dd>:last-child { margin-bottom: 0 }
pre, code, tt { font-size: 12px; font-family: Consolas, “Liberation Mono”, Courier, monospace }
code, tt { margin: 0; padding: 0; white-space: nowrap; border: 1px solid rgba(234, 234, 234, 1); background-color: rgba(248, 248, 248, 1); border-radius: 3px }
pre>code { margin: 0; padding: 0; white-space: pre; border: none; background: rgba(0, 0, 0, 0) }
pre { background-color: rgba(248, 248, 248, 1); border: 1px solid rgba(204, 204, 204, 1); font-size: 13px; line-height: 19px; overflow: auto; padding: 6px 10px; border-radius: 3px }
pre code, pre tt { background-color: rgba(0, 0, 0, 0); border: none }
kbd { -moz-border-bottom-colors: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: rgba(221, 221, 221, 1); background-image: linear-gradient(rgba(241, 241, 241, 1), rgba(221, 221, 221, 1)); background-repeat: repeat-x; border-top: 1px solid rgba(221, 221, 221, 1); border-right: 1px solid rgba(204, 204, 204, 1); border-bottom: 1px solid rgba(204, 204, 204, 1); border-left: 1px solid rgba(221, 221, 221, 1); border-image: none; border-radius: 2px; font-family: “Helvetica Neue”, Helvetica, Arial, sans-serif; line-height: 10px; padding: 1px 4px }
blockquote { border-left: 4px solid rgba(221, 221, 221, 1); padding: 0 15px; color: rgba(119, 119, 119, 1) }
blockquote>:first-child { margin-top: 0 }
blockquote>:last-child { margin-bottom: 0 }
hr { clear: both; margin: 15px 0; height: 0; overflow: hidden; border-top: none; border-right: none; border-bottom: 4px solid rgba(221, 221, 221, 1); border-left: none; background: rgba(0, 0, 0, 0); padding: 0 }
table th { font-weight: bold }
table th, table td { border: 1px solid rgba(204, 204, 204, 1); padding: 6px 13px }
table tr { border-top: 1px solid rgba(204, 204, 204, 1); background-color: rgba(255, 255, 255, 1) }
table tr:nth-child(2n) { background-color: rgba(248, 248, 248, 1) }
img { max-width: 100% }

Java集合类详解

概述

集合类就是一个动态对象数组
与之相关的接口包括

  • Collection

    • List

      • ArrayList
      • Vector
      • LinkedList
    • Set

      • HashSet
        • LinkedHashSet (按照插入顺序存放的,不重复的)
      • SortedSet

        • TreeSet
    • Queue

      • LinkedList
  • Map

    • hashMap
    • weakhashMap
    • SortedMap

      • TreeMap
  • Map.Entry
  • Iterator

    • ListIterator
  • Enumeration 最早的输出的接口

Collection接口

       @Override
        public boolean contains(Object o) {
            return false;
        }

        @Override
        public Iterator<Integer> iterator() {
            return null;
        }

        @Override
        public Object[] toArray() {
            return new Object[0];
        }

        @Override
        public <T> T[] toArray(T[] a) {
            return null;
        }


        @Override
        public boolean containsAll(Collection<?> c) {
            return false;
        }

        @Override
        public boolean addAll(Collection<? extends Integer> c) {
            return false;
        }

        @Override
        public boolean removeAll(Collection<?> c) {
            return false;
        }

        @Override
        public boolean retainAll(Collection<?> c) {
            return false;
        }

        @Override
        public void clear() {
        }

注意 al.retainAll(al1); 相当于求交集然后放在al中 Integer []array1=al.toArray(new Integer[]{});
Object []array2=al.toArray();
两个获得Array的方法,第一个是通过泛型指定了数组的类型,第二个返回的是Object[]数组,使用的时候需要转型

List接口

可以重复 ,可以认为是一个数组,可以通过索引取出数据
List对于Collection有很大的扩充
新增包括

  • 指定位置增加数据
  • 指定位置增加一组数据
  • 取得指定位置数据
  • 在指定位置删除数据
  • subList(from,to)返回子List

    • al1=al.subList(0,1);

数据的顺序与插入顺序一样

LinkedList类

同时实现了List和Queue接口,就是一个链表的封装

   LinkedList<String> ll=new LinkedList<String>();
    ll.add("eq");
    ll.add("eq1");
    ll.add("eq2");
    ll.add("eq3");
    ll.addFirst("23");
    ll.addLast("233");
    System.out.println(ll);

Queue接口

element找到头 offer在队列尾加元素
peek找到头
poll找到头并删除
remove删除头

Set

其中的方法与Collection的完全一样

  • 顺序与插入的顺序无关
  • 不重复的,如果重复插入会忽略
TreeSet
  • 数据是排序的!!,实际上也是SortedSet的一个子类
HashSet
  • 数据散列存放
TreeSet自动排序原理

要排序必须可比较,因此TreeSet指定的类必须实现了Comparable方法

判断重复元素的原理

使用equals和hashCode方法来判断
首先判断hashCoede是否一样,不相同则为不同元素,相同则调用equals()来最终决定,因此将元素加入Set的的时候最好复写这两个方法

  • 对象相等->hashcode一定相等
  • hashCode相等->对象不一定相等

SortedSet接口

是自动排序的
方法

  • 返回首尾元素
  • 返回任意一段子SortedSet headSet/tailSet/subSet

Iterator接口

Collection的输出方式

  • Iterator
  • ListIterator
  • foreach
  • Enumeration

集合的输出最好一定是用Iterator接口
Iterator是迭代输出接口
通过Collection.iterator()实例化
hasnext()判断是否有内容,并且移动指针
在使用迭代输出的时候,不要使用集合类的remove方法,而是要用iterator的remove方法,尽量不要再输出时删除
单向操作

ListIterator接口

继承自Iterator
可以双向的输出
只有List接口才有实例化该接口的方法
只能现正向输出,后逆向输出,不能倒过来 

   List ls=new ArrayList(); 
ls.add("hello");
ls.add("world");
ls.add("!!!");
ListIterator ll=ls.listIterator(); while (ll.hasNext()){ System.out.print(ll.next()); } while (ll.hasPrevious()){ System.out.print(ll.previous()); }

foreach

可以用于输出集合或者数组中的内容,定义为:

for(type element: array){
     System.out.println(element);
  }

举例如下

    int s[]={0,1,2,3,4,5};
    for(int w:s){
        System.out.println(w);
    }
    List<Integer> l=new ArrayList<Integer>();
    l.add(2);
    l.add(3);
    l.add(4);
    for(int w:l){
        System.out.println(w);
    }

    int ss[][]={{1,2,3},{4,5,6}};
    //这里展示一下不规则数组
    int sss[][]=new int[2][];
    sss[0]=new int[3];
    sss[1]=new int[5];
    for(int x[]:ss){
        for(int y:x){
            System.out.println(y);
        }
    }

Map接口

Map.Entry接口

是Map的内部接口
指的是一个key->value的对象

实际上Map保存的是Map.Entry的对象,其实也相当于一组对象的集合

常见方法

  • containsKey
  • containsValue
  • get
  • KeySet 返回Key的Set集合
  • Values 返回Value的Collection集合

常用的子类

  • HashMap
  • HashTable
    比较老的类,与HashMap相比类似于ArraList与Vector的关系
  • TressMap
    按照Key的排序类
  • WeakHashMap
WeakHashMap

当Map的某些内容长期不使用的时候,会自动删除掉
在垃圾回收的时候,会把没有被引用的数据删掉

Map输出,可以获得KeySet然后打印Value,或者使用Map.Entry来迭代输出

    Map<String,String> m=new HashMap<String,String>();
    m.put("1","11"); m.put("2","22");m.put("3","33");

    Set<Map.Entry<String,String>> ms=m.entrySet();
    //迭代器输出
    Iterator<Map.Entry<String,String>> msi=ms.iterator();
    while (msi.hasNext()){
        Map.Entry<String,String> me=msi.next();
        System.out.println(me.getKey()+" "+me.getValue());
    }
    //foreach输出
    for(Map.Entry<String,String> me:ms){
        System.out.println(me.getKey()+" "+me.getValue());
    }

作为Key,实际上是一个Set了,不能重复,是通过equals和hashCode来判断的,所以使用非系统类作为键值的话,应该复写这两个方法

IdentityHashMap

允许key是重复的,比较键(和值)时使用引用相等性代替对象相等性

我们说的Map的键值不能重复,就是说对象equals的时候不能想等。而InentityHashMap中必须使用同一个引用才能认为是相等的

    Map<String,String> mm=new IdentityHashMap<String,String >();
    mm.put(new String("123"),"1");
    mm.put(new String("123"),"2");
    Set<Map.Entry<String,String>> ms1=mm.entrySet();
    for(Map.Entry<String,String> me:ms1){
        System.out.println(me.getKey()+" "+me.getValue());
    }
//这段代码可以打印出结果,但是,由于“123”的引用都丢失了,实际上你通过get(i)是取不出来任何数据的

SoretdMap/TreeMap

按照key进行排序,可以获得第一个/最后一个key,获得部分的Map的集合

    SortedMap<String,String> sm=new TreeMap<String,String>();
    sm.put("A","AA");
    sm.put("C","CC");
    sm.put("B","BB");
    System.out.println(sm.lastKey());
    System.out.println(sm.firstKey());
    sm.subMap("B","C");

对象引用强度说明

  • 强引用
    • 即使jvm内存不足,也不回收这个对象
  • 软引用
    • 内存不足时,才回收
  • 弱引用
    • 不管内存咋样,只要能回收了就回收了
  • 虚引用
    • 跟没有引用一样

Collections 集合操作类

主要的是对于一些常见的集合的操作,都是一些静态的处理方法,可以用到,如下

    java.util.Collections.addAll(l, "d", "qwe", "asd");
    java.util.Collections.reverse(l);
    java.util.Collections.replaceAll(l,"d","ddd");
    java.util.Collections.binarySearch(l,"qwe");
    java.util.Collections.sort(l);
    java.util.Collections.swap(l,2,3);

Stack类

是Vector的子类

    Stack<String> stack=new Stack<String>();
    stack.push("1");

    stack.add(3,"10"); //栈中间加入
    stack.peek();//得到栈顶不弹出
    stack.search("2");//搜索

Properities 属性类

是hashtable的子类

    Properties pro=new Properties();
    pro.setProperty("1","11");
    pro.setProperty("2","22");
    pro.setProperty("3","33");

    pro.storeToXML(new FileOutputStream("D:/a.xml"),"MYXML");

    Properties pro1=new Properties();

    pro1.loadFromXML(new FileInputStream("D:/a.xml"));
    pro1.list(System.out);
    System.out.println("1"+"  "+pro1.getProperty("1"));

可以保存到xml或者普通文件。实际上就是一个Map,可以使用Map的全部操作。通过list可以列出全部的属性。
store和load方法来与文件进行交互!!!!

 

 

 

 

 

 

 

 

版权声明:本文为Coder-Pig原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Coder-Pig/p/6513338.html