Map、Set、List、Queue、Stack的特点与用法
2017-11-08 11:06
363 查看
Map是以键值对的形式存储(即:key-value),其中key是唯一的、不可重复的,value的值可以重复,当插入的值是key相同,后加进去的会将已有的进行覆盖。
Map有几个具体的实现类,包括TreeMap和HashMap,其中TreeMap是有序的,HashMap是无序的。
在运用Map的过程中,Map集合的key和value是可以单独抽取出来使用的,可以根据key来取得value的值。
其中keySet()方法可以将Map中的所有key值取出存储在一个Set集合中,而values()方法可以将Map中的所有value值取出组成一个集合。
Set
Set集合不能够包含重复元素,set中最多包含一个null元素
遍历方式只能通过Iterator迭代器进行单项遍历
Set是不同步的,没有同步方法
List
List是一个有序的可重复的集合可以在任意的位置进行增删改查
遍历方式多样,可以用for循环和迭代器
Query
Query队列,遵循先进先出原则,不允许插入null值
其中提供了相应的进队和出队的方法,但是建议使用offer()来添加元素,使用poll来删除元素,因为这两个方法会返回一个值用于给用户判断是否执行成功。
LinkedList实现了Query接口
Stack
package map;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.management.Query;
/**
* 队列
* @author wjddn
*
*/
public class QueryDemo {
public static void main(String[] args) {
}
Stack遵从后进先出原则。
Stack继承自Vector。
它通过五个操作对类Vector进行扩展,允许将向量视为堆栈,它提供了通常的push和pop操作,以及取堆栈顶点的peek()方法、测试堆栈是否为空的empty方法等
用法
如果涉及堆栈,队列等操作,建议使用List
对于快速插入和删除元素的,建议使用LinkedList
如果需要快速随机访问元素的,建议使用ArrayList
Map有几个具体的实现类,包括TreeMap和HashMap,其中TreeMap是有序的,HashMap是无序的。
在运用Map的过程中,Map集合的key和value是可以单独抽取出来使用的,可以根据key来取得value的值。
其中keySet()方法可以将Map中的所有key值取出存储在一个Set集合中,而values()方法可以将Map中的所有value值取出组成一个集合。
package map; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Map集合 * @author wjddn * */ public class MapDemo { public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); for(int i = 0 ; i< 10 ; i++){ map.put(i, "hello"+i); } Set<Integer> set = map.keySet(); for (Integer integer : set) { System.out.print(integer);//0123456789 获取到了所有的key值 } Collection<String> values = map.values(); for (String string : values) { System.out.println(string); //hello0 -- hello9 } } }
Set
Set集合不能够包含重复元素,set中最多包含一个null元素
遍历方式只能通过Iterator迭代器进行单项遍历
Set是不同步的,没有同步方法
List
List是一个有序的可重复的集合可以在任意的位置进行增删改查
遍历方式多样,可以用for循环和迭代器
Query
Query队列,遵循先进先出原则,不允许插入null值
其中提供了相应的进队和出队的方法,但是建议使用offer()来添加元素,使用poll来删除元素,因为这两个方法会返回一个值用于给用户判断是否执行成功。
LinkedList实现了Query接口
package map; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Queue; import javax.management.Query; /** * 队列 * @author wjddn * */ public class QueryDemo { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<>(); //进队 queue.offer(1); queue.offer(2); queue.offer(3); queue.offer(4); for (Integer integer : queue) { System.out.println(integer); } //迭代遍历 Iterator<Integer> iterator = queue.iterator(); while(iterator.hasNext()){ Integer integer = iterator.next(); System.out.print(integer); } //出队 System.out.println(queue.poll());//成功返回1 } }
Stack
package map;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.management.Query;
/**
* 队列
* @author wjddn
*
*/
public class QueryDemo {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>(); //进队 queue.offer(1); queue.offer(2); queue.offer(3); queue.offer(4); for (Integer integer : queue) { System.out.println(integer); } //迭代遍历 Iterator<Integer> iterator = queue.iterator(); while(iterator.hasNext()){ Integer integer = iterator.next(); System.out.print(integer); } //出队 System.out.println(queue.poll());//成功返回1 }
}
Stack遵从后进先出原则。
Stack继承自Vector。
它通过五个操作对类Vector进行扩展,允许将向量视为堆栈,它提供了通常的push和pop操作,以及取堆栈顶点的peek()方法、测试堆栈是否为空的empty方法等
package map; import java.util.Iterator; import java.util.Stack; import java.util.Vector; /** * Stack栈 * @author wjddn * */ public class StackDemo { public static void main(String[] args) { Stack<String> stack = new Stack<>(); //进栈 stack.add("hello"); stack.add("world"); stack.add("java"); //取栈顶 //先进后出 System.out.println("栈顶:"+stack.peek()); // 输出: 栈顶:java //进栈 System.out.println("进栈: "+stack.push("hello")); //遍历 for (String string : stack) { System.out.println(string); } System.out.println("**************************"); //迭代 Iterator<String> iterator = stack.iterator(); while(iterator.hasNext()){ String string = iterator.next(); System.out.println(string); } } }
用法
如果涉及堆栈,队列等操作,建议使用List
对于快速插入和删除元素的,建议使用LinkedList
如果需要快速随机访问元素的,建议使用ArrayList
相关文章推荐
- Map、Set、List、Queue、Stack的特点与用法1
- Map、Set、List、Queue、Stack的特点与用法2
- JAVA Map、Set、List、Queue、Stack的特点与用法
- Map、Set、List、Queue、Stack的特点与用法
- Map、Set、List、Queue、Stack的特点与用法
- Map、Set、List、Queue、Stack的特点与用法
- Map、Set、List、Queue、Stack的特点与用法。
- Map、Set、List、Queue、Stack的特点与用法1
- Map、Set、List、Collection、Queue、Stack的特点与用法。
- Map、Set、List、Queue、Stack的特点与用法
- java 集合(set、list、map、stack、queue)的用法
- STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map
- 16. Map、Set、List、Queue、Stack的特点与用法。
- STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL库整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)