//使用散列算法
set<T> t=new HashSet<T>();
//SortedSet 是一個接口TreeSet是它的唯一實現類
//TreeSet 將元素存儲在紅-黑樹結構中
//可以實現排序 升序(0,1,2)
SortedSet<T> t=new TreeSet<T>();
set<T> t=new TreeSet<T>();
map 通過計算出現的次數
Map<Integer, Integer> map=new HashMap<Integer, Integer>();
Random ran=new Random(40);
for (int i = 0; i < 100; i++) {
Integer r=ran.nextInt(20);
Integer v=map.get(r);
map.put(r, v==null?0:v+1);
}
System.out.println(map);
Java中Arrays的asList()方法 可以將 數組轉爲List 但是,這個數組類型必須是 引用類型的,
如果是8中基本數據類型就不可以 原因如下,引用別人的一篇文章:
queue
linkedList 提供了方法支持隊列的行爲,並且實現queue的接口
offer():插入
peek(),element() 返回列頭。如果爲空,前者返回null,後者返回NoSuchElementException
poll(),remove() 刪除並返回隊頭。如果爲空,前者返回null,後者返回NoSuchElementException
Queue< Integer> q=new LinkedList<Integer>();
q.offer(1);
System.out.println(q.size());
System.out.println(q.peek());
System.out.println(q.poll());
System.out.println(q.size());
// System.out.println(q.remove());
System.out.println(q.element());
//priorityQueue
public class test {
private String name;
private int population;
public test(String name, int population)
{
this.name = name;
this.population = population;
}
public String getName()
{
return this.name;
}
public int getPopulation()
{
return this.population;
}
public String toString()
{
return getName() + " - " + getPopulation();
}
public static void main(String args[])
{
Comparator<test> OrderIsdn = new Comparator<test>(){
public int compare(test o1, test o2) {
// TODO Auto-generated method stub
int numbera = o1.getPopulation();
int numberb = o2.getPopulation();
if(numberb > numbera)
{
return 1;
}
else if(numberb<numbera)
{
return -1;
}
else
{
return 0;
}
}
};
Queue<test> priorityQueue = new PriorityQueue<test>(11,OrderIsdn);
test t1 = new test("t1",1);
test t3 = new test("t3",3);
test t2 = new test("t2",2);
test t4 = new test("t4",0);
priorityQueue.add(t1);
priorityQueue.add(t3);
priorityQueue.add(t2);
priorityQueue.add(t4);
System.out.println(priorityQueue.poll().toString());
}
}
//string 反轉序列
Queue<test> priorityQueue = new PriorityQueue<test>(11,Collections.reverseOrder());
//輸出最大數字
PriorityQueue<Integer> queue = new PriorityQueue<Integer>(A.length,new Comparator<Integer>(){
@Override
public int compare(java.lang.Integer o1, java.lang.Integer o2) {
// TODO Auto-generated method stub
return o2-o1;
}
});
//一 map 的遍歷方法
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapTest4
{
public static void main(String[] args)
{
HashMap map = new HashMap();
map.put("a","aa");
map.put("b","bb");
map.put("c","cc");
map.put("d","dd");
Set set = map.entrySet();
for(Iterator iter = set.iterator(); iter.hasNext();)
{
Map.Entry entry = (Map.Entry)iter.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
System.out.println(key +" :" + value);
}
}
}
//二
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class HashMapTest2
{
public static void main(String[] args)
{
HashMap map = new HashMap();
map.put("a","aaaa");
map.put("b","bbbb");
map.put("c","cccc");
map.put("d","dddd");
Set set = map.keySet();
for(Iterator iter = set.iterator(); iter.hasNext();)
{
String key = (String)iter.next();
String value = (String)map.get(key);
System.out.println(key+"===="+value);
}
}
}