##**java 集合的自我总结**
/*
Collection:
{ vector 实现了Stack;
List 有序 值不唯一(有放入顺序))
ArrayList 底层是一个数组
LinkedList 底层为链表 LinkedList实现了Queue接口
Set 无序 值唯一
hashSet 无序 是根据hashCode的值选择位置 底层是hashMap hashMap是用哈希表(链表+数组)算法实现的
TreeSet 有序 (自然排序)自定义排序需要实现Comparable 底层是Treemap(通过Key来存储Set的值) TreMap是用红黑二叉树实现的}
Map:
hashMap 无序 <K,V> k(set) V(collecyion)
TreeMap 有序 (红黑二叉树实现)
Map 和Set的关系:
都采用了哈希表的实现方法,Set是用Map实现的 只用Map的Key存储元素,Map等同于Set
*
//向Map中存数据
if(map.containsKey(a)){
map.put(a,map.get(a)+1);
}
else{
map.put(a,1);
}
*
输出示例
for(String m:map.keySet())
{
System.out.println(m+map.getKey());
}
*
*/
map
//按照Value的升序排列
将Map转换为List才可按照value排列
Entry为map内部类
//原来默认是按照key的顺序排列
List<Entry<Integer, Integer>> entry = new ArrayList<Entry<Integer, Integer>>(map.entrySet());
Collections.sort(entry, new Comparator<Entry<Integer, Integer>>() {
//升序
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2)
{
return o1.getValue().compareTo(o2.getValue());
}
});
for(Entry<Integer, Integer> entrys:entry) {
System.out.println(entrys.getKey()+"出现的次数是:"+entrys.getValue());
}
请大神多多指正!!!