JAVA 集合操作總結
阿新 • • 發佈:2018-05-04
dna arr -o2 inter map 實現 void array 遍歷
1.Collection
1.基本操作
對集合的基礎操作 1.boolean add(Object o) //添加對象到集合 2.boolean remove(Object o) //刪除指定的對象 3.int size() //返回當前集合中元素的數量 4.boolean contains(Object o) //查找集合中是否有指定的對象。 5.boolean isEmpty() //判斷集合是否為空 6.Iterator iterator() //返回一個叠代器 操作整個集合的方法 7.boolean containsAll(Conllection c) //查找集合中是否有集合C中的元素 8.boolean addAll(Conllection c) //將集合c中的所有元素添加給該集合 9.void clear() //刪除集合中所有元素 10.void removeAll(Collection c) //從集合中也有的元素 11.void retainAll(Collection c) //從集合中刪除集合c中不包含的元素. 對數組操作的方法 12.Object[] toArray() //返回一個包含集合中所有元素的數組
2.集合中的遍歷:
1.for-each語法
Collection<Person> persons = new ArrayList(<Person>)();
for(Person person :persons){
Sys.out.println(person.name);
}
2.使用叠代器Iterator
Collection<Person> persons = new ArrayList<Person>();
Iterator iterator = persons.iterator();
while(iterator.hasNext ()){
System.out.println(iterator.next);
}
3.主要子接口對象
1.Set(無序、不能重復)
Eenuset SortedSet HashSet TreeSet
Set裏面存放的對象是無序,不能重復的,集合中的對象不按特定的方式排序,只是簡單的把對象加入集合中。
2.List(有序、可重復)
List裏面存放的對象是有序的,同時也是可以重復的,List關註的是索引,擁有一些列和索引相關的方法,查詢速度快。
ArrayList LinkedList Vector
3.Queue
Deque priorityQueue ArrayDeque
2.Map
基本方法:
1. boolean put(key,value) //添加一個map
2. boolean putAll(Map m) //存入一個map
3. boolean remove(key) //刪除某一個值
4. boolean clear() //清除所有內容
5. value get(key) //根據鍵獲取某個值
6. boolean isEmpty() //判斷是否為空
7. boolean containsKey(key) //判斷集合中是否包含指定的key
8. boolean containsValue(value) //判斷集合中是否包含某一個值
9. Int size() //Map的長度
鍵值對操作
10.Set KeySet() //返回所有key對象的集合
11.Collection values() //獲取所有的值
12.Set entrySet() //將map 集合中的鍵值映射關系打包成一個對象。
遍歷:
Map<Integer,Integer> map = new HashMap<Integer Integer>();
for(Map.Entry<Integer,Integer> entry:map.entrySet()){
System.out.println("key="+entry.getKey()+",Value="+entry.getValue());
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//遍歷map中的鍵
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//遍歷map中的值
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
使用Iterator遍歷
使用泛型:
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
Iterator<Map.Entry<Integer,Integer>> entries = map.entrySet().interator();
while(entries.hasNext()){
Map.Entry<Integer,Integer> entry = entries.next();
System.out.println("Key = "+entry.getKey()+",Value="+entry.getValue());
}
不使用泛型:
Map map = new HashMap();
Iterator entries = map.entrySet().interator();
while(entries.hasNext()){
Map.entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("key = "+key+",value="+value);
}
3.線程安全和不安全
1.加入join 控制
public static void main(String[] args) throws InterruptedException {
List<String> mylist = new Vector<String>();
List<Thread> myThread = new ArrayList<Thread>();
for(int i=1;i<=5;i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10 ; j++) {
mylist.add("a");
}
}
});
t.start();
myThread.add(t);
}
for (Thread t:myThread) {
t.join();
}
System.out.println(mylist.size());
}
2.同步的ArrayList
Collections.synchronizedList(new ArrayList
public static void main(String[] args) throws InterruptedException {
List<String> mylist = Collections.synchronizedList(new ArrayList<String>());
List<Thread> myThread = new ArrayList<Thread>();
for(int i=1;i<=5;i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10 ; j++) {
mylist.add("a");
}
}
});
t.start();
myThread.add(t);
}
for (Thread t:myThread) {
t.join();
}
System.out.println(mylist.size());
}
3.同步代碼塊
加入synchronized 鎖住代碼塊
public static void main(String[] args) throws InterruptedException {
List<String> mylist = new Vector<String>();
for(int i=1;i<=5;i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
synchronized (mylist){
for (int j = 0; j < 10 ; j++) {
mylist.add("a");
}
System.out.println(mylist.size());
}
}
});
t.start();
}
}
3.HashMap HashSet HashCode
HashSet是根據hashMap來實現的
TreeSet是根據TreeMap來實現的 TreeMap默認支持排序
1.判斷hashMap裏面的對象是否相等
package core;
public class Product {
private int prodID;
private String ProdName;
public Product(int prodID, String prodName) {
this.prodID = prodID;
ProdName = prodName;
}
public int getProdID() {
return prodID;
}
public void setProdID(int prodID) {
this.prodID = prodID;
}
public String getProdName() {
return ProdName;
}
public void setProdName(String prodName) {
ProdName = prodName;
}
@Override
public int hashCode() {
return (this.getProdName()+String.valueOf(this.getProdID())).hashCode();
}
@Override
public boolean equals(Object obj) {
Product getObj = (Product)obj;
if(this.getProdID()==((Product) obj).getProdID() && this.getProdName()==((Product) obj).getProdName()){
return true;
}else{
return false;
}
}
}
2.TreeMap進行排序
Map<String,Integer> user = new TreeMap<String,Integer>((String o1,String o2)->{
return o2.length()-o1.length()+o2.compareTo(o1); //加上Asics碼表比較
});
user.put("smartom",20);
user.put("zhangsan",18);
user.put("wangwu",100);
user.put("lisi",20);
for (Map.Entry<String,Integer> entry:user.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
3.數據結構二叉樹的建立
package BTree;
import java.util.Comparator;
public class BTree<T> {
Node<T> root = null;
//支持comparator接口
Comparator<T> comparator;
public BTree(Comparator<T> c){
comparator = c;
}
public BTree() {
}
//插值
public void put(T data){
if(root ==null){
root = new Node<T>(data);
}else{
// leftNode
root = addNode(root,data);
}
}
public void list(){
list(root);
}
public void list(Node n){
if(n!=null){
list(n.leftNode);
System.out.println(n.selfdata.toString());
list(n.rightNode);
}
}
Node<T> addNode(Node<T> node,T data){
if(node == null){
return new Node<T>(data);
}
int result = 0; //是那種比較對象
if (comparator!=null) {
result = comparator.compare(data,node.selfdata);
}else{
Comparable<T> comparable = (Comparable<T>)data;
result = comparable.compareTo(node.selfdata);
}
if(result<0){ //小就放左節點
node.leftNode = addNode(node.leftNode,data);
}else if(result > 0){
node.rightNode = addNode(node.rightNode,data);
}
return node;
}
class Node<T>{
private Node<T> leftNode= null;
private Node<T> rightNode = null;
private T selfdata;
public Node(T selfdata) {
this.selfdata = selfdata;
}
}
}
BTree<Product> bTree = new BTree<Product>((Product o1,Product o2)->{
return o1.getProdID()-o2.getProdID();
});
bTree.put(new Product(101,"java圖書"));
bTree.put(new Product(102,"php圖書"));
bTree.put(new Product(1014,"node圖書"));
bTree.put(new Product(103,"python圖書"));
bTree.put(new Product(105,"esma圖書"));
bTree.put(new Product(102,"html圖書"));
bTree.list();
BTree<String> bTree2 = new BTree<String>();
bTree2.put("3");
bTree2.put("5");
bTree2.put("7");
bTree2.put("9");
bTree2.put("5");
bTree2.list();
Queue列隊
Queue
線程
常用方法:
Thread
Thread(String name)
Thread(Runable target)
Thread(Runable target,String name)
線程的方法:
void start() 啟動線程
static void sleep(long millis)
static void sleep(long millis,int nanos) 線程休眠
void join() 是其他線程等待當前線程終止
void join(long millis)
void join(long millis,int nanos)
static void yield() 當前運行線程釋放 處理器資源
JAVA 集合操作總結