Set,Map,包装类工具类

package Leidejihe;
import java.util.*;

public class Test2set {

    public static void main(String[] args) {
        //两种方式。Set没有顺序输入和输出
        Set<String>s=new HashSet<String>();
        //HashSet<String>s1=new HashSet<String>();

        s.add("a");
        s.add("b");
        s.add("c");
        s.add("d");
        s.add("e");
        s.add("f");
        s.add("g");
    
        //s.add(null);
        //遍历方式
            //foreach
        for(String i:s)
        {
            System.out.print(i+" ");
        }
        
        System.out.println();
        //清除,remove(清除内容)
        if(s.remove("d"))
        {
            System.out.println("移除成功");
        }
        else
        {
            System.out.println("移除失败");
        }
        System.out.println("s的长度变为"+s.size());
        
            //迭代器
        
        Iterator<String>it=s.iterator();
        while(it.hasNext())
        {
            //移除c
            String t=it.next();
            if(t.equals("c"))
            {
                it.remove();
            } 
            else
            {
                System.out.print(t+" ");
            }    
            
        }
        System.out.println();
        System.out.println("s的长度"+s.size());
        
        //HashSet无序集合
        //TreeSet排序的无序集合
        
        Set<String>s2=new TreeSet<String>();
        //HashSet<String>s1=new HashSet<String>();

        s2.add("a");
        s2.add("b");
        s2.add("f");
        s2.add("d");
        s2.add("e");
        s2.add("c");
        s2.add("g");
        s2.add(null);
        for(String t:s2)
        {
            System.out.print(t+" ");
        }
        
    }

}
View Code
package Leidejihe;
import java.util.*;

public class Test3map {
    public static void main(String args[]){

        Map<String,String> m=new HashMap<String,String>();
        //put(key k,value v)
        m.put("0533", "淄博");
        m.put("0536", "莱芜");
        m.put("0537", "聊城");
        m.put("0531", "济南");
        m.put("0532", "青岛");
        m.put("0535", "烟台");
        m.put("0553", "德州");
        m.put("0541", "东营");
        m.put("0546", "滨州");
        m.put("0543", "日照");
        m.put("0542", "威海");
        m.put("0539", "潍坊");
//        System.out.println("长度:"+m.size());
//        
////        m.put("0532", "黄岛");
//        m.put("0534","青岛");
//        m.put(null,null);
//        System.out.println("长度:"+m.size());//放不进去,key不能重复
//    
//        
//        //根据key获取value
//        System.out.println("0533="+m.get("0533"));
//    
//        
//        
//        
//        //返回bool值,判断是不是存在key
//        System.out.println(m.containsKey("0533"));
//        if(m.containsKey("0533"))
//        {
//            System.out.println("0533已经存在");
//        }
//        //返回bool值,判断是不是存在value
//        System.out.println(m.containsValue("淄博"));
//        if(m.containsValue("淄博"))
//        {
//            System.out.println("淄博已经存在");
//        }
        
        
        
        
        //遍历
        
        for(String k:m.keySet())
        {
            System.out.println(k+"="+m.get(k));
        }
        
        System.out.println();
        
        
        
        
        //TreeMap排序
        Map<String,String> m1=new TreeMap<String,String>();
        
    
        m1.put("0533", "淄博");
        m1.put("0536", "莱芜");
        m1.put("0537", "聊城");
        m1.put("0531", "济南");
        m1.put("0532", "青岛");
        m1.put("0535", "烟台");
        m1.put("0553", "德州");
        m1.put("0541", "东营");
        m1.put("0546", "滨州");
        m1.put("0543", "日照");
        m1.put("0542", "威海");
        m1.put("0539", "潍坊");
        
        for(String k2:m1.keySet())
        {
            System.out.println(k2+"="+m1.get(k2));
        }
        
        
        
    
    
    
    }
}
View Code
package Leidejihe;

import java.util.Random;

public class Baozhuanglei {

    public static void main(String[] args) {
        
        
        //包装类Long型
        Long l=new Long(100);
        
        //用包装类把字符串转成数值
        
        Long l1=new Long("1000");
//        
//        String str=1000+"";//把1000转成字符串的
//        System.out.println(str);
        System.out.println(l1);
        //从包装类转成基本数据类型
        Long l2=l1.longValue();
        System.out.println("l2="+l2);
        
        
        Long l3=Long.parseLong("12999");
        
        System.out.println("直接转:Long.parseLong()"+l3);
        
        
        //int型
        Integer i=new Integer("1000");
        
        Integer.parseInt("100");
        
        
        //float
        
        Float f=new Float("123.45");
        Float.parseFloat("123.456");
        
        //Double
        Double d=new Double("1234.567");
        Double.parseDouble("123456.78");
        
        //boolean型
        Boolean b=new Boolean("aaaaa");
        System.out.println(b.booleanValue());
        
        Boolean b1=new Boolean("true");
        System.out.println(b1);
        
        
        //数学工具类。Math不能实例化,全是静态的方法
        System.out.println(Math.PI);
            
            //四舍五入
            Math.round(1234.56);
            System.out.println(Math.round(1234.56));
            double d1=12.345678;
            System.out.println("d1="+d1);
            System.out.println("d1四舍五入:"+Math.round(d1));
            System.out.println("d1保留两位小数:"+Math.round(d1*100)/100.0);//转成double型
            
            //去小数点取整:下限值,上限值。返回double型
            System.out.println("舍去小数点,下限值:"+Math.floor(d1));
            System.out.println("舍去小数点,上限值:"+Math.ceil(d1));
            
        //随机数Random。
            //random只产生0-1之间的数,要取0-100的数,先乘以100
            System.out.println("第一次随机:"+Math.random());
            System.out.println("第二次随机:"+Math.random());
        
            Random r=new Random();
            
            //随机数种子
            //伪随机数
            //根据种子计算出来的
            //由种子决定随机数的产生序列
            //默认时间做种子,没刷新一次,随机数更新一次
            //r=new Random(10);//每次产生的随机数一样
            System.out.println("范围内的随机数:");
            for(int j=0;j<1;j++)
            {
                System.out.println(r.nextInt(10));
            }
View Code

原文地址:https://www.cnblogs.com/1ming/p/5267206.html