加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

集合

发布时间:2020-12-14 06:25:31 所属栏目:Java 来源:网络整理
导读:一、为什么要使用集合 ? 当定义一个数组来存数据的时候长度可能不够用,此时集合类是最好的考虑 二、集合间的区别 1. ?ArrayList与LinkedList的区别 ? ? ?1.1 ArrayList的内部实现是数组,每当删除或增加一个元素的时候整个数组就会移动;优势是在进行查询的

一、为什么要使用集合

? 当定义一个数组来存数据的时候长度可能不够用,此时集合类是最好的考虑

二、集合间的区别

1. ?ArrayList与LinkedList的区别

? ? ?1.1 ArrayList的内部实现是数组,每当删除或增加一个元素的时候整个数组就会移动;优势是在进行查询的时候比较快速,因为只要找到ArrayList元素的下标即可进行操作,所以在对数组进行大量查询操作的时候,用ArrayList

ArrayList a = new ArrayList();// 动态数组 a.add(1); a.add(2); a.add(3); a.add(4); List b = a.subList(2,4); for (Integer temp : b) { out.println(temp); } out.println(a.indexOf(2)); // 通过indexof充分体现了这个内部的实现原理就是数组 out.println(a.indexOf(21));

? ? ?1.2 LinkedList的内部实现是链表,每当删除或增加一个元素的时候只需对链表的节点进行操作就行,不用移动整个数组;优势是在进行修改和删除的时候比较快速,因为只要找到对应的链表节点即可进行删除,在进行大量的修改和删除操作的时候使用LinkedList

LinkedList a = new LinkedList();// 链表 a.add(1); a.add(2); a.add(3); a.add(4); for (Integer temp : a) { out.println(temp); }

2.?ArrayList与Vector的区别

? ?2.1 ArrayList是非线程安全的,采用异步处理模式,性能更高? ?2.2 Vector是线程安全的,采用同步处理模式,性能较低? ?2.3 两个集合类的方法在使用上完全一样

3.?HashSet、linkedHashSet、TreeSet的区别

? ?3.1 HashSet的实现原理是哈希表,内部元素是无序不可重复的,插入的元素在输出的时候也是无序的

aaa(</span><span style="color: #0000ff"&gt;int</span><span style="color: #000000"&gt; i) { </span><span style="color: #0000ff"&gt;this</span>.i =<span style="color: #000000"&gt; i; } </span><span style="color: #0000ff"&gt;int</span> i = 1<span style="color: #000000"&gt;;

}

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
<span style="color: #008000">//<span style="color: #008000"> hashset不能保证插入数据的顺序,特别是保证该代码一直不变,
<span style="color: #008000">////<span style="color: #008000"> 实现原理是哈希表
<span style="color: #008000">//<span style="color: #008000"> 如果要保证插入顺序即为输出顺序的话使用LinkedHashSet
HashSet h1 = <span style="color: #0000ff">new HashSet<span style="color: #000000">();
h1.add(<span style="color: #0000ff">new aaa(2));<span style="color: #008000">//<span style="color: #008000"> 分配堆内存以后把地址给h1
h1.add(<span style="color: #0000ff">new aaa(5<span style="color: #000000">));
h1.add(<span style="color: #0000ff">new aaa(2));<span style="color: #008000">//<span style="color: #008000"> 虽然set不能有重复的但这两个值其实是不一样的因为new的地址不一样
h1.add(<span style="color: #0000ff">new aaa(4<span style="color: #000000">));
h1.add(<span style="color: #0000ff">new aaa(6<span style="color: #000000">));
h1.add(<span style="color: #0000ff">new aaa(0<span style="color: #000000">));

    </span><span style="color: #0000ff"&gt;for</span><span style="color: #000000"&gt; (aaa j : h1) {
        out.println(j.i);</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 遍历输出h1地址指向的值</span>

<span style="color: #000000"> }

    h1.add(</span><span style="color: #0000ff"&gt;null</span>);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 此类允许使用 null 元素,但不能空输出</span>

<span style="color: #000000">
}
}

输出结果:2,6,2,5,4

? ?3.2、LinkedHashSet的实现原理是哈希表和链表,可以对插入的元素进行原样输出

<span style="color: #0000ff">import <span style="color: #0000ff">static<span style="color: #000000"> java.lang.System.out;

<span style="color: #0000ff">import<span style="color: #000000"> java.util.LinkedHashSet;

