本帖最后由 Himoriarty 于 2015-6-10 13:05 编辑
------- <a target="blank">android培训</a>、<a target="blank">java培训</a>、期待与您交流! ----------
增加元素:
1.1 ArrayList和LinkedList元素为有序的,即存入和取出 的顺序一致。元素可以重复,即在使用add()方法添加元素时,不进行判断,返回值按照 Collection.add(E) 的指定,直接添加,且将指定的元素添加到此列表的尾部。
如果要去掉相同元素必须要在自定义去除相同元素的方法,在方法中将使用到contains(),此方法要使用到equals()方法,所以在对象中覆盖Object类中的equals()方法。
1.2 HashSet和TreeSet元素为无序的,即存入和取出的顺序不一致。元素不可以重复,即在使用add()方法添加元素时,需要进行判断,如果此 set 中尚未包含指定元素,则添加指定元素。更确切地讲,如果此 set 没有包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则向此 set 添加指定的元素 e。如果此 set 已包含该元素,则该调用不更改 set 并返回 false。
对于增加字符串或者数字HashSet、TreeSet这四种都可以经过判断之后存储。
对于添加对象,HashSe可以直接存入,因为对于HashSet来说,比较的是地址值,每个新建对象即使属性值相同,地址值也不同,所以会默认为是不同元素存入,若要去除属性值相同的元素必须要自定义方法去除,为提高效率对哈希值依据属性值进行修改,这样如果属性值不相同则哈希值也不相同可以直接判断,若哈希值相同,不进行存储,不过为了更加严谨,需要覆盖equals(),对属性值进行比较。但TreeSet不行,因为它要对元素进行比较之后按照顺序存储,而对对象来说,基于哪个属性进行排序方法内部不能自行判断,且没有默认方法,必须要继承Comparable接口,实现compareTo()方法,否则会抛出ClassCastException -(如果指定对象无法与此 set 的当前元素进行比较)。
- <div class="blockcode"><blockquote>/**
- LinkedList编写栈
- */
- import java.util.*;
- class Duilie
- {
- private LinkedList link;
- Duilie()
- {
- link = new LinkedList();
- }
-
- public void myAdd(Object obj)
- {
- link.addFirst(obj);
- }
-
- public Object myGet()
- {
- return link.pollLast();
- }
-
- public boolean isNull()
- {
- return link.isEmpty();
- }
- }
- public class LinkedListTest
- {
- public static void main(String[] args)
- {
- Duilie dl = new Duilie();
-
- dl.myAdd("java01");
- dl.myAdd("java02");
- dl.myAdd("java03");
- dl.myAdd("java04");
-
- while(!dl.isNull())
- {
- System.out.println(dl.myGet());
- }
- }
- }
复制代码- //ArrayList练习
- import java.util.*;
- class Person
- {
- private String name;
- private int age;
-
- Person(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
-
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Person))
- return false;
-
- Person p = (Person)obj;
- return this.name.equals(p.name) && this.age == p.age;
- }
-
- public String getName()
- {
- return name;
- }
-
- public int getAge()
- {
- return age;
- }
- }
- public class ArrayListDemo2
- {
- public static void main(String[] args)
- {
- ArrayList al = new ArrayList();
- al.add(new Person("001",20));
- al.add(new Person("002",24));
- al.add(new Person("03",45));
- al.add(new Person("004",32));
- al.add(new Person("004",32));
- al.add(new Person("004",32));
- al = SingleElement(al);
- Iterator it = al.iterator();
-
-
- while(it.hasNext())
- {
- Person p = (Person)it.next();
- System.out.println(p.getName() + "..." + p.getAge());
- }
- }
- public static ArrayList SingleElement(ArrayList al)
- {
- ArrayList newal = new ArrayList();
- Iterator it = al.iterator();
-
- while(it.hasNext())
- {
- Object obj = it.next();
- if(!newal.contains(obj))
- {
- newal.add(obj);
- }
- }
-
- return newal;
- }
- }
复制代码
- <div class="blockcode"><blockquote>/**
- TreeSet练习
- */
- import java.util.*;
- class Student implements Comparable
- {
- private String name;
- private int age;
-
- Student(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
-
- public int compareTo(Object obj)
- {
-
- if(!(obj instanceof Student))
- throw new RuntimeException("is not this class");
- Student s = (Student)obj;
- if(this.age < s.age)
- return 1;
- if(this.age == s.age)
- {
- return this.name.compareTo(s.name);
- }
- return -1;
- }
-
- public String getName()
- {
- return name;
- }
-
- public int getAge()
- {
- return age;
- }
- }
- public class TreeSetDemo
- {
- public static void main(String[] args)
- {
- TreeSet ts = new TreeSet();
-
- ts.add(new Student("004",19));
- ts.add(new Student("003",13));
- ts.add(new Student("006",17));
- ts.add(new Student("002",15));
- ts.add(new Student("0026",15));
-
- Iterator it = ts.iterator();
- while(it.hasNext())
- {
- Object obj = it.next();
- Student s = (Student)obj;
- System.out.println(s.getName() + "..." + s.getAge());
- }
-
- }
- }
复制代码- //HashSet练习
- import java.util.*;
- class Person
- {
- private String name;
- private int age;
-
- Person(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
-
- public int hashCode()
- {
- return this.name.hashCode() +age*23;
- }
- public boolean equals(Object obj)
- {
- /* if(!(obj instanceof Person))
- return false;
-
- Person p = (Person)obj;
- return this.name.equals(p.name) && this.age == p.age; */
- return true;
- }
-
- public String getName()
- {
- return name;
- }
-
- public int getAge()
- {
- return age;
- }
- }
- public class HashCodeDemo
- {
- public static void main(String[] args)
- {
- HashSet hs = new HashSet();
-
- hs.add(new Person("java1", 12));
- hs.add(new Person("java2", 13));
- hs.add(new Person("java2", 13));
- hs.add(new Person("java3", 14));
-
- Iterator it =hs.iterator();
-
- while(it.hasNext())
- {
- Person p = (Person)it.next();
- System.out.println(p.getName() +"..."+p.getAge());
- }
- }
- }
复制代码
----------android培训、java培训、java学习型技术博客、期待与您交流!------------
|
|