class myException1 extends Exception{
private static final long serialVersionUID = 1240520315986594291L;
public myException1(){
System.out.printf("error\n");
}
public myException1(String str){
System.out.printf(str);
}
}
class myCount{
public double div(double i,double j)throws myException1{
if(j==0){
throw new myException1();
}
return i/j;
}
}
public class throwstest {
public static void main(String[] args)throws Exception {
// TODO 自动生成的方法存根
myCount test=new myCount();
try{
test.div(1, 0);
}catch(myException1 e){
System.out.printf("myException1");
}catch(Exception e){
System.out.printf(e.getMessage());
}
}
}
/*
output:
error
myException1
*/
collection集合判断子串包含
import java.util.*;
public class collectiontest {
public static void main(String[] args) {
Set<String> str1=new HashSet<String>();
Set<String> str2=new HashSet<String>();
str1.add("1");
str1.add("2");
str1.add("3");
str1.add("4");
str1.add("5");
str1.add("6");
str1.add("7");
str1.add("8");
str2.add("4");
str2.add("5");
str2.add("6");
if(str1.containsAll(str2)==true){
System.out.printf("same\n");
}
else{
System.out.printf("not same\n");
}
str2.add("9");
if(str1.containsAll(str2)==true){
System.out.printf("same\n");
}
else{
System.out.printf("not same\n");
}
}
}
/*
output:
same
not same
*/
hashset 实现类覆盖equals hashcode
import java.util.*;
class human{
private String name;
private int idCard;
public human(String name,int idCard){
this.name=name;
this.idCard=idCard;
}
public int hashCode(){
return idCard;
}
public boolean equals(Object obj){
if(obj==null){
return false;
}
if(obj instanceof human ==false){
return false;
}
human u=(human)obj ;
if(u.idCard()!=this.idCard){
return false;
}
System.out.println("had same Object");
return true;
}
public String name(){
return name;
}
public int idCard(){
return idCard;
}
public String toString(){
return "it is "+name+" ID is "+idCard;
}
}
public class test {
public static void main(String[] args) {
human hs3=new human("张三",3);
human hs1=new human("张三",1);
human hs2=new human("张三",1);
Set<human>set=new HashSet<human>();
set.add(hs1);
set.add(hs2);
set.add(hs3);
for(human hashsetuser:set){
System.out.println(hashsetuser);
}
}
}
/*
output:
had same Object
it is 张三 ID is 1
it is 张三 ID is 3
*/
treeset 实现类
import java.util.*;
class human implements Comparable<human>{
private String name;
private int idCard;
public human(String name,int idCard){
this.name=name;
this.idCard=idCard;
}
public int hashCode(){
return idCard;
}
public boolean equals(Object obj){
if(obj==null){
return false;
}
if(obj instanceof human ==false){
return false;
}
human u=(human)obj ;
if(u.idCard()!=this.idCard){
return false;
}
System.out.println("had same Object");
return true;
}
public String name(){
return name;
}
public int idCard(){
return idCard;
}
public String toString(){
return "it is "+name+" ID is "+idCard;
}
@Override
public int compareTo(human o) {
if(this.idCard>o.idCard){
return -1;
}else if(this.idCard<o.idCard){
return 1;
}
return 0;
}
}
public class test {
public static void main(String[] args) {
human hs1=new human("张三",3);
human hs2=new human("张三",1);
human hs3=new human("张三",2);
human hs4=new human("张三",4);
human hs5=new human("张三",1);
Set<human>set=new TreeSet<human>();
set.add(hs1);
set.add(hs2);
set.add(hs3);
set.add(hs4);
set.add(hs5);
for(human hashsetuser:set){
System.out.println(hashsetuser);
}
}
}
/*
output:
it is 张三 ID is 4
it is 张三 ID is 3
it is 张三 ID is 2
it is 张三 ID is 1
*/