<span style="color: #0000ff">class<span style="color: #000000"> aaa {

aaa(</span><span style="color: #0000ff"&gt;int</span><span style="color: #000000"&gt; i) {
    </span><span style="color: #0000ff"&gt;this</span>.i =<span style="color: #000000"&gt; i;
}

</span><span style="color: #0000ff"&gt;int</span> i = 1<span style="color: #000000"&gt;;

}

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
LinkedHashSet h1 = <span style="color: #0000ff">new LinkedHashSet<span style="color: #000000">();
<span style="color: #008000">//<span style="color: #008000"> 具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现
h1.add(<span style="color: #0000ff">new aaa(2<span style="color: #000000">));
h1.add(<span style="color: #0000ff">new aaa(2));<span style="color: #008000">//<span style="color: #008000"> 这里两个 new aaa()其实是不一样的
h1.add(<span style="color: #0000ff">new aaa(1<span style="color: #000000">));
h1.add(<span style="color: #0000ff">new aaa(4<span style="color: #000000">));
h1.add(<span style="color: #0000ff">new aaa(3<span style="color: #000000">));
<span style="color: #0000ff">for<span style="color: #000000"> (aaa temp : h1) {
out.println(temp.i);
}
h1.add(<span style="color: #0000ff">null);<span style="color: #008000">//<span style="color: #008000"> 此类允许使用 null 元素
<span style="color: #000000">
}
}

输出结果:2,1,4,3

? ?3.3、TreeSet可以对插入的元素进行排序后再输出,会把一样的值去掉一个

TreeSet h1 = TreeSet h1.add(23844 (

输出结果:2,3,8

4.?HashMap和Hashtable的区别

? ?4.1、HashMap是非线程安全的,采用异步处理模式,性能更高? ?4.2、Hashtable是线程安全的,采用同步处理模式,性能较低? ?4.3、两个集合类的方法再使用上完全一样

5.?HashMap和IdentityHashMap的区别

?5.1、HashMap的内部是用equals来比较的,比较的是内容,当键值内容相等时,不允许重复的键值?5.2、IdentityHashMap内部是用==来比较的,比较的是地址,当地址不相同但内容相同时是允许重复的

<span style="color: #0000ff">import <span style="color: #0000ff">static<span style="color: #000000"> java.lang.System.out;

<span style="color: #0000ff">import<span style="color: #000000"> java.util.HashMap;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.IdentityHashMap;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Iterator;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Map.Entry;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Set;

<span style="color: #0000ff">class aa <span style="color: #0000ff">implements Comparable<span style="color: #000000"> {
<span style="color: #0000ff">int i = 1<span style="color: #000000">;

aa(</span><span style="color: #0000ff"&gt;int</span><span style="color: #000000"&gt; i) {
    </span><span style="color: #0000ff"&gt;this</span>.i =<span style="color: #000000"&gt; i;
}

@Override
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;int</span> compareTo(aa o) { <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 这函数就是排序的依据
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; TODO Auto-generated method stub</span>
    <span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;this</span>.i -<span style="color: #000000"&gt; o.i;
}

}

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
HashMap<Integer,String> k = <span style="color: #0000ff">new HashMap<Integer,String><span style="color: #000000">();
<span style="color: #008000">//<span style="color: #008000"> TODO Auto-generated method stub
HashMap<String,String> k1 = <span style="color: #0000ff">new HashMap<String,String>();<span style="color: #008000">//<span style="color: #008000"> 用equals比较 比较的是内容
String s1 = <span style="color: #0000ff">new String("1"<span style="color: #000000">);
String s2 = <span style="color: #0000ff">new String("2"<span style="color: #000000">);
String s3 = <span style="color: #0000ff">new String("2"<span style="color: #000000">);
String s4 = <span style="color: #0000ff">new String("3"<span style="color: #000000">);
k1.put(s1,"tom"<span style="color: #000000">);
k1.put(s2,"jack"<span style="color: #000000">);
k1.put(s3,"jack1"<span style="color: #000000">);
k1.put(s4,"mary"<span style="color: #000000">);
<span style="color: #0000ff">for (Entry<String,String><span style="color: #000000"> temp : k1.entrySet()) {
out.println(temp.getKey() + "," +<span style="color: #000000"> temp.getValue());
}
out.println(k1.size());

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 用==比较,比较的是地址
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 此类不是 通用 Map 实现!此类实现 Map 接口时,它有意违反 Map 的常规协定,
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 该协定在比较对象时强制使用 equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。</span>
    out.println("---------------identityHashMap-----------------"<span style="color: #000000"&gt;);
    IdentityHashMap</span><String,String> k2 = <span style="color: #0000ff"&gt;new</span> IdentityHashMap<String,String>();<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 用==比较,比较的是地址</span>
    k2.put(s1,"tom"<span style="color: #000000"&gt;);
    k2.put(s2,</span>"jack"<span style="color: #000000"&gt;);
    k2.put(s3,</span>"jack1"<span style="color: #000000"&gt;);
    k2.put(s4,String><span style="color: #000000"&gt; temp : k2.entrySet()) {
        out.println(temp.getKey() </span>+ "," +<span style="color: #000000"&gt; temp.getValue());
    }
    out.println(k1.size());
}

}

