日付、ハッシュ、文字列クラス、コレクションは、Comparableを書き換えます

Dateクラス

public class 日期 {
	public static void main(String[] args) throws Exception{
		
		//获取当前毫秒
		long date =System.currentTimeMillis();
		SimpleDateFormat simple =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");//格式化时间
		Date d =new Date();
		
		//date-->String
		String temp =simple.format(d);
		System.out.println(temp);
		
		//String -->date
		String da ="2019-05-15 12:32:34 333";
		Date dad =simple.parse(da);
		System.out.println(dad);
		
		Date m =new Date(System.currentTimeMillis()-1000*60*10);
		System.out.println(simple.format(m));
		System.out.println(simple.format(new Date()));

		Calendar c =Calendar.getInstance();//查看当前日历
		int i =c.get(Calendar.DAY_OF_WEEK);//常量是类名.
		System.out.println(i);//查看是第几天
		
		String ste ="2008-08-08";
		//Date kk =new SimpleDateFormat("yyyy-MM-dd".parse(ste));
		//c.settime(ste)设置的是ste中的日历
		
		//DecimalForamt数字格式化
		//创建数字格式化对象
		//需求:加入千分位
		DecimalFormat dec =new DecimalFormat("###,###");
	
		//开始格式化
		//Number-->String
		System.out.println(dec.format(1234567));//1,234,567
		
		//需求加入千分位,保留两位小数
		DecimalFormat dec1 =new DecimalFormat("###,###");
		System.out.println(dec1.format(1234567.123));
		
		//需求:加入千分位,保留4位小数,不够补0
		DecimalFormat dec2 =new DecimalFormat("###,###.0000");
		System.out.println(dec2.format(1234567.123));
	}
}

ハッシュ・クラス

package 方法;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.SimpleFormatter;

public class 哈希 {
	public static void main(String[] args) throws Exception  {
		Set s =new HashSet();
		gong g =new gong("100","jaf");
		gong g1 =new gong("101","jaf");
		gong g2 =new gong("200","jxa");
		
		s.add(g);
		s.add(g1);
		s.add(g2);
		System.out.println(g.hashCode());//hashCode
		System.out.println(s.size());
		//SortedSet无序不可重复,可以将存入的元素自动排序
		SortedSet ss =new TreeSet();
		ss.add(10);
		ss.add(3);
		ss.add(7);
		ss.add(72);
		Iterator it =ss.iterator();
		while(it.hasNext()) {
			Object o =it.next();
			System.out.println(o);
		String str ="2008-09-11";
		SimpleDateFormat d =new SimpleDateFormat("yyyy-MM-dd");
		Date da =(Date) d.parse(str);
		SortedSet times =new TreeSet();
		times.add(da);
		Iterator iti =times.iterator();
		while(it.hasNext()) {
			Object p =iti.next();
			if(o instanceof Date) {
				Date da1=(Date)p;
				System.out.println(d.format(da));
			}
			}
		}
	}
}
class gong{
	String no;
	String name;
	gong(String no,String name){
		this.name=name;
		this.no =no;
	}
	public boolean equals(Object o) {
		if(this==o) { 
			return true;
		}
		if(o instanceof gong) {
			gong e=(gong)o;
			if(e.no.equals(this.no)&&e.name.equals(name)) {
				return true;
			}
		}
		return false;
	}
	public int hashCode() {
		return no.hashCode();
	}
}

