复习 HashSet 方法:
- package com.java.collection;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- class People implements Comparable{
- String name;
- int id;
- People(String name,int id){
- this.name=name;
- this.id=id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + id;
- 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;
- People other = (People) obj;
- if (id != other.id)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- @Override
- public int compareTo(Object o) {
- People p=(People)o;
- /*if(this.id>p.id)
- return 1;
- if(this.id<p.id)
- return -1;
- if(this.id==p.id)
- return this.name.compareTo(p.name);
- return 0;*/
- int temp=this.id-p.id;
- return temp==0?this.name.compareTo(p.name):temp;
- }
- }
- public class HashSetTest {
- public People getPeople(String name,int id){
- return new People(name,id);
- }
- public static void main(String[] args){
- Set set=new HashSet();
- set.add(new People("隋宇航",160323));
- set.add(new People("陶磊",16032301));
- set.add(new People("隋宇航",160323));
- Iterator it=set.iterator();
- while(it.hasNext()){
- People person=(People)it.next();
- System.out.println(person.getId()+" "+person.getName());
- }
- }
- }
- /* 在使用 hashset 集合存储自定义的对象时, 需要重写自定义对象的 hashcode 和 equals 方法, 这样才能保证集合的唯一性.
- 如果没有重写 hashcode 方法和 equals 方法那么在比较的时候用的是 object 的 hashcode 和 equals 方法.
- linkedhashset 是有序的.
- */
复习 HashMap
- package com.java.collection;
- import java.util.HashMap;
- import java.util.Iterator;
- public class HashMapTest {
- public static void main(String[] args){
- HashMap<People,String> hm=new HashMap<People, String>();
- hm.put(new People("隋宇航",20),"公主岭");
- hm.put(new People("陶磊",20),"磐石");
- hm.put(new People("闫卿阁",20),"双阳");
- hm.put(new People("隋宇航",20),"四平");
- Iterator<People> it=hm.keySet().iterator();
- while(it.hasNext()){
- People p=it.next();
- System.out.println(p.getName()+""+p.getId()+" "+hm.get(p));
- }
- }
- }
- /*
- Map: 一次存储一对元素. collection 一次添加一个元素.
- map 也称为双列集合, collection 集合称为单列集合.
- map 集合中必须保证键的唯一性.
- 常用方法:
- 添加:
- value put(key,value), 返回前一个和 key 关联的值, 如果没有返回 null.
- 删除:
- void clear(); 清空 map 集合.
- value remove(key), 根据指定的 key 删除这个键值对.
- 判断:
- boolean containsKey(key);
- boolean containsValue(value);
- boolean isEmpty();
- 获取:
- value get(key); 通过键获取值, 如果没有该键返回 null.
- 当然可以通过返回 null, 来判断是否包含指定键.
- int size(); 获取键值的个数.
- keyset(): 取出 map 中的所有元素.
- 原理: 通过 keyset 方法获取 map 中所有的键所在的 set 集合, 在通过 set 的迭代器获取到每一个键, 在对每一个键获取其对应的值即可.
- entryset(): 通过 map 转成 set 就可以迭代了.
- 找到了另一个方法. entryset().
- 该方法将键和值的映射关系作为对象存储到了 set 集合中, 而这个映射关系的类型就是 map.Entry 类型.
- map 常用的子类:
- |--Hashtable: 内部结构是哈希表, 是同步的. 不允许 null 为键值.
- |--Properties: 用来存取键值对型的配置文件的信息. 可以和 io 技术相结合.
- |--HashMap: 内部结构是哈希表, 不是同步的. 允许 null 为键值.
- |--TreeMap: 内部结构是二叉树, 不是同步的. 可以对 map 集合中的键进行排序.
- LinkedHashMap: 有序, 存入和取出的顺序一样.
- */
复习 TreeSet:
- package com.java.collection;
- import java.util.Iterator;
- import java.util.TreeSet;
- public class TreeSetTest {
- public static void main(String[] args){
- HashSetTest h=new HashSetTest();
- TreeSet t=new TreeSet(new ComparatorTest());
- t.add(h.getPeople("陶磊",16032323));
- t.add(h.getPeople("隋宇航",16032322));
- t.add(h.getPeople("陈国海",16032301));
- Iterator it=t.iterator();
- while(it.hasNext()){
- People p=(People)it.next();
- System.out.println(p.getId()+" "+p.getName());
- }
- }
- }
- /*
- 可以对元素排序, 是不同步的.
- 判断元素唯一性的方式: 就是根据比较方法的返回结果是否是 0, 是 0, 就是相同元素, 不存. 9
- treeset 对元素排序的方法
- 方法一:
- 让元素自身具备比较功能, 元素需要实现 comparable 接口, 覆盖 compareto 方法. 实例: 按学号大小排序.
- 方法二:
- 如果不要按照对象中具备的自然顺序排序, 如果对象不具备自然顺序. 怎么办?
- 可以使用 treeset 集合的第二种排序方式: 让集合自身具备比较功能. 定义一个类实现 comparator 接口, 覆盖 compare 方法.
- 将该类对象作为参数传递给 treeset 的构造函数中.
- Map: 一次存储一对元素. collection 一次添加一个元素.
- map 也称为双列集合, collection 集合称为单列集合.
- */
复习 TreeSet 实现排序的方法 2:
- package com.java.collection;
- import java.util.Comparator;
- public class ComparatorTest implements Comparator<Object>{
- public int compare(Object o1, Object o2) {
- People p1=(People)o1;
- People p2=(People)o2;
- int temp=p1.getId()-p2.getId();
- int flag=p1.getName().compareTo(p2.getName());// 创建一个对象姓名排序对的比较器.
- return flag==0?temp:flag;
- }
- }
复习 TreeMap:
- package com.java.collection;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- public class TreeMapTest {
- public static void main(String[] args) {
- TreeMap<People,String> tm=new TreeMap<People, String>();
- tm.put(new People("隋宇航",25),"公主岭");
- tm.put(new People("陶磊",20),"磐石");
- tm.put(new People("闫卿阁",23),"双阳");
- tm.put(new People("隋宇航",25),"四平");
- Iterator<Map.Entry<People,String>> it=tm.entrySet().iterator();
- while(it.hasNext()){
- Map.Entry<People, String> me=it.next();
- People key=me.getKey();
- String value=me.getValue();
- System.out.println(key.getName()+""+key.getId()+" "+value);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2852000.html