### TreeMap

TreeMap与HashMap和Hashtable的区别在于可以按照key值对集合中的数据进行排序。

```java
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class Test2 {
    public static void main(String[] args) {
        TreeMap<User,String> treeMap = new TreeMap();
        treeMap.put(new User(3,"Java"), "Java");
        treeMap.put(new User(5,"JavaME"), "JavaME");
        treeMap.put(new User(1,"Hello"), "Hello");
        treeMap.put(new User(6,"JavaEE"), "JavaEE");
        treeMap.put(new User(2,"World"), "World");
        treeMap.put(new User(4,"JavaSE"), "JavaSE");
        Set keys = treeMap.keySet();
        Iterator iterator = keys.iterator();
        while(iterator.hasNext()) {
            User key = (User) iterator.next();
            String value = treeMap.get(key);
            System.out.println(key+"-"+value);
        }
        System.out.println("集合中的第一个key:"+treeMap.firstKey());
        System.out.println("集合中的第一个entry:"+treeMap.firstEntry());
        System.out.println("集合中的最后一个key:"+treeMap.lastKey());
        System.out.println("集合中的最后一个entry:"+treeMap.lastEntry());
        System.out.println(treeMap.higherKey(new User(3,"Java")));
        System.out.println(treeMap.lowerKey(new User(3,"Java")));
        System.out.println(treeMap.higherEntry(new User(3,"Java")));
        System.out.println(treeMap.lowerEntry(new User(3,"Java")));
        System.out.println(treeMap.subMap(new User(3,"Java"), new User(5,"JavaME")));
    }
}

class User implements Comparable{
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public User(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }
    
    /**
     * A.compareTo(B)
     * 返回值:
     * 1表示A大于B
     * 0表示A等于B
     * -1表示A小于B
     */
    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        User user = (User)o;
        if(this.id > user.id) {
            return 1;
        }else if(this.id == user.id) {
            return 0;
        }else {
            return -1;
        }
    }
}
```

### Collections

Collections封装了一系列可以操作集合的方法,与Arrays类似,Arrays是针对数组的工具类,Collections是针对集合的工具类。

Collections常用方法:

- sort                                 根据集合泛型对应的类实现的Comparable接口对集合进行排序
- binarySearch                                                 查找元素在集合中的下标,要求集合元素必须是升序排列
- get                                                                   根据下标找到对应的元素
- reverse                                                            将集合中的元素反序输出
- swap                                                                交换集合中指定位置的两个元素
- fill                                                                      将集合中所有的元素替换为一个指定值
- min                                                                  根据Comparable接口找到集合中的最小元素
- max                                                                 根据Comparable接口找到集合中的最大元素
- replaceAll                                                       将集合中所有元素替换为一个指定值
- addAll                                                              将一个集合中的所有值添加到目标集合中

```java
import java.util.ArrayList;
import java.util.Collections;

public class Test3 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("Hello");
        list.add("World");
        Collections.addAll(list,"Java","JavaSE","JavaEE","Java","Java");
        System.out.println(list);
        Collections.reverse(list);
        System.out.println(list);
        Collections.swap(list, 1, 3);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
        int index = Collections.binarySearch(list, "JavaEE");
        System.out.println(index);
        Collections.replaceAll(list, "Java", "Collections");
        System.out.println(list);
        Collections.addAll(list, 1,new User(1,"a"),"Str");
        System.out.println(list);
    }
}
```



### 泛型

泛型(Generics)是指在类定义时不指定类中信息的具体数据类型,而是用一个标识符来代替,当外部实例化对象时来指定具体的数据类型。

有了泛型我们就可以在定义类或者定义接口时不明确指定类中信息的具体数据类型,在实例化时再来指定,这样就极大的提升了程序的扩展性,一个类可以装载各种不同的数据类型,泛型可以指代类中成员变量的类型,方法的返回值类型以及方法的参数类型。

#### 泛型的应用

我们除了可以在实例化集合时指定泛型外,自定义的类也可以添加泛型,基本语法如下:

访问权限修饰符 class 类名<泛型标识1,泛型标识2...>{

​    访问权限修饰符 泛型标识 属性名;

​    访问权限修饰符 泛型标识 方法名(泛型标识 参数...){}

}

```java
public class Time<H,M,S> {
    private H hour;
    private M minute;
    private S second;
    public H getHour() {
        return hour;
    }
    public void setHour(H hour) {
        this.hour = hour;
    }
    public M getMinute() {
        return minute;
    }
    public void setMinute(M minute) {
        this.minute = minute;
    }
    public S getSecond() {
        return second;
    }
    public void setSecond(S second) {
        this.second = second;
    }
}
```

