public class Person {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(12, "aaa");
map.put(22, "df");
map.put(72, "awe");
map.put(32, "34");
map.put(52, "fhd");
// Set<Integer> mapSet = map.keySet(); //获取包含所有key的Set容器
// Iterator<Integer> it = mapSet.iterator();
// while(it.hasNext())
// {
// Integer x = it.next();
// System.out.println(x + " : " + map.get(x));
// }
// Collection<String> coll = map.values(); //获取包含所有value的Collection容器
// Iterator<String> it = coll.iterator();
// while(it.hasNext())
// {
// System.out.println(it.next());
// }
Set<Map.Entry<Integer, String>> mapEntries = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = mapEntries.iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> mEntry = it.next();
System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
}
}
}
class Car
{
private String name;
private int number;
private double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Car(String name, int number, double weight) {
this.name = name;
this.number = number;
this.weight = weight;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + number;
long temp;
temp = Double.doubleToLongBits(weight);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Car other = (Car) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (number != other.number)
return false;
if (Double.doubleToLongBits(weight) != Double
.doubleToLongBits(other.weight))
return false;
return true;
}
}
public class Person {
public static void main(String[] args) {
Map<Car, String> map = new Hashtable<Car, String>();
map.put(new Car("aaa", 111,1.02), "abc");
map.put(new Car("bbb", 222,1.02), "bcd");
map.put(new Car("ccc", 333,1.02), "cde");
map.put(new Car("aaa", 111,1.02), "qqq");
map.put(new Car("bbb", 234,1.02), "bcd");
Car car = new Car("zzz", 999,1.01);
map.put(car, "iii");
Set<Map.Entry<Car, String>> mapEntries = map.entrySet();
Iterator<Map.Entry<Car, String>> it = mapEntries.iterator();
while(it.hasNext())
{
Map.Entry<Car, String> mEntry = it.next();
System.out.println(mEntry.getKey().getName() + " : " + mEntry.getKey().getNumber() + " :" + mEntry.getValue());
}
System.out.println(map.containsKey(new Car("aaa", 111,1.02)));
System.out.println(map.containsKey(car));
}
}
class Car
{
private String name;
private int number;
private double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Car(String name, int number, double weight) {
this.name = name;
this.number = number;
this.weight = weight;
}
}
public class Person {
public static void main(String[] args) {
Map<Car, String> map = new TreeMap<Car, String>();
map.put(new Car("aaa", 111,1.02), "abc");
map.put(new Car("bbb", 222,1.02), "bcd");
map.put(new Car("ccc", 333,1.02), "cde");
map.put(new Car("aaa", 111,1.02), "qqq");
map.put(new Car("bbb", 234,1.02), "bcd");
Set<Map.Entry<Car, String>> mapEntries = map.entrySet();
Iterator<Map.Entry<Car, String>> it = mapEntries.iterator();
while(it.hasNext())
{
Map.Entry<Car, String> mEntry = it.next();
System.out.println(mEntry.getKey().getName() + " : " + mEntry.getKey().getNumber() + " :" + mEntry.getValue());
}
}
}
class Car implements Comparable
{
private String name;
private int number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Car(String name, int number) {
this.name = name;
this.number = number;
}
@Override
public int compareTo(Object o) {
if(!(o instanceof Car))
{
throw new ClassCastException("类型错误!");
}
Car c = (Car)o;
int temp = this.getName().compareTo(c.getName());
return temp == 0 ? this.getNumber() - c.getNumber() : temp;
}
}
public class Person {
public static void main(String[] args) {
//对于TreeMap容器,只需要实现对象中的比较方法compareTo(),其内部是通过二叉树实现的,
//当查找元素时,不会去调用hashCode()和equals(Object obj)方法。
Map<Car, String> map = new TreeMap<Car, String>();
map.put(new Car("aaa", 111), "abc");
map.put(new Car("bbb", 222), "bcd");
map.put(new Car("ccc", 333), "cde");
map.put(new Car("aaa", 111), "qqq");
map.put(new Car("bbb", 234), "bcd");
Set<Map.Entry<Car, String>> mapEntries = map.entrySet();
Iterator<Map.Entry<Car, String>> it = mapEntries.iterator();
while(it.hasNext())
{
Map.Entry<Car, String> mEntry = it.next();
System.out.println(mEntry.getKey().getName() + " : " + mEntry.getKey().getNumber() + " :" + mEntry.getValue());
}
System.out.println(map.containsKey(new Car("aaa", 111)));
}
}