A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wjiuchaol1 中级黑马   /  2015-12-15 22:31  /  760 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

双列集合:
map
k.映射维护键类型
v. 映射值的类型
一个映射不能包含重复的键
每个键最多值能映射到一个值

map是双列的. collection是单列的
map的键是唯一的. collection的子体细set是唯一的
map集合的数据结构值针对键有效,. 跟值无关 collection集合数据结构是针对元素有效

注意.
hashSet 底层依赖的是HasMap.
功能:
        添加:put(); 如果键是第一次添加 反悔null 如果不是第一次添加就用新值去替换就值
并且将就值返回

        删除:remove() 根据键删除该键值对 并且返回值 clear();

        判断:isEmpty()
             containskey()  containsValue

        获取: entryset  keyset get valuse

        长度:size() 返回键值对 的对数

Map<String , Integer> map = new HashMap<>();
Integer i1 = map.put("张散",23);
Integer i2 = map.put("张散",23);
Integer i3 = map.put("张散",23);
Integer i4 = map.put("张散",23); 相同的键不存储 覆盖的值返回

System.out.println(i1)
System.out.println(i2)
System.out.println(i3)
System.out.println(i4)



Map<String , Integer> map = new HashMap<>();
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)

//Integer value =map.remove("张三");
//System.out.println(value);
System,.out.println(map.containsKey("张三"));
System,.out.println(map.containsvalue("100"));
system.out.println(map);



Map<String , Integer> map = new HashMap<>();
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)
collection<Integer> c =  map.values();
System.out.println(c);
System.out.println(map.size());


双列集合如何遍历
1:根据键找值
获取键集合Ketset
遍历键的集合,.获取到每一个键 增强for 迭代骑也可以
根据键找到对应的值   get()

2.根据键值对对象 找键和值.
获取所有键值对的集合
遍历键值对的集合获取到每一个兼职对
根据键值对对象 找到对应的键和值

Map<String , Integer> map = new HashMap<>();
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)

Integer i = map.get("张三"); // 根据键获取值
System.out.println(i);

//获取所有的键
Set<String> keySet = map.KeySet(); //键集合
Iterator<String> it = KeySet.iterator();//获取大袋其
while(it.hasNext()) //判断集合中是否有元素
String Key = it.next(); //获取每一个贱
Integer value = map.get(key) //根据键获取值
System.out.println(key + ".."+value);

增强for循环:
for(String key : map.keyset()) //map.keyset获取所有键的集合
System.out.println(key + "= " map.get(key));


根据兼职对对象 获取键和值:
Map<String , Integer> map = new HashMap<>();
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)
map.put("张散",23)

set<map.Enery<String,integer>> entrySet = map.entrySet();
Iterator<map.Enery<String,integer>> it = entrySet.iterator();
while(it.hasNext())
map.Entry<String,Integer> en = it.next();
String key = en.getKey()
Integer value = en.Getvalue();
System.out.println(key+ "=" + value)






inteerface inter
interface inter2
public void show();

class Demo implements inter.inter2

public void show()



增强for:
for(Enery<String,integer >) en: map.entryset()
System.out.println(en.getKey() + "=" en.getvalue);

HashMap集合键是Student值是String
键是学生对象,代表每一个和学生\
值是字符串对象,代表学生归属第

HashMap<Student, String> hm = new HashMap<>();
hm.put(new Student("张散",23),"北京");
hm.put(new Student("张散的",23),"南京");
hm.put(new Student("张散的是",23),"东京");
hm.put(new Student("张散啊",23),"取经");

System.out.println(hm);

HashMap:
如果存储的元素 键是自定义类型 要保证键的唯一.必须重写Hashcode和equals方法
输出语句直接打印对象默认调用的是该对象的toString方法
如果打印的不是地址值肯定是该类或者该类的父类重写了object类中的头String方法

linkedHashMap的特点:
底层是链表实现的.可以保证怎么存就怎么取 (存取一致)
知识点回顾:
        linkedHashSet : 他是Set体系中唯一一个可以保证存取一直的集合类
       

LinkedHashMap<Student, integer> lhm  = new LinkedHashhMap<>();

lhm.put("张撒贝宁",23);
lhm.put("张撒贝宁",23);
lhm.put("张撒贝宁",23);
lhm.put("张撒贝宁",23);

System.out.println(lhm);

treeMap:
存储的键如果是自定义类型 如何保证键的唯一性
第一种:
让该自定义类型实现Comparable借口 然后重写借口中的compareto方法

