Person类:
package com.yfs.javase; import java.util.Date; public class Person implements Comparable { private String name; private int age; private char sex; public static Date date = new Date(); @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + sex; 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 != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (sex != other.sex) return false; return true; } public Person() { } public Person (String n) { this.name = n; } 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 char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public Person(String name, int age, char sex) { super(); this.name = name; this.age = age; this.sex = sex; } @Override public int compareTo(Object o) { Person p = (Person)o; return p.age - age; } }
1.set
package com.yfs.javase; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; public class Demo1 { /** * Set */ public static void main(String[] args) { // LinkedHashSet lSet = null;//增加 删除 // HashSet hSet = null;//查找 // TreeSet tSet = null;//排序 // useLinkedSet(); //useTreeSet(); useArrayList(); } public static void useArrayList() { ArrayList list = new ArrayList();//必须覆盖equals方法 Person p = new Person("张三",20,'男'); list.add(p); list.add(new Person("李四",23,'女')); list.add(new Person("王五",22,'男')); list.add(new Person("王Z",21,'女')); list.add(p); System.out.println("是否有张三:" + list.contains(p));//指向同一对象 System.out.println("是否有张三:" + list.contains(new Person("张三",20,'男'))); System.out.println("元素:" + list); } public static void useTreeSet() { TreeSet tSet = new TreeSet(); tSet.add("fff"); tSet.add("ccc"); tSet.add("aaa"); tSet.add("ddd"); tSet.add("aaa"); System.out.println("元素个数: " + tSet.size()); System.out.println("元素: " + tSet); } public static void useLinkedSet() { LinkedHashSet lSet = new LinkedHashSet(); lSet.add("fff"); lSet.add("ccc"); lSet.add("aaa"); lSet.add("ddd"); lSet.add("aaa"); System.out.println("元素个数: " + lSet.size()); System.out.println("元素: " + lSet); } }
2.Mycompare类
package com.yfs.javase; import java.util.Comparator; //外部比较器 public class Mycompare implements Comparator { @Override public int compare(Object o1, Object o2) { Person p1 = (Person)o1; Person p2 = (Person)o2; return p1.getAge() - p2.getAge();//使用年龄排序 } }
使用比较器:
package com.yema.javase; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; public class Demo2 { /** * Set */ public static void main(String[] args) { //useLinkedSet(); useTreeSet(); // useArrayList(); } public static void useArrayList() { ArrayList list = new ArrayList();//必须覆盖equals方法 Person p = new Person("张三",20,'男'); list.add(p); list.add(new Person("李四",23,'女')); list.add(new Person("王五",22,'男')); list.add(new Person("王Z",21,'女')); list.add(p); System.out.println("是否有张三:" + list.contains(p));//指向同一对象 System.out.println("是否有张三:" + list.contains(new Person("张三",20,'男'))); System.out.println("元素:" + list); } public static void useTreeSet() { // Mycompare com = new Mycompare();//使用比较器 // TreeSet tSet = new TreeSet(com);//必须覆盖hashcode equals方法 TreeSet tSet = new TreeSet();//必须覆盖hashcode equals方法 Person p = new Person("张三",20,'男'); tSet.add(p); tSet.add(new Person("李四",23,'女')); tSet.add(new Person("王五",22,'男')); tSet.add(new Person("王Z",21,'女')); tSet.add(p); System.out.println("是否有张三:" + tSet.contains(p));//指向同一对象 System.out.println("是否有张三:" + tSet.contains(new Person("张三",20,'男'))); System.out.println("元素:" + tSet); } public static void useLinkedSet() { LinkedHashSet lSet = new LinkedHashSet();//必须覆盖hashcode equals方法 Person p = new Person("张三",20,'男'); lSet.add(p); lSet.add(new Person("李四",23,'女')); lSet.add(new Person("王五",22,'男')); lSet.add(new Person("王Z",21,'女')); lSet.add(p); System.out.println("是否有张三:" + lSet.contains(p));//指向同一对象 System.out.println("是否有张三:" + lSet.contains(new Person("张三",20,'男'))); System.out.println("元素:" + lSet); } }
3.泛型
package com.yfs.javase; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Random; public class Demo3 { /** * 泛型 */ public static void main(String[] args) { // useArrayList(); useCollection(); } private static void useCollection() { // 指定集合放入对象类型 Collection<Person> col = new HashSet<Person>(); col.add(new Person("张三", 20, '男')); col.add(new Person("李四", 23, '女')); col.add(new Person("王五", 22, '男')); col.add(new Person("王Z", 21, '女')); // list.add(new Random()); 其他类型对象不能放入 System.out.println("元素:" + col); // Person p = (Person)list.get(4); // Object obj = list.get(4); // 取值不考虑类型 迭代器泛型 Iterator<Person> it = col.iterator(); while (it.hasNext()) { Person p = it.next(); System.out.println(p); } } public static void useArrayList() { // 指定集合放入对象类型 ArrayList<Person> list = new ArrayList<Person>(); list.add(new Person("张三", 20, '男')); list.add(new Person("李四", 23, '女')); list.add(new Person("王五", 22, '男')); list.add(new Person("王Z", 21, '女')); // list.add(new Random()); 其他类型对象不能放入 System.out.println("元素:" + list); // Person p = (Person)list.get(4); // Object obj = list.get(4); Person p = list.get(2);// 取值不考虑类型 } }
4.HashMap
package com.yfs.javase; import java.util.HashMap; import java.util.Map; import java.util.Random; public class MapDemo1 { /** * @param args */ public static void main(String[] args) { Map map = new HashMap(); map.put(1, new Person());//put添加对象key value map.put(2, new Random()); map.put("zhangsan", "1388888888"); map.put("lisi", "139999999"); map.put("zhangsan", "1300000000"); System.out.println("元素个数 : " + map.size()); System.out.println("元素: " + map); System.out.println(map.get(1)); System.out.println(map.get("lisi")); System.out.println("是否有zhangsan key :" + map.containsKey("zhangsan")); System.out.println("是否有139999999号码:" + map.containsValue("139999999")); } }
5.Map(Iterator)实体、key的集合
package com.yfs.javase; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class MapDemo2 { /** * @param args */ public static void main(String[] args) { //useMap(); //useMapEntry(); //Date d = Person.date; useKey(); } public static void useMapEntry() { Map<String,String> map = new HashMap<String,String>(); map.put("zhangsan", "1388888888"); map.put("lisi", "139999999"); map.put("zhang", "1300000000"); map.put("wangwu", "1311111111"); map.put("Tom", "1583333333333"); map.put("Jack", "133333333333"); Set<Map.Entry<String, String>> set = map.entrySet();//===================不同 for(Iterator<Map.Entry<String, String>> it = set.iterator(); it.hasNext();){ Map.Entry<String, String> entry = it.next(); String key = entry.getKey(); String value= entry.getValue(); System.out.println(key + " === > " + value); } } public static void useKey() { Map<String,String> map = new HashMap<String,String>(); map.put("zhangsan", "1388888888"); map.put("lisi", "139999999"); map.put("zhang", "1300000000"); map.put("wangwu", "1311111111"); map.put("Tom", "1583333333333"); map.put("Jack", "133333333333"); System.out.println("元素个数 : " + map.size()); System.out.println("元素: " + map); //所有key 集合 System.out.println("==========key + value 集合=========="); Set set = map.keySet();//========================================不同 for(Iterator<String> it = set.iterator(); it.hasNext();){ String key = it.next();//keyjihe System.out.print(key + " +++ "); String value = map.get(key);// mapjihe System.out.print(value + " "); } } }