Java基础_集合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java基础_集合相关的知识,希望对你有一定的参考价值。
1.定义罪犯Criminal类,height(身高)/weight(体重)/blood(血型)/home(籍贯)属性。
重写hashcode和equals,使用四个属性的组合进行实现。
创建HashSet集合,里面存放20个Criminal对象,其中O型血2人,A型血3人,B型血4人,AB型血1人,其余血型不详。
注意:hashcode()方法实现时,要求身高、体重、和血型三个属性合成一个数字,实现两两比较的高效算法。
2.Map:
-----------
key(键) - value(值) :kv对.
创建HashMap,Person为key,Dog为value。
存放100元素,遍历map集合,两种方式。EntrySet + KeySet.
删除操作。remove();
3.HashTable:线程安全的。
---------------
=================================================================
1.
package com.it18zhang11.job;
public class Criminal {
private int height;
private int weight;
private int blood;
private String home;
public Criminal(int height, int weight, int blood) {
if (height <= 0) {
System.out.println("身高不能为负数 ");
System.exit(-1);
} else if (weight <= 0) {
System.out.println("体重不能为负数 ");
System.exit(-1);
} else if (blood > 3 || blood < 0) {
System.out.println("血型,必须为0-3之间的值 ");
System.exit(-1);
} else {
this.height = height;
this.weight = weight;
this.blood = blood;
}
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
/**
* 重写hashcode方法
*/
@Override
public int hashCode() {
int i0 = 0 << 24;
int i1 = (ArrayTools.int2Bytes(height)[0] & 0xff) << 16;
int i2 = (ArrayTools.int2Bytes(weight)[0] & 0xff) << 8;
int i3 = (ArrayTools.int2Bytes(blood)[0] & 0xff) << 0;
int newResult = i0 | i1 | i2 | i3;
return home == null ? newResult : newResult + home.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
// 精准判断
if (obj.getClass() == Criminal.class) {
Criminal c = (Criminal) obj;
if (c.height == this.height && c.weight == this.weight
&& c.blood == this.blood) {
return true;
}
}
return false;
}
}
package com.it18zhang11.job;
public class ArrayTools {
/**
*
* 整数转换为字节数组 ;向右移位,然后强转截断。
*/
public static byte[] int2Bytes(int i) {
byte[] bytes = new byte[4];
bytes[0] = (byte) i;// 低位
bytes[1] = (byte) (i >> 8);// 次低位
bytes[2] = (byte) (i >> 16);// 次高位
bytes[3] = (byte) (i >> 24); // 高位
return bytes;
}
/**
*
* 字节数组 转 为整数 ,向左移位,由于向左移位,先转换为整数 ,缺的用符号位补齐,故用&0xFF去除
*/
public static int bytes2Int(byte[] bytes) {
int i0 = bytes[3] << 24;
int i1 = (bytes[2] & 0xFF) << 16;
int i2 = (bytes[1] & 0xFF) << 8;
int i3 = (bytes[0] & 0xFF) << 0;
return i0 | i1 | i2 | i3;
}
}
package com.it18zhang11.job;
import java.util.HashSet;
import java.util.Set;
public class HashSetTest {
public static void main(String[] args) {
Set<Criminal> cSet = new HashSet<Criminal>();
Criminal c1 = new Criminal(180, 170, 2);
Criminal c2 = new Criminal(170, 180, 0);
Criminal c3 = new Criminal(160, 180, 1);
Criminal c4 = new Criminal(150, 180, 3);
Criminal c5 = new Criminal(150, 180, 3);
cSet.add(c1);
cSet.add(c2);
cSet.add(c3);
cSet.add(c4);
cSet.add(c5);
System.out.println(c1.equals(c2));
System.out.println(c5.equals(c4));
for (Criminal c : cSet) {
out(c);
}
}
public static void out(Criminal c) {
String blood = "";
switch (c.getBlood()) {
case 0:
blood = "A";
break;
case 1:
blood = "B";
break;
case 2:
blood = "AB";
break;
case 3:
blood = "O";
break;
}
System.out.println("身高:" + c.getHeight() + "\t 体重: " + c.getWeight()
+ "\t" + "血型:" + blood);
}
}
2.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class HashMapTest {
public static void main(String[] args) {
Map<Person, Dog> map = new HashMap<Person, Dog>();
List<Person> keys = new ArrayList<Person>();
for (int i = 0; i < 100; i++) {
Person p = new Person("person" + i, i);
map.put(p, new Dog("dog" + i, i));
keys.add(p);
}
System.out.println("-----------keySet-------------");
for (Person p : map.keySet()) {
System.out.println(p.getName() + " " + map.get(p).getName());
}
System.out.println("-----------EntrySet-------------");
for (Entry<Person, Dog> entry : map.entrySet()) {
System.out.println(entry.getKey().getName() + " "
+ entry.getValue().getName());
}
System.out.println("-----------before remove-------------map.size : " + map.size());
for (Person p : keys) {
map.remove(p);
}
System.out.println("-----------after remove-------------map.size : " + map.size());
}
}
package com.it18zhang11.job;
/**
* hashcode + equlas
*/
public class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//
public int hashCode() {
// System.out.println(super.hashCode());
return name == null ?age : name.hashCode() + age ;
}
public boolean equals(Object obj) {
if(obj == null)
return false ;
if(obj == this)
return true ;
//精准判断
if(obj.getClass() == Person.class){
Person p = (Person)obj;
//name是否相同
boolean nameEqu = false ;
if(this.name == null){
if(p.name == null){
nameEqu =true ;
}
else{
nameEqu = false ;
}
}
//name 不null
else{
nameEqu = name.equals(p.name);
}
//age 是否相同
boolean ageEqu = (this.age == p.age) ;
//
return nameEqu && ageEqu ;
}
return false ;
}
}
package com.it18zhang11.job;
/**
*
*/
public class Dog {
private String name;
private int age;
public Dog() {
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class HashTableTest {
public static void main(String[] args) {
Map<Person, Dog> map = new Hashtable<Person, Dog>();
List<Person> keys = new ArrayList<Person>();
for (int i = 0; i < 100; i++) {
Person p = new Person("person" + i, i);
map.put(p, new Dog("dog" + i, i));
keys.add(p);
}
System.out.println("-----------keySet-------------");
for (Person p : map.keySet()) {
System.out.println(p.getName() + " " + map.get(p).getName());
}
System.out.println("-----------EntrySet-------------");
for (Entry<Person, Dog> entry : map.entrySet()) {
System.out.println(entry.getKey().getName() + " "
+ entry.getValue().getName());
}
System.out.println("-----------before remove-------------map.size : " + map.size());
for (Person p : keys) {
map.remove(p);
}
System.out.println("-----------after remove-------------map.size : " + map.size());
}
}
本文出自 “作业” 博客,请务必保留此出处http://10718270.blog.51cto.com/10708270/1782531
以上是关于Java基础_集合的主要内容,如果未能解决你的问题,请参考以下文章