第二种:
创建TreeMap集合对象的时候传入一个比较器的借口comparator的子类对象 重写借口
       
TreeMap<Student , String> tm = new TreeMap<>(new comparator<Student>);
public int compre(Student s1 Student s2)
int num = s1.getName() .compareTp(s2.getName)
ruturn num == 0? S1.getAge - s2.getAge : num
tm.put(new Student("张散",233)."东京")
tm.put(new Student("张散的",223)."大板")
tm.put(new Student("张散是",213)."妈妈桑")
tm.put(new Student("张散啊",23333)."shit")

System.out.println(tm);

统计字符串每个字符出现次数,String str= "aaaabbbcccc";

思路:
<Character , Integer>  
定义一个需要被统计的字符串
将字符串转换为字符数组
定义双链集合存储字符串中字符和出现的次数
遍历字符数组获取每一个字符,并将字符存储在双列集合中
纯矗过程中要做判断.如果集合中不包含这个键.就将该字符当作键值为1存储
打印双列集合获取字符出现的次数
String s = "aaaavvvvvvvsdsdxafasdf"
char[] arr = s.toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for(Char c : arr )
/*if(!hm.containsKey(c))
hm.put(c,1);
else
hm.put(c,hm.get(c)+1)*/

hm.put(c.!hm.containsKey(c)? 1:hm.get(c)+1);
for(Character Key : hm.keySet())
System.out.println(Key + "=" + hm.get(Key));


集合嵌套HashMap嵌套HashhMap

需求.双园课堂很多基础班
第88期定义成一个双列集合.键是学生对象, 值是学生归属第
第99期定义成一个双列集合.键是学生对象, 值是学生归属第

无论88还是99 都是班级对象,为了便于统一管理把这些班级对象添加到双园课堂 集合中
HashMap<Student,String> hm88 = new HashWap
hm88.put(new Student("咋好难过",2122),"北京")
hm88.put(new Student("咋好的",222),"北京")
hm88.put(new Student("咋好难是过",232),"北京")
hm88.put(new Student("咋啊好难过",242),"北京")

HashMap<Student,String> hm99 = new HashWap
hm99.put(new Student("咋好是难过",2122),"北京")
hm99.put(new Student("咋好啊啊的",222),"北京")
hm99.put(new Student("咋好的难是过",232),"北京")
hm99.put(new Student("咋我我啊好难过",242),"北京")


HashMap<HashMap<Student,String>,String> hm = new HashWap

hm.put(hm,"88起基础班")
hm.put(hm,"99起基础班")

for(HashMap<Student,String> h : hm.KeySet())
String value = hm.get(h);
for(Student key:h.KeySet())
String value2 = h.get(Key);
System.out.println(Key + "="+ value2+ "="+value)


通过HashMap的嵌套来实现遍历的时候可以通过增强for的嵌套来做

重点面试题:HashMap和Hashtable的区别:
共同点: 底层都是哈希算法 都是双列集合
区别:
1,HashMap是线程不安全的.效率高,jdk.1.2版本
,Hashtable是现成安全的,. 效率底,jdk.1.0版本的
2,HasgMap可以存储null键和值
Hashtable不可以存储null键和null值


Collections : 操作Collection集合的工具类.
sort();
binarySearch();
max();
reverse();
shuffle();


ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("b");
list.add("a");
list.add("f");
list.add("d");

System.out.println(list);
Collections.sort(list);
System.out.println(list);



二分查找法:
ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("b");
list.add("a");
list.add("f");
list.add("d");
System.out.println(Collections.binrySearch(list,"c"));


获取最大值: Collection list
ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("b");
list.add("a");
list.add("f");
list.add("d");
System.out.println(Collections.max(list));


反转:
ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("b");
list.add("a");
list.add("f");
list.add("d");
Collections.reverse(list);
System.out.println(list);


随机排序:
ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("b");
list.add("a");
list.add("f");
list.add("d");

Collections.shuffle(list);
System.out.println(list);

以上全都是list体系.gogogo!


发扑克.这么长是要搞死我啊
分析:买一副扑克.其实就是自己创建一个集合对象,将扑克排存储进去
洗牌
发牌
看牌

String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q""K"};
String[] color = {"红桃"."黑桃","方片","梅花"};
ArrayList<String> poker new ArrayList<>();

for(String s1 : color)
for(String s2 : num)
poker.add(s1.concat(s2)); //连接两个字符串





评分

参与人数 1技术分 +3 收起 理由
张研老师 + 3 神马都是浮云

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马