Java Day 17

Vector
 addElement()
 elements()
  hasMoreElements()
  nextElement()

LinkedList
 addFirst()
 addLast()

 获取不移除
 getFirst() 有异常抛出
 peekFirst() 返回null

 获取并移除
 removeFirst()  有异常抛出
 pollFirst() 返回null

ArrayList
 

 1 package com.company.Day017;
 2 
 3 import java.util.LinkedList;
 4 
 5 /**
 6  * Created by junius on 2016/10/6.
 7  */
 8 public class DuiLie {
 9     private LinkedList link;
10     DuiLie(){
11         link = new LinkedList();
12     }
13     public void myAdd(Object obj){
14         link.addLast(obj);
15     }
16     public Object myGet(){
17         return link.removeFirst();
18     }
19     public boolean isNull(){
20         return link.isEmpty();
21     }
22 }
23 /*--------------------------------------------*/
24 package com.company.Day017;
25 import com.company.Day017.DuiLie;
26 /**
27  * Created by junius on 2016/10/6.
28  */
29 public class DuiLieDemo {
30     public static void main(String[] args){
31         DuiLie dl = new DuiLie();
32         dl.myAdd("abc1");
33         dl.myAdd("abc2");
34         dl.myAdd("abc3");
35         dl.myAdd("abc4");
36 
37         while(!dl.isNull()){
38             System.out.println(dl.myGet());
39         }
40     }
41 }


Set
 HashSet 内部数据结构是哈希表,不同步
  判断哈希值 hashCode()
  判断内容 equals()
 TreeSet

 1 package com.company.Day017;
 2 
 3 import java.util.HashSet;
 4 import java.util.Iterator;
 5 
 6 /**
 7  * Created by junius on 2016/10/7.
 8  */
 9 public class HashSetTest {
10     public static void main(String[] args) {
11         HashSet hs = new HashSet();
12 
13         hs.add(new Person("lisi1", 23));
14         hs.add(new Person("lisi2", 24));
15         hs.add(new Person("lisi3", 25));
16         hs.add(new Person("lisi1", 23));
17 
18         Iterator it = hs.iterator();
19         while (it.hasNext()) {
20             Person p = (Person) it.next();
21             System.out.println(p.getName() + "..." + p.getAge());
22         }
23     }
24 }

去除ArrayList重复元素

 1 package com.company.Day017;
 2 
 3 import java.sql.Array;
 4 import java.sql.SQLSyntaxErrorException;
 5 import java.util.ArrayList;
 6 import java.util.Iterator;
 7 
 8 /**
 9  * Created by junius on 2016/10/7.
10  */
11 public class removeArrayList {
12     public static void main(String[] args){
13         ArrayList al = new ArrayList();
14         al.add("abc1");
15         al.add("abc2");
16         al.add("abc3");
17         al.add("abc1");
18         al.add("abc");
19         System.out.println(al);
20         al = getSingleArrayList(al);
21         System.out.println(al);
22     }
23 
24     public static ArrayList getSingleArrayList(ArrayList al) {
25         ArrayList temp = new ArrayList();
26         Iterator it = al.iterator();
27         while(it.hasNext()){
28             Object obj = it.next();
29             if(!temp.contains(obj))
30                 temp.add(obj);
31         }
32         return temp;
33     }
34 }

LinkedHashSet 有序的HashSet

TreeSet
 对Set元素进行排序,不同步
 判断元素的唯一性。就是比较方法
 实现Comparable接口 覆盖compareTo方法

 1 package com.company.Day017;
 2 
 3 import java.util.Comparator;
 4 
 5 /**
 6  * Created by junius on 2016/10/7.
 7  */
 8 
 9 
10 public class Person implements Comparable{
11     private String name;
12     private int age;
13 
14     public Person() {
15     }
16 
17     public Person(String name, int age) {
18         this.name = name;
19         this.age = age;
20     }
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public int getAge() {
31         return age;
32     }
33 
34     public void setAge(int age) {
35         this.age = age;
36     }
37 
38     @Override
39     public int hashCode() {
40         return this.name.hashCode()+age;
41     }
42 
43     @Override
44     public boolean equals(Object obj) {
45         Person p = (Person)obj;
46         return this.name.equals(p.name) && this.age==p.age;
47     }
48 
49     @Override
50     public int compareTo(Object o) {
51         Person p = (Person) o;
52         int temp = this.age - p.age;
53         return temp==0?this.name.compareTo(p.name):temp;
54     }
55 }

Comparator比较器
 如果不按照对象中具备的自然顺序进行排序,或对象中不具备自然顺序。
 可以使用TreeSet集合第二种排序方法,让集合自身具备比较功能。
 定义一个类实现Comparator接口,覆盖compare方法。
 将该类对象作为参数传递给TreeSet集合的构造函数

 1 package com.company.Comparator;
 2 
 3 import com.company.Day017.Person;
 4 
 5 import java.util.Comparator;
 6 
 7 /**
 8  * Created by junius on 2016/10/7.
 9  */
10 public class ComparatorByName implements Comparator{
11     @Override
12     public int compare(Object o1, Object o2) {
13         Person p1 = (Person) o1;
14         Person p2 = (Person) o2;
15 
16         int temp = p1.getName().compareTo(p2.getName());
17         return temp==0?p1.getAge()-p2.getAge():temp;
18 //        return -1;
19     }
20 }
 1 package com.company.Day017;
 2 
 3 import com.company.Comparator.ComparatorByLength;
 4 
 5 import java.util.Iterator;
 6 import java.util.TreeSet;
 7 
 8 /**
 9  * Created by junius on 2016/10/7
10  */
11 public class TreeSetUsing {
12     public static void main(String[] args) {
13         TreeSet ts = new TreeSet(new ComparatorByLength());
14 
15         ts.add("abd");
16         ts.add("abccc");
17         ts.add("abc");
18         ts.add("zzzz");
19         ts.add("aaaa");
20 
21         Iterator it = ts.iterator();
22         while (it.hasNext()) {
23             String s = (String) it.next();
24             System.out.println(s);
25         }
26     }
27 }

二叉树
 1、实现怎么存进去怎么取出来

 compare 直接 return 1;

2、逆序取元素

compare 直接 return -1;

 
字符串长度排序-TreeSet

 1 package com.company.Comparator;
 2 
 3 import java.util.Comparator;
 4 
 5 /**
 6  * Created by junius on 2016/10/7.
 7  */
 8 public class ComparatorByLength implements Comparator{
 9     @Override
10     public int compare(Object o1, Object o2) {
11         String s1 = (String)o1;
12         String s2 = (String)o2;
13         int temp = s1.length()-s2.length();
14         return temp==0?s1.compareTo(s2):temp;
15     }
16 }
17 
18 /*------------------------------------------------------------*/
19 package com.company.Day017;
20 
21 import com.company.Comparator.ComparatorByLength;
22 
23 import java.util.Iterator;
24 import java.util.TreeSet;
25 
26 /**
27  * Created by junius on 2016/10/7
28  */
29 public class TreeSetUsing {
30     public static void main(String[] args) {
31         TreeSet ts = new TreeSet(new ComparatorByLength());
32 
33         ts.add("abd");
34         ts.add("abccc");
35         ts.add("abc");
36         ts.add("zzzz");
37         ts.add("aaaa");
38 
39         Iterator it = ts.iterator();
40         while (it.hasNext()) {
41             String s = (String) it.next();
42             System.out.println(s);
43         }
44     }
45 }
View Code
原文地址:https://www.cnblogs.com/zhuzhuqwa/p/5936219.html