集合框架(Map集合/双列集合)

Map集合与Set集合的关系:

1,Map集合与Set集合可以相互转化,从继承体系看,这些Map接口实现类和Set接口实现类的类名完全相同。
2,Map集合中所有的key具有Set集合的特征,只要把Map所有的key集中来看,它就是一个Set,Set的底层就是使用的Map。当把Set集合中的元素看做是键值对是,他就是相当于Map。
3,对于Map集合而言,它实质上是一个关联数组。
二、Map特点:
Map集合:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
 1,添加。
  put(K key, V value) 
  putAll(Map<? extends K,? extends V> m)
 2,删除。
  clear() 
  remove(Object key) 
 3,判断。
  containsValue(Object value) 
  containsKey(Object key) 
  isEmpty() 
 4,获取。
  get(Object key) 
  size() 
  values() 

  entrySet() 
  keySet() 

Map:  
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0.效率低。  
|--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,jdk1.2.效率高。  
|--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的键进行排序。 示例: 
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<k> keySet:将map中所有的键存入到Set集合。因为set具备迭代器。所有可以迭代方式取出所有的键,在根据get方法。获取每一个键对应的值。 Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。 


2,Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是:Map.Entry
Entry其实就是Map中的一个static内部接口。
为要定义在内部呢?
因为只有有了Map集合,有了键值对,才会有键值的映射关系。关系属于Map集合中的一个内部事物。而且该事物在直接访问Map集合中的元素。


代码:

public class MapDemo2 {
 public static void main(String[] args) {
  Map<String, String> map = new HashMap<String, String>();
  map.put("01", "zhangsan01");
  map.put("02", "zhangsan02");
  map.put("03", "zhangsan03");
  map.put("04", "zhangsan04");
  // 第一种方法
  Set<String> keySet = map.keySet();
  for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
   String key = it.next();
   String value = map.get(key);
   sop(key + "=" + value);
  }
  sop("———————————————————————————————————————————————");
  // 第二种方法
  Set<Map.Entry<String, String>> entrySet = map.entrySet();
  for (Iterator<Map.Entry<String, String>> it = entrySet.iterator(); it
    .hasNext();) {
   Map.Entry<String, String> me = it.next();
   String key = me.getKey();
   String value = me.getValue();
   sop(key + "=" + value);
  }
 }
 private static void sop(Object obj) {
  System.out.println(obj);
 }
}


练习:

需求:存储一组学生,每个学生有对应的地址
分析:每一个学生都有对应的归属地。学生Student,地址String。学生属性:姓名,年龄。注意:姓名和年龄相同的视为同一个学生。
保证学生的唯一性。
1,描述学生。
2,定义map容器。将学生作为键,地址作为值,存入。
3,获取map集合中的元素。
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", 21), "beijing");
		hm.put(new Student("lisi1", 21), "tianjin");
		hm.put(new Student("lisi2", 22), "shanghai");
		hm.put(new Student("lisi3", 23), "nanjing");
		hm.put(new Student("lisi4", 24), "wuhan");

		// 取出方式 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);
		}
	}
}

TreeMap

需求:对学生对象的年龄进行升序排序。 因为数据是以键值对形式存在的。 所以要使用可以排序的Map集合。TreeMap。 

public class MapTest2 {
 public static void main(String[] args) {
  TreeMap<Student, String> tm = new TreeMap<Student, String>(new nameComp());
  
  tm.put(new Student("lisi1", 25), "wuhan");
 // tm.put(new Student("lisi1", 21), "beijing");
  tm.put(new Student("lisi3", 23), "shanghai");
  tm.put(new Student("lisi4", 24), "zhengzhou");
  tm.put(new Student("lisi2", 26), "tianjin");
  
  Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
  for(Iterator<Map.Entry<Student, String>> it = entrySet.iterator(); it.hasNext();){
   Map.Entry<Student, String> me = it.next();
   Student stu = me.getKey();
   String address = me.getValue();
   System.out.println(stu+":::"+"address:"+address);
  }
 }
}


class nameComp implements Comparator<Student>{
 @Override
 public int compare(Student s1, Student s2) {
  int num = s1.getName().compareTo(s2.getName());
  if (num == 0)
   return s1.getAge() - s2.getAge();
  return num;
 } 
}

练习:

"sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。
希望打印结果:a(1)c(2).....
public class MapTest3 {
 public static void main(String[] args) {
  String str = "sdfgzxcvasdfxcvdf";
  String str1 = "aaaabbbccd";
  String result = getStrLen(str1);
  System.out.println(result);
 }


 public static String getStrLen(String str) {
  TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
  char[] chs = str.toCharArray();
  for (int x = 0; x < chs.length; x++) {
   if (!((chs[x] >= 'a' && chs[x] <= 'z') || (chs[x] >= 'A' && chs[x] <= 'Z')))
    continue;
   if (!tm.containsKey(chs[x]))
    tm.put(chs[x], 1);
   else
    tm.put(chs[x], tm.get(chs[x]) + 1);
  }
  StringBuilder sb = new StringBuilder();


  Set<Character> keySet = tm.keySet();
  for (Iterator<Character> it = keySet.iterator(); it.hasNext();) {
   Character ch = it.next();
   Integer len = tm.get(ch);
   sb.append(ch + "(" + len + ")");
  }
  return sb.toString();
 }
}









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值