Java容器类小结

付费节点推荐


免费节点


节点使用教程


Java容器类

Java容器类类库的用途是“保存对象”,并将其划分为两个不同概念:

  • Collection:一个独立元素的序列,比如:List必须按照插入的顺序保存元素,而Set不能有重复元素。
  • Map:一组成对的“键值对”对象,允许使用键查找值。

List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。 和下面要提到的Set不同,List允许有相同的元素。 除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个 ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加,删除,设定元素, 还能向前或向后遍历。

请注意,Map没有继承Collection接口,Map提供key到value的映射。一个Map中不能包含相同的key,每个key只能映射一个 value。Map接口提供3种集合的视图,Map的内容可以被当作一组key集合,一组value集合,或者一组key-value映射。

迭代器:是一个对象,他的工作是遍历并选择序列中的对象。java中的Iterator迭代器只能单向移动,功能如下:

  1. 使用方法iterator()要求容器返回一个Iterator。Iterator将准备好返回序列的第一个元素。
  2. 使用next()获得序列中的下一个元素。
  3. 使用hasNext()检查序列中是否还有元素。
  4. 使用remove()从迭代器删除元素。

List之 ArrayList

ArrayList实现了可变大小的数组。它允许所有元素,包括null。
package cn.bcoder.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class List_ArrayList {
public static void main(String[] args) {
List<String>arraylist=new ArrayList<String>();
arraylist.add("b");
arraylist.add("c");Iterator<String>iter=arraylist.iterator();
while(iter.hasNext())  //使用迭代器输出
{
System.out.println(iter.next());}
//使用foreach输出
for(String s:arraylist)
{
System.out.println(s);
}

System.out.println(arraylist);  //使用tostring输出

 }
}

List之LinkedList

LinkedList底层为链表,所以在插入删除时不需要移动元素,效率要比ArrayList高,但随机访问操作比ArrayList低。
package cn.bcoder.Collection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
public class List_LinkedList {

 public static void main(String[] args) {
Collection<Integer>linkedlist=new LinkedList<Integer>(Arrays.asList(1,2,3,4,5)); //Arrays.asList()方法接受一个用逗号分隔的列表或数组,并将其转换为一个List对象。
linkedlist.add(99); //添加一个Integer数据
Integer []c={5,4,3,2,1};  //添加一个Integer数组
Collections.addAll(linkedlist, c); //Collections.addAll()方法接受一个Collection对象,以及一个数组或一个用逗号分隔的列表。
System.out.println(linkedlist);
}
}

List之Stack

package cn.bcoder.Collection;

import java.util.Stack;

public class List_Stack {

public static void main(String[] args) {
Stack<String>stack=new Stack<String>();
for(String s:"我 爱 bcoder".split(" "))
{
stack.push(s);  //入栈
}
System.out.println(stack);
while(!stack.isEmpty())
{
stack.pop();  //出栈
}System.out.println(stack);

}

}

List之Queue

package cn.bcoder.Collection;

import java.util.LinkedList;
import java.util.Queue;

public class List_Queue {
 public static void main(String[] args) {
// TODO 自动生成的方法存根
Queue<Integer> queue=new LinkedList<Integer>();
for(int i=0;i<10;i++)
queue.offer(i); //将元素插入到队尾,或者返回false
while(queue.peek()!=null) //返回队头,为空时返回null,而element()返回队头,为空时抛出NoSuchElementException异常.
// System.out.println(queue.remove()); //移除队头,队列为空时抛出异常
System.out.println(queue.poll()); //移除队头,队列为空时返回null
}

}

List之PriorityQueue

package cn.bcoder.Collection;
import java.util.PriorityQueue;
import java.util.Random;
/**
* 优先级队列(自动排列队列,数字默认从小到大)
* @author wangzheng
*
*/

public class List_PriorityQueue {

 public static void main(String[] args) {
Random rand=new Random(45);
PriorityQueue<Integer> queue=new PriorityQueue<Integer>();
for(int i=0;i<10;i++)
queue.offer(rand.nextInt(20));
  while(queue.peek()!=null)
System.out.print(queue.poll()+" ");}

}

运行结果:
0 1 3 7 9 11 11 11 13 14

Set之HashSet

package cn.bcoder.Collection;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Set_HashSet {
public static void main(String args[]) {
Random rand = new Random(47);
Set<Integer> intset = new HashSet<Integer>();
for (int i = 0; i < 10000; i++)
intset.add(rand.nextInt(30));
System.out.println(intset);

}
}

运行结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

Set之TreeSet

package cn.bcoder.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class Set_TreeSet {
public static void main(String args[]) {

Set<String> intset = new TreeSet<String>();
Collections.addAll(intset,"1,2,3,4,5,6".split(","));
System.out.println(intset.contains("1")); //判断字符“1”是否在此TreeSet中
intset.remove("1");
System.out.println(intset.contains("1"));
System.out.println(intset);

}
}

运行结果:
true
false
[2, 3, 4, 5, 6]

Map之HashMap

package cn.bcoder.Collection;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Map_HashMap {

public static void main(String[] args) {
Map<Integer,Integer>map=new HashMap<Integer,Integer>();
Random rand=new Random(47);
for(int i=0;i<10000;i++)
{
int key=rand.nextInt(20); //key保存随机数
Integer value=map.get(key); //value保存值
map.put(key, value==null?1:value+1); //如果键对应的值为NULL,则值为1,否则值+1
//map.put将值与映射的键关联
}
System.out.println(map.size());
map.put(20, 0); //获取map中键值对数

System.out.println(map); //输出map
System.out.println(map.containsKey(20)); //查找键为“20”是否存在
System.out.println(map.containsValue(0)); //查找值为“0”是否存在
map.remove(20);
System.out.println(map.containsKey(20)); //查找键为“20”是否存在
System.out.println(map.containsValue(0)); //查找值为“0”是否存在

}

}

运行结果:

20
{0=481, 1=502, 2=489, 3=508, 4=481, 5=503, 6=519, 7=471, 8=468, 9=549, 10=513, 11=531, 12=521, 13=506, 14=477, 15=497, 16=533, 17=509, 18=478, 19=464, 20=0}
true
true
false
false

未经允许不得转载:Bcoder资源网 » Java容器类小结

相关推荐

更多优质资源关注微信公众号: bcoder

bcoder
赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册