collection是集合的顶级父类接口
List 有序可重复集合
add增加,remove删除
linkedlist
基于双向链表,线程安全,增删改快,查询慢
但是,可以通过push形成栈的形式,后进先出,通过pop()删除。
vector
线程安全,基于数组,增删改快,但是查询慢
arraylist
线程非安全,基于数组,查询快,但是增删改慢
set无序不可重复集合
add增加,remove删除
set的遍历是通过迭代器。
第一种:通过map.keyset() 。第二种map.entry(). 第三种:通过迭代器iterator
hashset
取得时候是无序的
treeset
默认会排序的,但是当类型时无法排序类型时,需要javabean继承compareable。给出排序规则
linkedhashset
怎么存就怎么取
map集合
添加put,删除remove、clear
四种遍历方式,
1,只能得到值,
for(string value:maps.values){
}
2,通过迭代器
3,String 遍历
使用put(),增加数据。使用get(),得到数据
是键值对的形式
treemap ,
和treeset一样是默认排序,如果是javabean无法排序类型,需要继承compareable给出排序规则
hashmap
线程非同步,键值都可以为空,
hashtable
线程同步,安全,键不可以为空,但是性能很差。
ConcurrentHashMap
在提高性能的基础上,没有降低太多的性能。用的分段锁。
package collection.map;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ItMap {
public static void main(String[] args) {
Map<String,String> maps =new HashMap<String, String>();
maps.put("1","String");
maps.put("2","String");
Set set=maps.entrySet();
Iterator it =set.iterator();
while (it.hasNext()){
Map.Entry er= (Map.Entry)it.next();
System.out.println(er.getKey()+","+er.getValue());
}
Set set1=maps.keySet();
Iterator its = set1.iterator();
while (its.hasNext()){
Object key = its.next();
System.out.println(key+","+ maps.get(key));
}
Set<String> ss=maps.keySet();
for (String s:ss)
{
System.out.println(s+","+maps.get(s));
}
}
}
输出结果:
1,String
2,String
1,String
2,String
1,String
2,String