输出结果:

1,tom2,jack13,mary3---------------identityHashMap-----------------1,mary2,jack3

6.?TreeSet和TreeMap的区别

?6.1、TreeSet与TreeMap实现原理都是红黑树,都能够对内部元素进行排序后输出?6.2、都要实现Comparable泛型接口,否则不能比较

<span style="color: #0000ff">import <span style="color: #0000ff">static<span style="color: #000000"> java.lang.System.out;

<span style="color: #0000ff">import<span style="color: #000000"> java.util.Map.Entry;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.TreeMap;

<span style="color: #0000ff">class aaa <span style="color: #0000ff">implements Comparable<span style="color: #000000"> {
aaa(<span style="color: #0000ff">int<span style="color: #000000"> i) {
<span style="color: #0000ff">this.i =<span style="color: #000000"> i;
}

</span><span style="color: #0000ff"&gt;int</span> i = 1<span style="color: #000000"&gt;;

@Override
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;int</span><span style="color: #000000"&gt; compareTo(aaa o) {
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; TODO Auto-generated method stub</span>
    <span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;this</span>.i -<span style="color: #000000"&gt; o.i;
}

}

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
TreeMap<aaa,String> h1 = <span style="color: #0000ff">new TreeMap<aaa,String><span style="color: #000000">();
<span style="color: #008000">//<span style="color: #008000"> 可以排序的set
h1.put(<span style="color: #0000ff">new aaa(2),"jack"<span style="color: #000000">);
h1.put(<span style="color: #0000ff">new aaa(1),"tom"<span style="color: #000000">);
h1.put(<span style="color: #0000ff">new aaa(3),"mary"<span style="color: #000000">);
<span style="color: #0000ff">for (Entry<aaa,String><span style="color: #000000"> temp : h1.entrySet()) {
out.println(temp.getKey().i + "," +<span style="color: #000000"> temp.getValue());
}

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 因为Integer是实现了Comparable接口的,所以做为key的比较</span>
    TreeMap<Integer,String> h2 = <span style="color: #0000ff"&gt;new</span> TreeMap<Integer,String><span style="color: #000000"&gt;();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 可以排序的set</span>
    h2.put(2,"jack"<span style="color: #000000"&gt;);
    h2.put(</span>1,"tom"<span style="color: #000000"&gt;);
    h2.put(</span>3,"mary"<span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;for</span> (Entry<Integer,String><span style="color: #000000"&gt; temp : h2.entrySet()) {
        out.println(temp.getKey() </span>+ "," +<span style="color: #000000"&gt; temp.getValue());
    }
}

}

三、集合的方法的使用

1.?HashMap和Hashtable各种方法的使用(HashMap与Hashtable的使用完全一样)

<span style="color: #0000ff">import <span style="color: #0000ff">static<span style="color: #000000"> java.lang.System.out;

<span style="color: #0000ff">import<span style="color: #000000"> java.util.HashMap;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Iterator;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Map.Entry;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Set;