```java
public class Test5 {
    public static void main(String[] args) {
        Time<String,Integer,Float> time = new Time();
        time.setHour("十点");
        time.setMinute(10);
        time.setSecond(10.0f);
        System.out.println("现在的时间是:"+time.getHour()+":"+time.getMinute()+":"+time.getSecond());
    }
}
```



#### 泛型通配符

如果我们在定义一个参数为ArrayList类型的方法时,希望该方法即可以接收String类型的参数,也可以接收Integer类型的参数,应该如何处理?可以使用多态的思想来定义该方法。

```java
import java.util.ArrayList;

public class Test6 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList();
        ArrayList<Integer> list2 = new ArrayList();
        test(list1);
        test(list2);
    }
    
    public static void test(ArrayList<?> list) {
        
    }
}
```



#### 泛型上限和下限

我们在使用泛型时,往往数据类型会有限制,只能使用一种具体的数据类型,可以通过泛型上限和下限完成对数据类型的扩容。

泛型上限表示实例化的具体数据类型,可以是上限类型的子类或者上限类型本身,用extends关键字来修饰。泛型下限表示实例化的具体数据类型,可以是下限类型的父类或者下限类型本身,用super关键字来修饰。

泛型上限:类名<泛型标识 extends 上限类名>

泛型下限:类名<泛型标识 super 下限类名>

```java
import java.util.ArrayList;

public class Test6 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList();
        ArrayList<Integer> list2 = new ArrayList();
        test(list1);
        test(list2);
    }
    
    public static void test(ArrayList<?> list) {
        
    }
}
```



#### 泛型接口

不仅仅在定义类时可以添加泛型,同样的在定义接口时也可以添加泛型,声明泛型接口的语法和声明泛型类的语法很相似,在接口名添加<T>即可。

基本语法:访问权限修饰符 interface 接口名<泛型标识> 

```java
public interface MyInterface<T> {
    public T getValue();
}
```

实现泛型接口有两种方式,一种是实现类在定义时继续使用泛型标识,另一种是实现类在定义时直接给出具体的数据类型。

```java
public class MyInterfaceImpl<T> implements MyInterface<T> {

    @Override
    public T getValue() {
        // TODO Auto-generated method stub
        return null;
    }

}
```



```java
public class MyInterfaceImpl2 implements MyInterface<String> {

    @Override
    public String getValue() {
        // TODO Auto-generated method stub
        return null;
    }

}
```



两种不同实现类的实例化方式也是不同的,第一种(实现类在定义时继续使用泛型)需要在实例化时指定具体的数据类型。

第二种(实现类在定义时指定具体的数据类型)在实例化时不需要指定具体的数据类型。



```java
public class Test7 {
    public static void main(String[] args) {
        MyInterface<String> my = new MyInterfaceImpl<>("接口");
        String value = my.getValue();
        System.out.println(value);
        
        MyInterface my2 = new MyInterfaceImpl2("接口");
        String value2 = (String)my2.getValue();
        System.out.println(value2);
        
    }
}
```

MyInterface.java

public interface MyInterface<T> {
    public T getValue();
}

 

MyInterfaceImpl.java

public class MyInterfaceImpl<T> implements MyInterface<T> {

    private T value;
    
    @Override
    public T getValue() {
        // TODO Auto-generated method stub
        return this.value;
    }

    public MyInterfaceImpl(T value) {
        super();
        this.value = value;
    }
    
    

}

 

MyInterImpl2.java

public class MyInterfaceImpl2 implements MyInterface<String> {
    
    private String value;

    @Override
    public String getValue() {
        // TODO Auto-generated method stub
        return this.value;
    }

    public MyInterfaceImpl2(String value) {
        super();
        this.value = value;
    }
    
    

}

 

 

 Test.java

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<Integer,String> treeMap = new TreeMap();
        treeMap.put(3, "Java");
        treeMap.put(5, "JavaME");
        treeMap.put(1, "Hello");
        treeMap.put(6, "JavaEE");
        treeMap.put(2, "World");
        treeMap.put(4, "JavaSE");
        System.out.println(treeMap);
        Set keys = treeMap.keySet();
        Iterator setIter = keys.iterator();
        while(setIter.hasNext()) {
            Integer key = (Integer)setIter.next();
            String value = treeMap.get(key);
            System.out.println(key+"-"+value);
        }
        Collection values = treeMap.values();
        Iterator valueIter = values.iterator();
        while(valueIter.hasNext()) {
            String value = (String)valueIter.next();
            System.out.println(value);
        }
    }
}

 

