java从入门到弃坑第十一天

来源:互联网 发布:东方闻樱 知乎 编辑:程序博客网 时间:2024/06/06 03:54

1.HashSet:元素唯一,但是无序,他不保证set的迭代顺序,也不保证顺序的永恒不变。

             注:如何保证元素的唯一性?在add源码的方法中

                    if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {...}

                    左边:e.hash==hash比较对象的哈希码值

                    中间:(k = e.key) == key 比较对象的地址值

                    右边:key.equals(k)比较对象的内容是否相同。

                    所以hashset中判断元素是否唯一主要依赖hashcode和equals方法。判断是,首先判断哈希值是否相同,如果不同直接添加元素,如果相同,在判断对象的equals方法,不同再添加,相同就说明元素重复,不做添加。所以要用hashset作集合时,要重写hash code和equals方法,在java类中自动生成。

exp:存储String类型的元素,说明元素无序且唯一

import java.util.HashSet;public class hashDemo {public static void main(String[] args) {String s1="a";String s2="b";String s3="a";HashSet<String> h=new HashSet<String>();h.add(s1);h.add(s2);h.add(s3);for (String s : h) {System.out.println(s);}}}//输出b a或 a b

2.TreeSet:根据元素的自然顺序对元素进行排序,或根据创建set时提供的comparator进行排序。

     二叉树:

 exp:创建学生类对象,用treeset集合储存并遍历,学生对象为标准java类,包含tostring方法的重写

import java.util.Comparator;import java.util.TreeSet;public class StudentTest {public static void main(String[] args) {Student s1=new Student("a",1);Student s2=new Student("b",2);Student s3=new Student("c",3);Student s4=new Student("a",1);TreeSet<Student> t= new TreeSet<Student>(new Comparator<Student>() {        //匿名内部类传参。public int compare(Student o1, Student o2) {//自己规定比较规则。int num1=o1.getAge()-o2.getAge();int num2=num1==0?o1.getName().compareTo(o2.getName()):num1;return num2;}});t.add(s1);t.add(s2);t.add(s3);t.add(s4);for (Student s : t) {System.out.println(s);}}}//Student [name=a, age=1] //Student [name=b, age=2] //Student [name=c, age=3]有结果知,重复元素被删除,并按照以重写规则进行排序。


3.Set的遍历:两种方法,迭代器或者增强for循环。

4.hashset与treeset的不同:A:底层存储的数据结构不同。分别为hashmap和treemap。

                                             B:存储时保证数据唯一性的依据不同。前者时hashcode和equals方法,后者是     

                                                   Compareable接口的compareTo()方法。

                                             C:有序性不同,hashset无序,treeset有序。

5.Map:将键映射到值的对象,一个映射不能包含重复的键,每个键最多映射一个值。

   方法:A:删除功能:void clear():移除集合中的所有键值对元素

                                      V remove(Object key):根据键移除键值对元素,并返回值

              B:判断功能:boolean containsKey(Object key):判断集合中是否包含指定的键

                                      boolean containsValue(Object value):判断集合中是否包含指定的值

                                      boolean isEmpty():判断集合是否为空

              C:获取功能:V get(Object key):根据键获取值

                                      Set<K> keySet():获取所有的键

                                      Collection<V> values():获取所有的值

              D:添加功能:V put(K key,V value):集合添加键值对

              E:长度功能:int size():键值对对数。

6.HashMap:元素顺序为不可预测,底层算法为哈希算法。

               注:键是同一个对象的时候要把之前存入的元素挤出去,想要实现这样的效果的话,

                      需要重写javabean里面的hashCode()和equals()方法。
   exp:用hashmap存储学生的键对值对象并遍历。                  

public class Student {private String name;private int age;public Student() {super();// TODO Auto-generated constructor stub}public Student(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;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}@Overridepublic int hashCode() {//重写hashcode和equals方法,自动生成即可。final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}}

                                            

import java.util.HashMap;import java.util.Set;public class StudentTest {public static void main(String[] args) {Student s1=new Student("a", 1);Student s2=new Student("b", 2);Student s3=new Student("c", 3);HashMap<String, Student> h=new HashMap<String, Student>();h.put("a", s1);h.put("b", s2);h.put("c", s3);Set<String> key1=h.keySet();for (String s : key1) {System.out.println(s+" "+h.get(s));}System.out.println("-----------------------");h.put("a", s2);//把s2覆盖到key“a”上Set<String> key2=h.keySet();for (String s : key2) {//遍历hashmap中的键值System.out.println(s+" "+h.get(s));}}/*b Student [name=b, age=2]  c Student [name=c, age=3]  a Student [name=a, age=1]   -----------------------  b Student [name=b, age=2]  c Student [name=c, age=3]  a Student [name=b, age=2]*/}



0 0
原创粉丝点击