1. 1-50的随机数 用集合实现
生成50个[10,50]之间的随机数,然后按数字从小到大顺序输出。(这次用Map做)
输出格式 数字:出现次数
1 //用ArraryList 集合来实现 2 3 public static void main(String[] args) { 4 ArrayList list =new ArrayList(); 5 6 for(int i=0;i<50;i++){ 7 list.add(i, 0); 8 } 9 10 11 for (int i=0;i<50;i++){12 Random rand =new Random();13 int randnum=rand.nextInt(40)+10;14 int index=(int)list.get(randnum);15 list.set(randnum, ++index);16 17 18 }19 20 for (int i=0;i
2. set根据什么集合判断重复,总结规则
Set判断是否重复是根据需要添加的类中的hashcode和equal方法确定的。
Object 类 的equals 比较的是内存地址 String类的 equals比较的是值 其他类可以通过API查询。只作这两个比较。/*举个例子,我们重写一下Animal 类的HashCode和equals方法就可以达到效果,只要Animal的名字相同,返回的hashcode就会相同,认定相等在这里我通过Car类和Animal类的作比较*/
1 // car类 2 public class Car implements Comparable{ 3 private String name ; 4 private int speed; 5 6 7 public Car(){} 8 public Car(String name ){ 9 this.name=name; 10 11 } 12 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getSpeed() { 23 return speed; 24 } 25 26 public void setSpeed(int speed) { 27 this.speed = speed; 28 } 29 30 public void getCar(String name,int speed){ 31 System.out.println("CAR NAME:"+name+" SPEED is "+speed); 32 }; 33 34 @Override 35 public int compareTo(Object o){ 36 Car car=(Car)o; 37 return this.name.compareTo(car.getName()); 38 } 39 @Override 40 public String toString() { 41 return "Car [name=" + name + ", speed=" + speed + "]"+"\n"; 42 } 43 44 //animal 类 45 public class Animal { 46 private String kind; 47 48 private String name; 49 50 public Animal(){ 51 52 } 53 54 public Animal(String kind, String name){ 55 56 this.kind = kind; 57 this.name = name; 58 59 } 60 61 public String getKind() { 62 return kind; 63 } 64 65 public void setKind(String kind) { 66 this.kind = kind; 67 } 68 69 public String getName() { 70 return name; 71 } 72 73 public void setName(String name) { 74 this.name = name; 75 } 76 77 @Override 78 public boolean equals(Object obj) { 79 Animal animal = (Animal)obj; 80 return this.name.equals(animal.getName()); 81 } 82 83 @Override 84 public int hashCode() { 85 return this.name.hashCode(); 86 } 87 88 @Override 89 public String toString() { 90 return this.name + ":" + this.kind; 91 } 92 93 94 } 95 96 97 } 98 //Test 类 99 100 public class HashTest {101 public static void main(String[] args) {102 103 HashSet set = new HashSet(); 104 105 Car car1 = new Car("宝马");106 Car car2 = new Car("玛莎拉蒂");107 Car car3 = new Car("宝马");108 109 Animal an1=new Animal("cat","cat");110 Animal an2=new Animal("dog","dog");111 Animal an3=new Animal("tiger","cat");112 113 set.add(car1);114 set.add(car2);115 set.add(car3);116 set.add(an1);117 set.add(an2);118 set.add(an3);119 System.out.println("-----Car类,没有重写equals和hashcode方法------");120 System.out.println("car1: "+car1.hashCode());121 System.out.println("car2: "+car2.hashCode());122 System.out.println("car3: "+car3.hashCode());123 System.out.println("----animal类,重写了equals和hascode方法-------");124 125 System.out.println("an1: "+an1.hashCode());126 System.out.println("an2: "+an2.hashCode());127 System.out.println("an3: "+an3.hashCode());128 129 130 }131 }
PS:可以看到 重写了equals方法后,只要名字相同,hashcode得出来的就相同。