01-java学习笔记01
1.Map集合
Map集合:该集合存储键值对。一对一对往里存。
并且要保证
键的惟一性。
Map<
K,
V>
1. 添加 1. put(
K key,
V value)
2. putAll(Map<? extends K,? extends V> m)
2. 删除
1. clear()
2. remove(Object key)
3. 判断
1. containsKey(Object key)
2. containsValue(Object value)
3. isEmpty()
4. 获取
1.
V get(Object key)
2. size()
3.
Collection<V> values()
entrySet()
keySet()
Map与Collection
Map与Collection在集合框架中属并列存在
Map存储的是键值对
Map存储元素使用put方法,Collection使用add方法
Map集合没有直接取出元素的方法,而是先转成Set集合,在经过迭代获取元素
Map集合中键要保证惟一性
Map集合经常使用类
Hashtable:线程安全(
同步),速度慢,
不容许存放null键,null值,已被HashMap替代。JDK1.0 效率低
HashMap:线程不安全(
不一样步),速度快,
容许存放null 键,null值。JDK1.2 效率高
TreeMap:对键进行排序,排序原理与TreeSet 相同。
Map
|--Hashtable 底层是哈希表数据结构 用做键的对象必须实现
hashCode方法和
equals方法
|--HashMap 底层是哈希表数据结构
|--TreeMap 底层是二叉树数据结构。线程不一样步。能够用于给Map集合中的键
排序
和Set很像
其实TreeSet的底层就是TreeMap
Map共性方法
import java.util.*;
class MapDemo
{
public
static
void main(String[] args)
{
Map<String,String> map =
new HashMap<String,String>();
//
添加元素,若是添加时出现相同的键,那么后添加的值会覆盖原有键对应值。
//
而且put方法会返回被覆盖的值。
System.out.println("put:"+map.put("01","zhangsan1"));
System.out.println("put:"+map.put("01","wnagwu"));
map.put("02","zhangsan2");
map.put("03","zhangsan3");
System.out.println("containsKey:"+map.containsKey("022"));
//
System.out.println("remove:"+map.remove("02"));
System.out.println("get:"+map.get("023"));
map.put("04",
null);
//
没有意义
System.out.println("get:"+map.get("04"));
//
能够经过get方法的返回值来判断一个键是否存在。经过返回null来判断。
//
获取map集合中全部的值。
Collection<String> coll = map.values();
System.out.println(coll);
System.out.println(map);
}
}
map集合的两种取出方式:
1.Set<E>keySet 将Map中全部的键存到Set集合。由于set具有迭代器。因此能够用迭代方式取出全部的键。
再根据get方法。获取每个键对应的值
2.entrySet
1.keySet
//先获取map集合的全部键的Set集合,keySet();
Set<String> keySet = map.keySet();
//有了Set集合。就能够获取其迭代器。
Iterator<String> it = keySet.iterator();
while(it.hasNext())
{
String key = it.next();
//有了键能够经过map集合的
get方法获取其对应的值。
String value = map.get(key);
System.out.println("key:"+key+",value:"+value);
}
2.entrySet
将map集合中的映射关系取出存到一个集合Set中,
这个关系就是Map.Entry类型
关系对象Map.Entry获取到后,
就能够经过Map.Entry中的getKey()和getValue方法获取到key和value
Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口。
interface Map
{
public static Interface Entry
{
public abstract Object getKey();
public abstract Object getValue();
}
}
class HashMap implements Map
{
class Haha implements Map.Entry
{
public Object getKey();
public Object getValue();
}
}
练习
1.Map
每个学生都有对应的归属地。
学生Student,地址String。
学生属性:姓名,年龄。
注意:姓名和年龄相同的视为同一个学生。
保证学生的惟一性。
1,描述学生。
2,定义map容器。将学生做为键,地址做为值。存入。
3,获取map集合中的元素。
import java.util.*;
class Student
implements Comparable<Student>
{
private String name;
private
int age;
Student(String name,
int age)
{
this.name = name;
this.age = age;
}
public
int compareTo(Student s)
{
int num =
new Integer(
this.age).compareTo(
new Integer(s.age));
if(num==0)
return
this.name.compareTo(s.name);
return num;
}
public
int hashCode()
{
return name.hashCode()+age*34;
}
public
boolean equals(Object obj)
{
if(!(obj
instanceof Student))
throw
new ClassCastException("类型不匹配");
Student s = (Student)obj;
return
this.name.equals(s.name) &&
this.age==s.age;
}
public String getName()
{
return name;
}
public
int getAge()
{
return age;
}
public String toString()
{
return name+":"+age;
}
}
class MapTest
{
public
static
void main(String[] args)
{
HashMap<Student,String> hm =
new HashMap<Student,String>();
hm.put(
new Student("lisi1",20),"beijing");
hm.put(
new Student("lisi1",20),"tianjin");
hm.put(
new Student("lisi2",22),"shanghai");
hm.put(
new Student("lisi3",24),"nanjing");
hm.put(
new Student("lisi4",25),"shenzhen");
//
第一种取出方式 keySet
Set<Student> keySet = hm.keySet();
Iterator<Student> it = keySet.iterator();
while(it.hasNext())
{
Student stu = it.next();
String addr = hm.get(stu);
System.out.println(stu+".."+addr);
}
//
第二种取出方式 entrySet
Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();
while(iter.hasNext())
{
Map.Entry<Student,String> me = iter.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+"....."+addr);
}
}
}
2. TreeMap需求:对学生对象的年龄进行升序排序。
由于数据是以键值对形式存在的。
因此要使用能够排序的Map集合。TreeMap。
import java.util.*;
class StuNameComparator
implements Comparator<Student>
{
public
int compare(Student s1,Student s2)
{
int num = s1.getName().compareTo(s2.getName());
if(num==0)
return
new Integer(s1.getAge()).compareTo(
new Integer(s2.getAge()));
return num;
}
}
class MapTest2
{
public
static
void main(String[] args)
{
TreeMap<Student,String> tm =
new TreeMap<Student,String>(
new StuNameComparator());
tm.put(
new Student("blisi3",23),"nanjing");
tm.put(
new Student("lisi1",21),"beijing");
tm.put(
new Student("alisi4",24),"wuhan");
tm.put(
new Student("lisi1",21),"tianjin");
tm.put(
new Student("lisi2",22),"shanghai");
Set<Map.Entry<Student,String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Student,String> me = it.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+":::"+addr);
}
}
}
3.TreeMap练习
/*
练习:
"sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。
但愿打印结果:a(1)c(2).....
经过结果发现,每个字母都有对应的次数。
说明字母和次数之间都有映射关系。
注意,当发现有映射关系时,能够选择map集合。
由于map集合中存放就是映射关系。
何时使用map集合呢?
当数据之间存在这映射关系时,就要先想map集合。
思路:
1,将字符串转换成字符数组。由于要对每个字母进行操做。
2,定义一个map集合,由于打印结果的字母有顺序,因此使用treemap集合。
3,遍历字符数组。
将每个字母做为键去查map集合。
若是返回null,将该字母和1存入到map集合中。
若是返回不是null,说明该字母在map集合已经存在并有对应次数。
那么就获取该次数并进行自增。,而后将该字母和自增后的次数存入到map集合中。覆盖调用原理键所对应的值。
4,将map集合中的数据变成指定的字符串形式返回。
import java.util.*;
class MapTest3
{
public
static
void main(String[] args)
{
String s= charCount("ak+abAf1c,dCkaAbc-defa");
System.out.println(s);
}
public
static String charCount(String str)
{
char[] chs = str.toCharArray();
TreeMap<Character,Integer> tm =
new TreeMap<Character,Integer>();
int count = 0;
for(
int x=0; x<chs.length; x++)
{
if(!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z'))
continue;
Integer value = tm.get(chs[x]);
if(value!=
null)
count = value;
count++;
tm.put(chs[x],count);
//
直接往集合中存储字符和数字,为何能够,由于自动装箱。
count = 0;
/*
if(value==null)
{
tm.put(chs[x],1);
}
else
{
value = value + 1;
tm.put(chs[x],value);
}
*/
}
//
System.out.println(tm);
StringBuilder sb =
new StringBuilder();
Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Character,Integer> me = it.next();
Character ch = me.getKey();
Integer value = me.getValue();
sb.append(ch+"("+value+")");
}
return sb.toString(); } }