标签:contain source smart 包含 nano java 集合操作 处理 arraylist 关系
对集合的基础操作
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() //返回一个包含集合中所有元素的数组
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);
}
Eenuset SortedSet HashSet TreeSet
Set里面存放的对象是无序,不能重复的,集合中的对象不按特定的方式排序,只是简单的把对象加入集合中。
List里面存放的对象是有序的,同时也是可以重复的,List关注的是索引,拥有一些列和索引相关的方法,查询速度快。
ArrayList LinkedList Vector
Deque priorityQueue ArrayDeque
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);
}
使用泛型:
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);
}
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());
}
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());
}
加入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();
}
}
HashSet是根据hashMap来实现的
TreeSet是根据TreeMap来实现的 TreeMap默认支持排序
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;
}
}
}
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());
}
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
常用方法:
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() 当前运行线程释放 处理器资源
标签:contain source smart 包含 nano java 集合操作 处理 arraylist 关系
原文地址:https://www.cnblogs.com/subtract/p/8989363.html