Stringクラス

	public static void main(String[] args) {
		
		//一共创建了三个对象
		//常量池中一个,堆中两个
		byte [] bytes = {97,98,99,100};
		String a =new String(bytes);
		String b =new String(bytes,0,2);//0是第0位上的字符,1是字符的长度
		char[] c = {'我','是','中','国','人'};
		String c1 =new String(c);
		String d =new String(c,2,2);
		System.out.println(d);
		//boolean endsWith(String endstr)
		System.out.println("helloworld,java".endsWith("java"));
		//equalsIgnoreCase比较两个字符串是否相等,忽略大小写
		//byte getBytes将字符串转化为byte值
		byte [] m = "abcd".getBytes();
		for(int i=0; i<m.length; i++) {
			System.out.print(m[i]);
		}
		System.out.println();
		//indexOf返回字符串第一次出现的索引
		System.out.println("ajvaejidjngh.ds".indexOf("ae",3));
		System.out.println("jfincnx".lastIndexOf("in"));
		
		//将字符串用另一个字符串代替
		System.out.println("cmieanij".replace("ea", "yy"));
		
		//用split将字符串分割
		String time ="yan,wei,shuai";
		String [] my =time.split(",");
		for(int i=0; i<my.length; i++) {
			System.out.println(my[i]);
		}
		
		//boolean startWith 判断字符串的开头是否为j
		System.out.println("jancaienkg".startsWith("j"));
		
		//substring截取字符串,将之后的截取出来
		System.out.println("hjnca/ennc".substring(5));
		System.out.println("hjnca/ennc".substring(1,4));
		
		//toCharArray将字符串转化为数组
		char[] m2 ="我是闫伟".toCharArray();
		for(int i=0; i<m2.length; i++) {
			System.out.println(m2[i]);
		}
		
		//转化为大写
		System.out.println("javac".toUpperCase());
		//转化为小写	
		System.out.println("javac".toLowerCase());
		
		//忽略前导和后导空白,中间的去不了
		System.out.println("  d ss gda   ".trim());
	}

セット

package 方法;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

public class 集合 {
	/*
	 * booolean add(Object element)向集合中添加元素
	 * void clear()清空集合
	 * boolean isRmpty();判断集合中是否有元素
	 * Iterator<E>iterator();获取集合中所依赖的迭代器对象
	 * boolean remove(Object o)删除集合中某个元素
	 * int size();获取集合中元素的个数
	 * object[]	toArray();将集合转换成数组
	 */
	public static void main(String[] args) {
		Collection c =new LinkedList() ;
		c.add(100);
		c.add(false);
		c.add(3);
		//Iterator it =c.iterator();
		//while(it.hasNext()) {
			//Object o =it.next();
			//System.out.println(o);
		 /*
		 * boolean b=it.hasNext;判断是否有更多的内容,如果有返回true
		 * Object o =it.next();将迭代器向下移动一位,并取出指向元素
		 * 调用it.next之前必须调用it.hasnext();
		 */
		for(Iterator it =c.iterator();it.hasNext();) {
			Object o=it.next();
			System.out.println(o);
		}
		/*
		 * boolean contains;判断集合中是否含有某一个元素
		 * boolean remove();删除集合中的某个元素
		 */
		Collection d =new ArrayList();
		Integer t =new Integer(10);
		d.add(t);
		//contains方法底层调用的是equals方法, 
		System.out.println(d.contains(t));
	
		Manger m1 =new Manger("java",10);
		Manger m2 =new Manger("java",10);
		d.add(m1);
		System.out.println(d.contains(m2));
		//remove都需要调用底层的equals方法
		d.remove(m2);
		System.out.println(d.size());
	}
}
class Manger{
	String name;
	int no;
	Manger(String name,int no){
		this.name =name;
		this.no =no;
	}
	//重写方法
	public boolean equals(Object o) {
		if(this==o)	return true;
		if(o instanceof Manger) {
			Manger m =(Manger)o;
			if(m.no==this.no&&m.name.equals(this.name)) {
				return true ;
			}
		}
		return false;
	}
}

同等の書き換え方法

	public static void main(String[] args) {
		
		SortedMap products =new TreeMap();
		
		Product p =new Product("西瓜",100);
		Product p1 =new Product("苹果",32);
		Product p2 =new Product("柿子",52);
		Product p3 =new Product("香蕉",62);
		
		products.put(p, 1);
		products.put(p1, 93);
		products.put(p2, 81);
		products.put(p3, 3);
		
		//将value自动排序
		Set keys =products.keySet();
		Iterator it =keys.iterator();
		while(it.hasNext()) {
			Object o =it.next();
			Object c =products.get(o);
			System.out.println(o+" "+c);
		}
}
}

class Product implements Comparable{
	String name;
	double price;
	Product(String name,double price){
		this.name=name;
		this.price=price;
	}
	public String toString() {
		return "Product[name="+name+",price="+price+"]";
	}
	
	public int compareTo(Object o) {
		double price1 =this.price;
		double price2 =((Product)o).price;
		if(price1>price2) {
			return 1;
		}else if(price<price2) {
			return -1;
		}else {
			return 0;
		}
	}
リリース8元の記事 ウォンの賞賛0 ビュー18

おすすめ

転載: blog.csdn.net/richpersion/article/details/105125567