<span style="color: #0000ff">class aa <span style="color: #0000ff">implements Comparable<span style="color: #000000"> {
<span style="color: #0000ff">int i = 1<span style="color: #000000">;

aa(</span><span style="color: #0000ff"&gt;int</span><span style="color: #000000"&gt; i) {
    </span><span style="color: #0000ff"&gt;this</span>.i =<span style="color: #000000"&gt; i;
}

@Override
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;int</span> compareTo(aa o) { <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 这函数就是排序的依据
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; TODO Auto-generated method stub</span>
    <span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;this</span>.i -<span style="color: #000000"&gt; o.i;
}

}

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
HashMap<Integer,String><span style="color: #000000">();
<span style="color: #008000">//<span style="color: #008000"> 增加
k.put(1,"jack"<span style="color: #000000">);
k.put(2,"rose"<span style="color: #000000">);
k.put(3,"alan"<span style="color: #000000">);
k.put(4,"jim"<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000"> out.println(k.size());
<span style="color: #008000">//<span style="color: #008000"> 删除
k.remove(1<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000"> out.println(k.size());
<span style="color: #008000">//<span style="color: #008000"> 改 先删除再修改
<span style="color: #008000">//<span style="color: #008000"> 查
<span style="color: #008000">//<span style="color: #008000"> out.println(k.get(2));
<span style="color: #008000">//<span style="color: #008000"> bollean型
<span style="color: #008000">//<span style="color: #008000"> out.println(k.containsKey(2));<span style="color: #008000">//<span style="color: #008000">s输出为true
<span style="color: #008000">//<span style="color: #008000"> out.println(k.containsValue("rose"));

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; HashMap<Integer,String> k1=new HashMap<Integer,String>();
    </span><span style="color: #008000"&gt;//</span> <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;增加元素
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; k1.put(11,"lgs");
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; k1.put(22,"yc");

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 把一个HashMap添加到另一个HashMap里面去
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; k.putAll(k1);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(k.size());

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; k.clear();清除k中的所有元素
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(k.isEmpty());

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Set<Integer> s1=k.keySet();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; for(Integer s:s1){
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(s);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; }
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Collection<String> c=k.values();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; for(String s:c){
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(s);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; }
    </span><span style="color: #008000"&gt;//</span> <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;输出HashMap的内容
    </span><span style="color: #008000"&gt;//</span> <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;使用entrySet()方法将键值封装成set类型的</span>
    Set<Entry<Integer,String>> k1 =<span style="color: #000000"&gt; k.entrySet();
    </span><span style="color: #008000"&gt;//</span> <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;遍历输出set的内容</span>
    <span style="color: #0000ff"&gt;for</span> (Entry<Integer,String><span style="color: #000000"&gt; k2 : k1) {
        out.println(k2.getKey() </span>+ "," +<span style="color: #000000"&gt; k2.getValue());
    }

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 拆解set里面的内容
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 迭代输出set里面的内容</span>
    out.println("-------迭代输出set里面的内容-------"<span style="color: #000000"&gt;);
    Iterator</span><Entry<Integer,String>> i1 =<span style="color: #000000"&gt; k1.iterator();
    </span><span style="color: #0000ff"&gt;while</span><span style="color: #000000"&gt; (i1.hasNext()) {
        Entry</span><Integer,String> e =<span style="color: #000000"&gt; i1.next();
        out.println(e.getKey() </span>+ "," +<span style="color: #000000"&gt; e.getValue());
    }
}

}

2.?PriorityQueue优先队列各种方法的使用

<span style="color: #0000ff">import <span style="color: #0000ff">static<span style="color: #000000"> java.lang.System.out;

<span style="color: #0000ff">import<span style="color: #000000"> java.util.PriorityQueue;

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
<span style="color: #008000">//<span style="color: #008000"> 队列:先进先出
<span style="color: #008000">//<span style="color: #008000"> 优先队列
PriorityQueue p = <span style="color: #0000ff">new PriorityQueue<span style="color: #000000">();
p.add(1);<span style="color: #008000">//<span style="color: #008000"> 把元素添加到队列的尾部,如果空间不够自动开辟空间
p.add(2<span style="color: #000000">);
p.offer(3);<span style="color: #008000">//<span style="color: #008000"> 把元素添加到队列的尾部,如果空间不够不会自动开辟空间,适用于空间被指定的队列集合
<span style="color: #008000">//<span style="color: #008000"> out.println(p.size());
<span style="color: #008000">//<span style="color: #008000"> p.remove();<span style="color: #008000">//<span style="color: #008000">获取队列头部的元素并删除
<span style="color: #008000">//<span style="color: #008000"> for(Integer p1:p){
<span style="color: #008000">//<span style="color: #008000"> out.println(p1);
<span style="color: #008000">//<span style="color: #008000"> }
out.println(p.peek());<span style="color: #008000">//<span style="color: #008000"> peek()方法看一下头部元素而已 不删除 队列为空时返回null
<span style="color: #000000"> out.println(p.size());
out.println(p.element());<span style="color: #008000">//<span style="color: #008000"> element()方法获取队列的头部元素 不删除
<span style="color: #000000"> out.println(p.size());
out.println(p.poll());<span style="color: #008000">//<span style="color: #008000"> poll()方法获取队列的头部元素 并删除 队列为空时返回null
<span style="color: #000000"> out.println(p.size());

    </span><span style="color: #0000ff"&gt;for</span><span style="color: #000000"&gt; (Integer p1 : p) {
        out.println(p1);
    }
}

}

3.?双向链表LinkedList中各种方法的使用

<span style="color: #0000ff">import <span style="color: #0000ff">static<span style="color: #000000"> java.lang.System.out;

