练习使用工具类Collections

来源:互联网 发布:纪梵希散粉 知乎 编辑:程序博客网 时间:2024/05/29 02:13
import java.util.*;

public class TestCollections{
public static void main(String[] args)
{
    CollcetionsTest ct=new CollcetionsTest();
    ct.printMap();
    }
}


class CollcetionsTest{
    HashMap hash=new HashMap();
    public void printMap()
    {
        hash.put("1","value1");
        hash.put("2","value2");
        hash.put("3","value3");
        hash.put("4","value4");

        List valueList=new ArrayList(hash.values());
        for(int i=0;i<valueList.size();i++)
        {
            String temp=(String)valueList.get(i);
            System.out.println("value"+i+"="+temp);  //the sequence is not sure
            }
/*wrong useage    coz list for binarySearch must be sorted(ascending or descending) first*/
        int targetPosition=Collections.binarySearch(valueList,"value3");
        System.out.println("$$$$$$position of value3="+targetPosition);
        targetPosition=Collections.binarySearch(valueList,"value1");
        System.out.println("$$$$$$position of value1="+targetPosition);
/* wrong usage*/

        ArrayList testList=(ArrayList)valueList;
        for(int i=0;i<testList.size();i++)
        {
            String temp=(String)testList.get(i);
            System.out.println("********testList value"+i+"="+temp);  //the sequence is not sure
            }




            Collections.sort(valueList);
        for(int i=0;i<valueList.size();i++)
        {
            String temp=(String)valueList.get(i);
            System.out.println("^^^^^value"+i+"="+temp);  //the sequence is not sure
            }

            System.out.println("maxvalue"+"="+Collections.max(valueList));
            System.out.println("minvalue"+"="+Collections.min(valueList));

        Set set=hash.keySet();
        System.out.println("set size="+set.size());
            System.out.println("maxvalue"+"="+Collections.max(set));
            System.out.println("minvalue"+"="+Collections.min(set));

        targetPosition=Collections.binarySearch(valueList,"value3");
        System.out.println("$$$$$$position of value3="+targetPosition);
        targetPosition=Collections.binarySearch(valueList,"value1");
        System.out.println("$$$$$$position of value1="+targetPosition);


        HashMap hash0=new HashMap();
        Set set0=hash0.keySet();
        System.out.println("^^^^^^^^^set0="+set0.size());
        if(!set0.isEmpty()){
       int maxKey = ((Integer) Collections.max(set0)).intValue();
        }
System.out.println("set0="+set0+"=========="+hash0.keySet());//This means: even if
//there is no value in hash0, hash0.keySet() will not return "null" ,
//that means set0 is not null, though set0 has no value.
//At this time , set0.isEmpty==true while set0.size()==0.


HashSet hset=new HashSet(set);
        System.out.println("~~~hset size="+hset.size());
            System.out.println("~~~hset maxvalue"+"="+Collections.max(hset));
            System.out.println("~~~hset minvalue"+"="+Collections.min(hset));


    }
    }
原创粉丝点击