Test2.java

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class Test2 {
    public static void main(String[] args) {
        TreeMap<User,String> treeMap = new TreeMap();
        treeMap.put(new User(3,"Java"), "Java");
        treeMap.put(new User(5,"JavaME"), "JavaME");
        treeMap.put(new User(1,"Hello"), "Hello");
        treeMap.put(new User(6,"JavaEE"), "JavaEE");
        treeMap.put(new User(2,"World"), "World");
        treeMap.put(new User(4,"JavaSE"), "JavaSE");
        Set keys = treeMap.keySet();
        Iterator iterator = keys.iterator();
        while(iterator.hasNext()) {
            User key = (User) iterator.next();
            String value = treeMap.get(key);
            System.out.println(key+"-"+value);
        }
        System.out.println("集合中的第一个key:"+treeMap.firstKey());
        System.out.println("集合中的第一个entry:"+treeMap.firstEntry());
        System.out.println("集合中的最后一个key:"+treeMap.lastKey());
        System.out.println("集合中的最后一个entry:"+treeMap.lastEntry());
        System.out.println(treeMap.higherKey(new User(3,"Java")));
        System.out.println(treeMap.lowerKey(new User(3,"Java")));
        System.out.println(treeMap.higherEntry(new User(3,"Java")));
        System.out.println(treeMap.lowerEntry(new User(3,"Java")));
        System.out.println(treeMap.subMap(new User(3,"Java"), new User(5,"JavaME")));
    }
}

class User implements Comparable{
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public User(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }
    
    /**
     * A.compareTo(B)
     * 返回值:
     * 1表示A大于B
     * 0表示A等于B
     * -1表示A小于B
     */
    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        User user = (User)o;
        if(this.id > user.id) {
            return 1;
        }else if(this.id == user.id) {
            return 0;
        }else {
            return -1;
        }
    }
}

 

Test3.java

import java.util.ArrayList;
import java.util.Collections;

public class Test3 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("Hello");
        list.add("World");
        Collections.addAll(list,"Java","JavaSE","JavaEE","Java","Java");
        System.out.println(list);
        Collections.reverse(list);
        System.out.println(list);
        Collections.swap(list, 1, 3);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
        int index = Collections.binarySearch(list, "JavaEE");
        System.out.println(index);
        Collections.replaceAll(list, "Java", "Collections");
        System.out.println(list);
        Collections.addAll(list, 1,new User(1,"a"),"Str");
        System.out.println(list);
    }
}

 

Test4.java

import java.util.ArrayList;

public class Test4 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList();
        list.add(1);
        list.add(2);
        for(int i = 0; i < list.size();i++) {
            int num = list.get(i);
            System.out.println(num);
        }
    }
}

 

Test5.java

public class Test5 {
    public static void main(String[] args) {
        Time<String,Integer,Float> time = new Time();
        time.setHour("十点");
        time.setMinute(10);
        time.setSecond(10.0f);
        System.out.println("现在的时间是:"+time.getHour()+":"+time.getMinute()+":"+time.getSecond());
    }
}

 

Test6.java

import java.util.ArrayList;

public class Test6 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList();
        ArrayList<Integer> list2 = new ArrayList();
        test(list1);
        test(list2);
    }
    
    public static void test(ArrayList<?> list) {
        
    }
}

 

Test7.java

public class Test7 {
    public static void main(String[] args) {
        MyInterface<String> my = new MyInterfaceImpl<>("接口");
        String value = my.getValue();
        System.out.println(value);
        
        MyInterface my2 = new MyInterfaceImpl2("接口");
        String value2 = (String)my2.getValue();
        System.out.println(value2);
        
    }
}

 

Time.java

public class Time<H,M,S> {
    private H hour;
    private M minute;
    private S second;
    public H getHour() {
        return hour;
    }
    public void setHour(H hour) {
        this.hour = hour;
    }
    public M getMinute() {
        return minute;
    }
    public void setMinute(M minute) {
        this.minute = minute;
    }
    public S getSecond() {
        return second;
    }
    public void setSecond(S second) {
        this.second = second;
    }
    
}

 

test\Time.java

package test;

public class Time<T> {
    public static void main(String[] args) {
        test(new Time<Integer>());
//        test(new Time<String>());
        test2(new Time<String>());
//        test2(new Time<Integer>());
    }
    
    /**
     * 参数泛型只能是Number或者子类,Number、Byte、Short
     */
    public static void test(Time<? extends Number> time) {
        
    }
    
    /**
     * 参数泛型只能是String或者其父类,即String和Object
     */
    public static void test2(Time<? super String> time) {
        
    }
}

 

posted on 2019-07-11 21:59  HiJackykun  阅读(503)  评论(0编辑  收藏  举报