黑马程序员——集合体系
------- android培训、java培训、期待与您交流! ----------
------------------
|
Collection集合根类。单例集合....
----------------------------
| List接口集合特点:有序,可重复的。
------------------------------------
| ArrayList集合特点:底层维护了一个Object类数组,容量不够时自动增长0.5倍,带索引,所以查询快,增删慢。JDK1.2出现的,线程不安全。
------------------------------------| LinkedList集合特点:底层链表数据结构实现。增删快,查询慢,由于链表实现,增加时只需要让前一个元素记住自己就可以了,删除时让前一个元素记住后一个元素,后一个元素记住前一个元素,这样增删效率很高但查询需要一个个遍历所以效率低。
------------------------------------
| Vector集合特点:和ArrayList原理相同,但线程安全,效率低,底层也是维护一个数组<每个值都会记录下一个值的>,所以查询慢,增删快。JDk1.0出现的。
----------------------------
| Set接口集合特点:无序,不可重复的
------------------------------------| HashSet集合特点:底层是基于哈希表数据结果实现的。
------------------------------------| TreeSet集合特点:底层是二叉树数据结构,存入此集合中的元素会自动按照自然排序,如果该元素不具备自然排序,那么必须实现Comparable接口,实现compareto方法。或者自定义一个类实现Comparator,实现compare方法,创建自定义类对象作为实参传递给TreeSet构造函数中。
------------------------------------| LinkedHashSet
-----------------| Map集合。双列集合根接口也就是键值对存在。键是唯一的
------------------------------------| HashMap 底层基于哈希表实现的,但线程不安全,JDK1.2出现的。
往hashMap存储元素的时候,会先调用键的hashCode方法算出该键存储到哈希表中那个位置,
然后如果该位置没有任何的元素存在,那么该元素可以直接存储,如果算出的位置目前已经存在
着其他的元素,那么还会调用equals方法与该位置的元素再比较一次,如果equals方法返回的
是true,那么该元素不允许被添加,如果equals返回的是false,那么该元素允许被添加。
------------------------------------| TreeMap
也是基于红黑树即二叉树原理的数据结构实现的,如果键是具备了自然顺序的特性,那么就会根据键的 自然顺序
进行排序,如果
键不具备自然顺序的特性
那么键所属的类必须要实现Comparable接口,
或者是在创建TreeMap对象的时候传入一个比较器。
------------------------------------| HashTable<了解即可> 底层也是基于哈希表实现的,实现原理与HashMap一致,但线程安全,效率低。JDK1.0出现的。
------------------------------------| LinkedHashMap
关于HashMap的存储原因代码演示:
package cn.chen.map;
import java.util.HashMap;
public class Demo2 {
public static void main(String[] args) {
HashMap<Person,String> map = new HashMap<Person,String>();
map.put(new Person(110,"chenlong"), "chen");
map.put(new Person(112,"tanglong"), "tang");
map.put(new Person(111,"wangqiang"), "wang");
//如果添加重复的键,那么该键对应的值会取代之前的值。
map.put(new Person(111,"wangqiang"), "qiang");
System.out.println(map);
}
}
class Person{
int id;
String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "编号:"+id+" 姓名:"+name;
}
@Override
public int hashCode() {
System.out.println("====hascode===");
return this.id;
}
@Override
public boolean equals(Object obj) {
System.out.println("====equals====");
Person p =(Person)obj;
return this.id==p.id ;
}
}
关于TreeMap存储原理演示代码:
class Emp{ // implements Comparable<Emp>{
String name;
int salary;
public Emp(String name, int salary) {
super();
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";
}
/*@Override
public int compareTo(Emp o) {
return this.salary - o.salary;
}*/
}
//自定义一个比较器
class MyComparator implements Comparator<Emp>{
@Override
public int compare(Emp o1, Emp o2) {
return o2.salary-o1.salary;
}
}
public class Demo5 {
public static void main(String[] args) {
MyComparator comparator = new MyComparator();
TreeMap<Emp, String> tree = new TreeMap<Emp,String>(comparator);
tree.put(new Emp("chenlong",800),"chen");
tree.put(new Emp("canglaoshi",900), "cang");
tree.put(new Emp("lingdong",700), "ling");
tree.put(new Emp("xiaoyan",1000), "xiao");
System.out.println(tree);
}
}
HashMap集合三种迭代的方法:
第一种方式:keySet()方法,
缺点: keySet方法只能返回所有的键,并不能返回值。
public class Demo1 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("wangqiang", "wang");
map.put("chenlong", "chen");
map.put("tanglong", "tang");
//利用keySet方法,获取Map集合中所有键存入Set集合中返回。
Set<String> set = map.keySet();
//得到的set集合中获取迭代器。
Iterator<String> it = set.iterator();
while(it.hasNext()){
//迭代出set集合中的键并记录
String key = it.next();
System.out.println(key+" : "+map.get(key));//根据键获取值
}
第二种方式:entrySet方法。
public class Demo1 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("wangqiang", "wang");
map.put("chenlong", "chen");
map.put("tanglong", "tang");
//根据entrySet方法获取键值对存入Set集合中返回
Set<Map.Entry<String, String>> set = map.entrySet();
//得到set集合中获取迭代器。
Iterator<Map.Entry<String, String>> it = set.iterator();
while(it.hasNext()){
//迭代出键值对视图
Map.Entry<String, String> entry = it.next();
// entry.setValue("xielijuan");用指定的值替换与此项对应的值、
//利用键值对类中方法获取键值。
System.out.println(entry.getKey()+" : "+entry.getValue());
}
第三种方法:根据values方法,
缺点: 只能获取到值,不能获取到键。
public class Demo1 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("wangqiang", "wang");
map.put("chenlong", "chen");
map.put("tanglong", "tang");
//根据values方法获取值存入集合中返回
Collection<String> coll = map.values(); //把 map中 的所有的值都存储 到一个Collection的集合中返回。
//拿到集合中迭代器。
Iterator<String> it = coll.iterator();
while(it.hasNext()){
System.out.println(it.next());//这种方法只能迭代出值,不能获取健
}
本文详细介绍了Java集合体系,包括Collection和Map两大类集合的特点与应用场景。深入探讨了List、Set、HashMap、TreeMap等核心接口及其实现类的区别。
9248

被折叠的 条评论
为什么被折叠?



