list.set.map 补充说明

来源:互联网 发布:淘宝店铺运营助手在哪 编辑:程序博客网 时间:2024/06/17 10:22
set
//使用散列算法
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);
  }
 }
}

原创粉丝点击