Collection集合的特点:
集合中存储的所有元素都是单一元素,元素和元素之间没有必然的关系。因此我们把Collection集合也称为单列集合。
Map集合:
Map集合中保存的是一组(一对)数据。数据之间存在着一定的对应关系。Map集合就可以将数据的对应关系维护起来。当这样的数据存储在Map集合中,我们取出的时候,就可以根据这种对应关系获取到其中这组数据。
实现Map集合的集合有HashMap,TreeMap,LinkedHashMap,Hashtable;
HashMap:HashMap按键值插入的,无序的,键值相同会覆盖原来相同的,如果插入自定义对象,则在自定义对象的类中重写hashcold方法和equals方法;无法进行比较
TreeMap:插入元素后按从小到大拍,如果用户自定义排序算法,则重写compareTo()方法或compare()方法,
LinkedHashMap:能记住插入顺序,增删快,查询慢;
Hashtable:很老的;:
代码如下:
package com.Map集合自定义对象重写方法;
import java.util.Comparator;
public class Person implements Comparable<Person>{
private String name;
private Integer age;
public String getName() {
return name;
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public Person() {
super();
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public int compareTo(Person p) {
// TODO Auto-generated method stub
int number = this.age - p.age;
int number2 = number==0?this.name.length() - p.name.length():number;
int number3 = number2==0?this.name.compareTo(p.name):number2;
return number3;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package com.Map集合自定义对象重写方法;
import java.util.Comparator;
public class Mycompare implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
// TODO Auto-generated method stub
int namber=p1.getName().length()-p2.getName().length();
int namber1=namber==0?p1.getName().compareTo(p2.getName()):namber;
int namber2=namber==0?p1.getAge()-p2.getAge():namber1;
return namber2;
}
}
package com.Map集合自定义对象重写方法;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class HashMapDemo {
public static void main(String[] args) {
// HashMap<String,Integer> p=new HashMap<String,Integer>();
HashMap<Integer, Person> p = new HashMap<Integer, Person>();
p.put(1001, new Person("张三", 01));
p.put(1001, new Person("李四", 18));// 覆盖了第一个
p.put(1003, new Person("王五", 9));
p.put(1004, new Person("三八", 4));
Set<Integer> set = p.keySet();
for (Integer in : set) {
Person p1 = p.get(in);
System.out.println(in + "=" + p1);
}
// 自定义的对象做键值时,不会覆盖相同的键值,而是一并输出,确保唯一性,必须重写
System.out.println("=============");
TreeMap<Person, String> p1 = new TreeMap<Person, String>();
p1.put(new Person("张三", 17), "1001");
p1.put(new Person("李四", 28), "1002");
p1.put(new Person("王八", 25), "1003");
p1.put(new Person("三八", 19), "1004");
p1.put(new Person("三八", 19), "1004");
Set<Entry<Person, String>> entry = p1.entrySet();
for (Entry<Person, String> e : entry) {
Person ps = e.getKey();
String s = e.getValue();
System.out.println(ps + "=" + s);
}
System.out.println("====================");
TreeMap<Person, Integer> p2 = new TreeMap<Person, Integer>(
new Mycompare());
p2.put(new Person("张三", 17), 1001);
p2.put(new Person("李四", 19), 1001);
p2.put(new Person("王五", 11), 1001);
p2.put(new Person("三七", 29), 1001);
Set<Entry<Person, Integer>> en1 = p2.entrySet();
for (Entry<Person, Integer> e : en1) {
Person key1 = e.getKey();
Integer value = e.getValue();
System.out.println(key1 + "=" + value);
}
System.out.println("==========================");
TreeMap<Person, Character> p3 = new TreeMap<Person, Character>(
new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
// TODO Auto-generated method stub
int namber = p1.getAge() - p2.getAge();
int namber1 = namber == 0 ? p1.getName().length()
- p2.getName().length() : namber;
int namber2 = namber1 == 0 ? p1.getName().compareTo(
p2.getName()) : namber1;
return namber2;
}
});
p3.put(new Person("张三", 17), '女');
p3.put(new Person("李四", 19), '男');
p3.put(new Person("王五", 11), '女');
p3.put(new Person("三七", 29), '男');
Set<Person> set2 = p3.keySet();
for (Person p4 : set2) {
Character a = p3.get(p4);
System.out.println(p4 + "=" + a);
}
}
}
输出结果为:
1001=Person [name=李四, age=18]
1003=Person [name=王五, age=9]
1004=Person [name=三八, age=4]
=============
Person [name=张三, age=17]=1001
Person [name=三八, age=19]=1004
Person [name=王八, age=25]=1003
Person [name=李四, age=28]=1002
====================
Person [name=王五, age=11]=1001
Person [name=张三, age=17]=1001
Person [name=李四, age=19]=1001
Person [name=三七, age=29]=1001
==========================
Person [name=王五, age=11]=女
Person [name=张三, age=17]=女
Person [name=李四, age=19]=男
Person [name=三七, age=29]=男