<span style="color: #0000ff">import<span style="color: #000000"> java.util.LinkedList;

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
<span style="color: #008000">//<span style="color: #008000"> 双向队列
LinkedList l = <span style="color: #0000ff">new LinkedList<span style="color: #000000">();
<span style="color: #008000">//<span style="color: #008000"> l.add(9);<span style="color: #008000">//<span style="color: #008000">将指定元素插入双向队列的尾部
<span style="color: #008000">//<span style="color: #008000"> l.push(18);<span style="color: #008000">//<span style="color: #008000">push()方法将一个元素放入双向队列的头部
l.addFirst(1);<span style="color: #008000">//<span style="color: #008000"> 将指定元素插入双向队列的开头 与offerFist()方法类似
l.addFirst(2<span style="color: #000000">);
l.addFirst(3<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000"> l.add(9);<span style="color: #008000">//<span style="color: #008000">将指定元素插入双向队列的尾部
<span style="color: #008000">//<span style="color: #008000"> l.add(19);<span style="color: #008000">//<span style="color: #008000">将指定元素插入双向队列的尾部
l.addLast(4);<span style="color: #008000">//<span style="color: #008000"> 将指定元素插入双向队列的结尾 offerLast()方法类似
l.addLast(1<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000"> for(Integer k:l){
<span style="color: #008000">//<span style="color: #008000"> out.println(k);
<span style="color: #008000">//
<span style="color: #008000">//<span style="color: #008000"> }
<span style="color: #008000">//<span style="color: #008000"> out.println(l.getFirst());<span style="color: #008000">//<span style="color: #008000">获取双向队列的第一个元素
<span style="color: #008000">//<span style="color: #008000"> out.println(l.getLast());<span style="color: #008000">//<span style="color: #008000">获取双向队列的最后一个元素
<span style="color: #008000">//<span style="color: #008000"> out.println(l.removeFirstOccurrence(1)); <span style="color: #008000">//<span style="color: #008000">删除第一个重复的
<span style="color: #008000">//<span style="color: #008000"> for(Integer k:l){
<span style="color: #008000">//<span style="color: #008000"> out.println(k);
<span style="color: #008000">//
<span style="color: #008000">//<span style="color: #008000"> }
<span style="color: #008000">//<span style="color: #008000"> out.println(l.removeLastOccurrence(1)); <span style="color: #008000">//<span style="color: #008000">删除最后一个重复的
<span style="color: #0000ff">for<span style="color: #000000"> (Integer k : l) {
out.println(k);

    }

    </span><span style="color: #008000"&gt;//</span>
    <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(pq.peekFirst());
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(pq.peekLast());
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(pq.pollFirst());
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(pq.pollLast());
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println("----------------");
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; for(Integer temp:pq)
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(temp);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(pq.pop());
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(pq.removeFirst());
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println("----------------");
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; for(Integer temp:pq)
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; out.println(temp);</span>

<span style="color: #000000"> }
}

4.?Properties的使用(自己创建读取Properties文件的过程)

<span style="color: #0000ff">import <span style="color: #0000ff">static<span style="color: #000000"> java.lang.System.out;

<span style="color: #0000ff">import<span style="color: #000000"> java.io.FileInputStream;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.FileNotFoundException;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.FileOutputStream;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.IOException;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Map.Entry;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Properties;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Set;

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;static</span> <span style="color: #0000ff"&gt;void</span> main(String[] args) <span style="color: #0000ff"&gt;throws</span><span style="color: #000000"&gt; FileNotFoundException,IOException {
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; TODO Auto-generated method stub
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 创建配置文件过程</span>
    Properties pOut = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; Properties();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 设置配置文件的属性</span>
    pOut.setProperty("username","pw1"<span style="color: #000000"&gt;);
    pOut.setProperty(</span>"username1","pw2"<span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 把文件存到硬盘上并命名</span>
    pOut.store(<span style="color: #0000ff"&gt;new</span> FileOutputStream("test.properties"),"ini file"<span style="color: #000000"&gt;);

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 从硬盘读取配置文件的过程</span>
    Properties pIn = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; Properties();
    pIn.load(</span><span style="color: #0000ff"&gt;new</span> FileInputStream("test.properties"<span style="color: #000000"&gt;));
    Set</span><Entry<Object,Object>> s1 =<span style="color: #000000"&gt; pIn.entrySet();
    </span><span style="color: #0000ff"&gt;for</span> (Entry<Object,Object><span style="color: #000000"&gt; s : s1) {
        out.println(s.getKey() </span>+ "," +<span style="color: #000000"&gt; s.getValue());
    }

}

}

输出结果:

username1,pw2username,pw1


作者:小不点啊出处:http://www.cnblogs.com/leeSmall/p/7631344.html版权归作者所有,转载请注明出处

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读