标签:-- prime 参考 ++ htable img i++ name abc
集合框架:
另外:
(1)ArrayList: List list = new ArrayList();
(2)HashMap使用:put进去时,如果存在key,则替换value,如果不存在Key,则新建一个k-v,而且HashMap可以接受为null的键值(key)和值(value)
Map m = Collections.synchronizeMap(hashMap);
另外关于 代替hashtable 的 ConcurrentHashMap 参考: http://www.importnew.com/21388.html
1 package com.wang.map; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 8 public class TestHashMap { 9 10 /** 11 * @param args 12 */ 13 public static void main(String[] args) { 14 //先将字符串放到一个char数组中去 15 String str = "ababcd"; 16 char[] ch = str.toCharArray(); 17 HashMap<Character, Integer> map = new HashMap<>(); 18 19 int count = 0; 20 //遍历数组,判断char数组中的元素重复情况 21 for (int i = 0; i < ch.length; i++) { 22 Integer value = map.get(ch[i]); 23 // 判断map中是否存在某值 24 if (value != null) { 25 // 存在的话,返回value,将value+1后赋值给该key的值 26 count = value; 27 } 28 count++; 29 // 不存在的话,将该值put进map,并且赋值1, 30 map.put(ch[i], count); 31 count = 0; 32 } 33 //System.out.println(map); 34 35 //hashMap的entrySet()方法,将map中的数据存在set集合中,从而便于使用iteration遍历 36 Set<Entry<Character,Integer>> set = map.entrySet(); 37 Iterator<Entry<Character, Integer>> it = set.iterator(); 38 while (it.hasNext()) { 39 Entry<Character, Integer> next = it.next(); 40 Character k = next.getKey(); 41 Integer v = next.getValue(); 42 System.out.println(k + "(" + v + ")"); 43 } 44 } 45 46 }
--------------------------------------
d(1)
b(2)
c(1)
a(2)
-------------------------------------
HashSet:不允许重复,其中对象的HashCode值决定了在哈希表中的存储位置
1 package set; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 7 class Student{ 8 private int sn; 9 private String name; 10 private int age; 11 public Student(int sn, String name, int age) { 12 super(); 13 this.sn = sn; 14 this.name = name; 15 this.age = age; 16 } 17 public String toString() { 18 return "Student [sn=" + sn + ", name=" + name + ", age=" + age + "]"; 19 } 20 public int hashCode() { 21 final int prime = 31; 22 int result = 1; 23 result = prime * result + sn; 24 return result; 25 } 26 public boolean equals(Object obj) { 27 if (this == obj) 28 return true; 29 if (obj == null) 30 return false; 31 if (getClass() != obj.getClass()) 32 return false; 33 Student other = (Student) obj; 34 if (sn != other.sn) 35 return false; 36 return true; 37 } 38 39 40 } 41 public class HashSetDemo { 42 43 /** 44 * @param args 45 */ 46 public static void main(String[] args) { 47 Set<Student> stu = new HashSet<>(); 48 stu.add(new Student(1,"zhangsan",17)); 49 stu.add(new Student(1,"zhangsan",17)); 50 stu.add(new Student(3,"lisi",18)); 51 stu.add(new Student(4,"lisi",18)); 52 System.out.println(stu.size()); 53 System.out.println(stu.toString()); 54 } 55 56 }
------------------------------------------------------
3
[Student [sn=3, name=lisi, age=18], Student [sn=4, name=lisi, age=18], Student [sn=1, name=zhangsan, age=17]]
--------------------------------------------------------
标签:-- prime 参考 ++ htable img i++ name abc
原文地址:http://www.cnblogs.com/wang--lei/p/7289279.html