/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) */
/* Author: Nicolas Hery - http://nicolashery.com */
/* Version: b13fe65ca28d2e568c6ed5d7f06581183df8f2ff */
/* Source: https://github.com/nicolahery/markdownpad-github */
/* RESET
=============================================================================*/
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
=============================================================================*/
body {
font-family: Helvetica, arial, freesans, clean, sans-serif;
font-size: 16px;
line-height: 1.6;
color: #333;
background-color: #fff;
padding: 20px;
max-width: 960px;
margin: 0 auto;
}
body>*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
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: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
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;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
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: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 16px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 14px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 15px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
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: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* TABLES
=============================================================================*/
table th {
font-weight: bold;
}
table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
Map
java.util.Map 查找表
package map;
import java.util.HashMap;
import java.util.Map;
/**
* java.util.Map 查找表
* Map 的结构是一个多行两列的表格,其中左列称为key,右列称为value
* Map总是以key-value对的形式宝尊数据,并且总是以key来获取
* 对应的value
* 所以我们经常讲要出查询的数据作为value,讲查询条件作为key
* 保存在Map中以便于根据条件快速查找对应的数据
*
* java.util.HashMap 散列表
* HashMap时Map最常用的实现类,也是当今最快的查询结构
* @author Cole
* 2018年12月13日上午9:09:21
*/
public class MapDemo1 {
public static void main(String[] args) {
/*
* Map要求指定两个泛型,分别说明key与value的类型
*/
Map<String,Integer> map = new HashMap<>();
/*
* V put(K k,V v)
* 将给定的键值对保存在Map中
* 注:
* Map有一个要求,即:Map中key不允许重复,重复的标准是
* 依靠key自身的equals比较结果
* 所以put方法是有返回值的,若本次存放的key已经在Map中
* 存在,则是替换value操作,那么返回值就是被替换的value,
* 否则为null
*/
/*
* 若value的类型为包装类,切记不要用对应的基本类型
* 接收put方法的返回值,因为这会触发包装类自动拆箱
* 而put方法返回值有可能为null,这时若拆箱会造成
* 空指针异常
*/
Integer num = map.put("语文", 88);
System.out.println(num);
map.put("数学", 90);
map.put("物理", 92);
map.put("化学", 90);
System.out.println(map);
num = map.put("语文", 77);
System.out.println(map);
System.out.println(num);
/*
* v get(Object key)
* 根据给定的key 获取对应的value,若给定的key在
* Map中不存在则返回值为null
*/
num = map.get("物理");
System.out.println("物理:"+num);
num = map.get("历史");
System.out.println("历史:"+num);
/*
* v size()
* 获取查找表长度
*/
int size = map.size();
System.out.println("size:"+size);
/*
* v remove(object key)
* 根据给定的key删除对应的这组键值对,返回值为
* 改组键值对中的value
*/
num = map.remove("化学");
System.out.println(num);
System.out.println(map);
/*
* boolean containsKey(Object k)
* boolean containsKey(Object v)
* 判断当前Map是否包含给定的key或value
* 包含的判断还是依靠元素自身equals比较的结果
*/
boolean ck = map.containsKey("语文");
boolean cv = map.containsValue(77);
System.out.println("是否包含key:"+ck);
System.out.println("是否包含value:"+cv);
}
}
Map的遍历
package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Map的遍历
* Map的遍历有三种方式:
* 遍历所有的key
* 遍历所有的key-value对
* 遍历所有的value(相对不常用)
*
* @author Cole
* 2018年12月13日上午10:21:47
*/
public class MapDemo2 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("语文", 90);
map.put("数学", 91);
map.put("物理", 92);
map.put("化学", 93);
map.put("英语", 94);
System.out.println(map);
/*
* 遍历所有的key
* Set keySet()
* 将当前Map中所有的key以一个Set集合的形式返回
* 遍历噶几和就等同于遍历了所有的 key
*/
Set<String> keySet = map.keySet();
for(String key:keySet) {
System.out.println(key);
}
/*
* 遍历魅族键值对
* Set entrySet()
* 将当前Map中每组键值对(若干的Entry实例)以一个
* Set集合的形式返回
*
* java.util.Map.Entry
* 每个Entry实例表示Map中的一组键值对
* 常用方法:
* K getKey():获取其表示的键值对中的key
* V getValue():获取其表示的键值对中的value
*/
Set<Entry<String,Integer>> entrySet = map.entrySet();
for(Entry<String,Integer> entry:entrySet) {
String str = entry.getKey();
Integer value = entry.getValue();
System.out.println(str+":"+value);
}
/*
* 遍历所有的value
* Collection values()
* 将当前Map中所有的value以一个集合形式返回
* 由于Map中不要求value不重复,所以其不以Set集合
* 形式返回
*/
Collection<Integer> values = map.values();
for(Integer value:values) {
System.out.println("value:"+value);
}
}
}
Test
package map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
/**
* 统计字符串中每个字符出现的次数
* @author Cole
* 2018年12月13日上午10:57:28
*/
public class Test {
public static void main(String[] args) {
String str = "helloworld!this is java!i love java!";
Map<Character,Integer> map = new HashMap<>();
/*
* Map
* key存出现的字符
* value存该字符出现的次数
*/
for(int i=0;i<str.length();i++) {
char c = str.charAt(i);
//存在表明该字符出现过
if(map.containsKey(c)) {
int num = map.get(c);
num = num + 1;
map.put(c, num);
}else {
map.put(c, 1);
}
}
System.out.println("统计完毕");
System.out.println(map);
}
}
Key
package map;
/**
* HashMap是当今世界上查询速度最快的数据结构,但是作为Key元素
* 的hashCode方法和equals方法的实现如果不妥当,就会降低散列表的查询
* 性能
* 在HashMap中出现链表就会影响其查询性能,而出现链表的一个
* 主要原因:
* 当两个Key元素的hashCode值(hashCode方法返回的数字)相同
* 时(hashcode决定该元素在HashMap内部数组的下标位置)但他们
* equals比较部位true(equals方法决定HashMap是否认为这两个
* Key为重复)时,则会在HashMap内部形成链表
*
* hashCode方法与equals方法是Object定义的方法,这两个方法在
* API手册的Object类中有明确的说明
* 当我们需要重写一个类的equals或hashCode方法时要遵循下面的
* 几点要求:
* 1.成对重写,当我们重写一个类的equals方法时就应当连同重写
* hashCode方法反过来也一样
* 2.一致性,当两个对象equals比较为true时,hashCode方法
* 返回的数字必须相等,反过来则不是必须的,但是也尽量保证当两个
* 对象hashCode相同时equals比较也为true
*
* 3.稳定性,当一个对象参与equals比较的属性值没有发生过改变的前提下,多次调用hashCode
* 方法返回的数字应当不变
* @author Cole
* 2018年12月13日下午2:21:49
*/
public class Key {
private int x;
private int y;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Key other = (Key) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
返回SE 返回主页
一键复制
编辑
Web IDE
原始数据
按行查看
历史