学习java之HashMap和TreeMap
HashMap和TreeMap是Map接口的两种实现,ArrayDeque和LinkedList是Queue接口的两种实现方式。下面的代码是我今天学习这四个数据结构之后写的。还是不熟悉,TreeMap中的其余构造方法我还没有实现,真搞不懂同样的数据结构用java却写的这么麻烦。。
package regexp; import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap; public class test {
public static void main(String args[]) throws NoSuchElementException{
//Queue接口的实现
Queue<Integer> q = new LinkedList<Integer>(); //通过LinkedList实现Queue接口
// Queue<Integer> q = new ArrayDeque<Integer>(); //通过LinkedList实现Queue接口
for(int i=0;i<10;i++) {
q.add(i+1);
}
System.out.printf("%d%n", q.size());
q.element();
System.out.printf("%d%n", q.element());
if(q.isEmpty()) {
System.out.printf("%s%n", "The ArrayDeque is empty");
} else {
System.out.printf("%d%n", q.peek());
} System.out.printf("%d%n", q.size());
Integer first = q.remove(),last = q.remove();
System.out.printf("%d %d%n",first,last);
System.out.printf("%d%n", q.size()); //Map接口的实现
Map<Integer,String> mp = new HashMap<Integer,String>();
mp.put(1, "111");
mp.put(2, "222");
String tt = mp.put(3, "333");
System.out.println(tt); //null
String tmp = mp.put(1, "444");
System.out.printf("%s%n",tmp); //
System.out.printf("The size of the map is %d.%n", mp.size()); //
for(int i=0;i<mp.size();i++) {
System.out.printf("id=%d, value=%s%n",i+1,mp.get(i+1));
}
mp.remove(1); //delete key=1 Set<Integer> keys = mp.keySet();
//foreach
for(Integer num : keys) {
System.out.printf(" %d", num);
}
//iterator
Iterator<Integer> it1 = keys.iterator();
while(it1.hasNext()) {
System.out.printf(" %d", it1.next());
} Collection<String> coll = mp.values();
//foreach
for(String s : coll) {
System.out.printf(" %s", s);
}
//iterator
Iterator<String> it2 = coll.iterator();
while(it2.hasNext()) {
System.out.printf(" %s", it2.next());
} Set<Map.Entry<Integer, String>> ss = mp.entrySet();
//foreach
for(Map.Entry<Integer, String> pair : ss) {
Integer num = pair.getKey();
String value = pair.getValue(); System.out.printf(" %s '+' id=%d, value=%s", pair,num,value);
}
//iterator
Iterator<Map.Entry<Integer, String>> it3 = ss.iterator();
while(it3.hasNext()) {
System.out.printf(" %s", it3.next());
} //TreeMap
TreeMap<Integer,String> treemp = new TreeMap<Integer,String>(new myComparator());
treemp.put(111,"111");treemp.put(222, "222");treemp.put(112,"112");treemp.put(113, "113");
for(Map.Entry<Integer, String> pair : treemp.entrySet()) {
Integer num = pair.getKey();
String str = pair.getValue();
System.out.printf(" %d:%s ", num,str);
}
}
} class myComparator implements Comparator<Integer> {
public myComparator() {
}
public int compare(Integer a,Integer b) {
return a.compareTo(b);
} }
下面是运行出来的结果,友友们可以对照着看,有没有初学者呀,come here and leave a message please!
10
1
1
10
1 2
8
null
111
The size of the map is 3.
id=1, value=444
id=2, value=222
id=3, value=333
2 3 2 3 222 333 222 333 2=222 '+' id=2, value=222 3=333 '+' id=3, value=333 2=222 3=333 111:111 112:112 113:113 222:222
学习java之HashMap和TreeMap的更多相关文章
- Java中HashMap,LinkedHashMap,TreeMap的区别[转]
原文:http://blog.csdn.net/xiyuan1999/article/details/6198394 java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类 ...
- Java中HashMap和TreeMap的区别深入理解
首先介绍一下什么是Map.在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.这就是我们平时说的键值对. Has ...
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMap Map主要用于存储健值对,根 ...
- Java中HashMap和TreeMap的区别
什么是Map集合在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.这就是我们平时说的键值对. HashMap ...
- Java 集合系列14之 Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)
概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...
- 【Java】Map杂谈,hashcode()、equals()、HashMap、TreeMap、LinkedHashMap、ConcurrentHashMap
参考的优秀文章: <Java编程思想>第四版 <Effective Java>第二版 Map接口是映射表的结构,维护键对象与值对象的对应关系,称键值对. > hashco ...
- Java开发笔记(六十六)映射:HashMap和TreeMap
前面介绍了两种集合的用法,它们的共性为每个元素都是唯一的,区别在于一个无序一个有序.虽说往集合里面保存数据还算容易,但要从集合中取出数据就没那么方便了,因为集合居然不提供get方法,没有get方法怎么 ...
- Java 容器 & 泛型:五、HashMap 和 TreeMap的自白
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Java 容器的文章这次应该是最后一篇了:Java 容器 系列. 今天泥瓦匠聊下 Maps. 一.Ma ...
- 杨晓峰-Java核心技术-9 HashMap Hashtable TreeMap MD
目录 第9讲 | 对比Hashtable.HashMap.TreeMap有什么不同? 典型回答 考点分析 知识扩展 Map 整体结构 有序 Map HashMap 源码分析 容量.负载因子和树化 精选 ...
随机推荐
- (转)Linux进程间通信
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 谢谢nonoob纠错 我们在Linux信号基础中已经说明,信号可以看作一种粗糙的进 ...
- Eclipse 字体选择
Windows下推荐使用Consolas Linux下推荐使用DejaVu Sans Mono, Website: http://dejavu-fonts.org/wiki/Main_PageDown ...
- (转)Android之ListView原理学习与优化总结
转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...
- Linux多线程之同步3
需求 客户端将需要解决的task发送给服务器,服务器调用线程来解决客户端发送的task,解决完由线程负责将其发送回客户端.(用管道实现通信) 思路 1. server维护两个列表.一是客户端列表.二是 ...
- 标准管道(popen)
NAME popen, pclose - pipe stream to or from a process SYNOPSIS #include <stdio.h> FILE *popen( ...
- 记一段使用node对mysql数据库做处理
所用到的存储过程如下: temp_get_userCount: BEGIN #Routine body goes here... SELECT COUNT(id) as num FROM tbl_us ...
- redhat6修改主机名
1.临时修改主机名 sudo hostname lyhost 2.永久修改主机名 vim /etc/sysconfig/network 修改里面的hostname字段即可,重启后生效.
- ffmpeg转码MPEG2-TS的音视频同步机制分析
http://blog.chinaunix.net/uid-26000296-id-3483782.html 一.FFmpeg忽略了adaptation_field()数据 FFmpeg忽略了包含PC ...
- Ffmpeg解析media容器过程/ ffmpeg 源代码简单分析 : av_read_frame()
ffmpeg 源代码简单分析 : av_read_frame() http://blog.csdn.net/leixiaohua1020/article/details/12678577 ffmpeg ...
- Shell脚本基础II
1.shell算术运算 1)加法 r=`expr 4 + 5`(注意! '4' '+' '5' 这三者之间要有空白) r=$[ 4 + 5 ] r=$(( 4 + 5 )) echo $r 2)乘法 ...