java基础—集合Connection
集合只能单个存储元素 单个存储并且只能存储引用类型
虚线代表的实现 实线代表继承
list集合
List中的存储元素是有序的 并且可重复。存进去什么顺序 取出来还是什么顺序 队列
ArrayList
使用数组存储元素 所以适合查询 不适合增删元素
底层是数组 所以是存在下标的 默认底层初始化容量是10 扩大后为原来的1.5倍
public static void main(String[] args) {
List l = new ArrayList();//创建数组集合
//注意这里的父类用的是List 不是Collection父类
//因为list中有自己独特的方法,如果你用的是Collection的 //话则无法使用子类的特有方法
//添加元素
l.add(12);
l.add(13);
l.add(14);
l.add(15);
l.add(15);
//遍历
Iterator it= l.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
LinkedList
底层采用双向链表数据结构存储数据 链表适合频繁的增删元素 不适合查询元素
Collection c=new LinkedList();
c.add(10);
c.add(new Integer(100));
c.add(3.14);
//获取迭代对象
//不需要关心底层的具体类型
//所有集合以来的迭代器都实现了java.util.Iteractor这个接口
Iterator it = c.iterator();//迭代器是面向接口编程
//IT 是引用。保存了内存地址指向了堆中的迭代器对象
System.out.println(it);
//调用方法 完成遍历(迭代)
while(it.hasNext()) {
Object o = it.next();
System.out.println(o);
}
Vector
底层与LinkedList相同 但是他是线程安全的 效率低 不常用
Set集合
set集合特点相反 存储是无序的 并且数据不可重复 存进去一个顺序但是取出数据时候就会混乱
SortedSet
集合存储元素特点:无序 不可重复 继承set 但是存储进去的元素可以按照元素的大小自动排序
自动排序因为自动调用了comparable接口
//创建集合
AbstractSet s = new TreeSet();
s.add(10);
s.add(50);
s.add(8);
s.add(9);
s.add(40);
Iterator it =s.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
HashSet
底层是哈希表/散列表
hashset底层其实就是一个hashmap hashmap底层调用的是哈希表数据结构
map集合
hashmap
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建集合
Map persion = new HashMap();//hashmap默认初始化容量10 默认加载因子0.75
persion.put("1000", "goudan");
persion.put("1000", "jack");
persion.put("1001", "xiaoming");
persion.put("1002", "mimi");
System.out.println(persion.size());//判断个事
/*persion.clear(); //清空
System.out.println(persion.size());*/
//判断集合中是否有这样的Key
System.out.println(persion.containsKey("1000"));
//判断集合中是否有这样的value 当输入同样的Key时 其中的值会发生覆盖 会被后加入的Key中的值覆盖
System.out.println(persion.containsValue("goudan"));
//通过Key获取value
String k = "1001";
Object v = persion.get(k);
System.out.println(v);
//通过key删除键值对
persion.remove("1000");
System.out.println(persion.size());
//获取所有value
Collection value = persion.values();
Iterator it = value.iterator();//遍历
while(it.hasNext()) {
System.out.println(it.next());
}
//获取所有的key //演示如何遍历map集合
Set keys = persion.keySet();
Iterator i = keys.iterator();
while(i.hasNext()) {
Object K = i.next();
Object V = persion.get(k);
System.out.println(K+"-->"+V);
}
//entrySet() 将map转换生Set集合
//这个方法用得少
Set set=persion.entrySet();
Iterator i1 = set.iterator();
while(i1.hasNext()) {
System.out.println(i1.next());
}
}
hashtable
//创建属性类对象
Properties p =new Properties();
//存 同样key重复了 value值将会被覆盖
p.setProperty("User", "goudan");
p.setProperty("Possword", "123456");
p.setProperty("Like", "pussy");
//取
String v1 = p.getProperty("User");
String v2 = p.getProperty("Possword");
String v3 = p.getProperty("Like");
System.out.println(v1);
System.out.println(v2);
System.out.println(v3);
}
sortedMap
SortedMap products = new TreeMap();
product p1 = new product("西瓜",1.0);
product p2 = new product("香蕉",3.0);
product p3 = new product("桃", 4.0);
product p4 = new product("苹果",5.0);
products.put(p1, 8.0);
products.put(p2, 6.0);
products.put(p3, 5.0);
products.put(p4, 7.0);
//遍历
Set keys = products.keySet();
Iterator it = keys.iterator();
while(it.hasNext()) {
Object name = it.next();
Object price = products.get(name);
System.out.println(name+"--->"+price);
}
}
}
class product implements Comparable{
String name;
double price;
public product(String name,double price) {
this.name=name;
this.price=price;
}
public String toString() {
return "["+name+","+price+"]";
}
public int compareTo(Object o) {
double price1 = this.price;
double price2 = ((product)o).price;
if(price1 == price2) return 0;
if(price1>price2) {
return -1;
}
else {
return 1;
}
}