Java 集合类的细节

java集合类

1.Collection,Map层次图

2.Collection接口

list

存放有序且允许重复的集合的接口 这里的有序是指存入顺序和取出顺序相同。子类有:{ ArrayList,LinkedList}
ArrayList: 是通过数组结构实现的List集合 (通过索引值快速取出数据) 删除,插入慢,可使用for语句遍历
     public Object get(int index)           返回列表中指定位置的元素
    public void add(int index, Object element)   在列表的指定位置插入指定元素.将当前处于该位置的元素(如果有的话)和所有后续元素向右移动
     public Object set(int index, Object element) 用指定元素替换列表中指定位置的元素,返回替换出来的元素
     public Object remove(int index)         移除列表中指定位置的元素
     List subList(int fromIndex, int toIndex)     返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。
     int indexOf(Object o)             返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。

LinkedList: 是通过使用双向链表实现的List集合(毫无疑问,LinkedList对于频繁的插入删除有着较好的效率,
       适合实现栈(Stack)和队列(Queue)。可使用for语句遍历


  void addFirst(E e)    将指定元素插入此双端队列的开头。
   void addLast(E e)      将指定元素添加到此列表的结尾。等效于add(E e)
  E getFirst()         返回此列表的第一个元素,等效于element(),只看不动.
  E getLast()         返回此列表的最后一个元素。只看不动.

set
存放无序且不包含重复元素的集合的接口。子类:{HashSet SorterSet}实现类:{ LinkedHashSet, TreeSet} 

LinkHashSet:

  根据元素的哈希码进行存放,同时用链表记录元素的加入顺序。 不能使用for语句遍历,可以使用foreach,Iterator
TreeSet:
 TreeSet使用红黑树结构对加入的元素进行排序存放,输出时也会按排序后的顺序,所以放入TreeSet中元素必须是”可排序的”
两个对象判断是否重复是通过HashCode和equals 比较的。TreeSe
t不能使用for语句遍历,可以使用foreach,Iterator
Collection代码实现

遍历:
        Collection <String>collection1=new ArrayList();
        collection1.add("1");
        collection1.add("1");
        Iterator<String> iterator=collection1.iterator();
        while (iterator.hasNext())
        Log.d(TAG, "onCreate:collection1:: "+iterator.next());

集合对象排序:

(1) 排序对象实现Comparable接口重写方法compareTo

public class Person2  implements Comparable<Person2>{

    public String name;
     public  int age;

    public Person2(String name, int age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public int compareTo(@NonNull Person2 o) {
        return this.age-o.age;
    }
}
View Code

 

     List<Person2> list=new ArrayList<>();
        list.add(new Person2("gxw",23));
        list.add(new Person2("gxw2",25));
        list.add(new Person2("gxw4",22));
        Collections.sort(list);
        for(Person2 person2:list){
        Log.d(TAG, "onCreate: Person2::"+person2.name+"**"+person2.age);

        }

(2) 创建一个比较器,实现ava.util.Comparator接口,重写compare方法

public class Comparator<E> implements java.util.Comparator<Person3> {
    @Override
    public int compare(Person3 o1, Person3 o2) {
        /**
         * 先比较name,再比较age
         */
       int flag= o1.getName().compareTo(o2.getName());
        if(flag==0){
        return  o1.getAge()-o2.getAge();
        }else{
            return flag;
        }

    }
}
View Code
public class Person3  {
    public String name;
    public int age;

    public Person3(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

     List<Person3> list2=new ArrayList<>();
        list2.add(new Person3("gxw",23));
        list2.add(new Person3("gxw",24));
        list2.add(new Person3("gxw",25));
        list2.add(new Person3("gxc",23));
        list2.add(new Person3("gxe",23));
        list2.add(new Person3("gxy",24));
        list2.add(new Person3("gxa",25));
        list2.add(new Person3("gxz",25));
        Comparator<Person3> comparator=new Comparator<Person3>();
        Collections.sort(list2,comparator);

        for(Person3 person3:list2){
        Log.d(TAG, "onCreate: Person3::"+person3.toString());
        }

Map接口

 

Map实现类中存储的“键-值”映射对是通过键来唯一标识。
V   put(K key, V value)               将指定的"键-值"对存入Map中
V get(Object key);    返回指定键所映射的值
V remove(Object key);  根据指定的键把此"键-值"对从Map中移除。
boolean containsKey(Object key); 判断此Map是否包含指定键的"键-值"对。
boolean containsValue(Object value); 判断此Map是否包含指定值的"键-值"对。
int size();  获得些Map中"键-值"对的数量。
Set<K> keySet();   返回此Map中包含的键的Set集。
Collection<V> values();  返回此Map中包含的值的Collection集。值是可重复的.
对于Map接口来说,其本身是不能直接使用迭代进行输出的,因为Map中的每一个位置存放的是一对值(keyvalue),
而Iterator中每次只能找到一个值。所以如果非要使用迭代进行输出的话,要按照以下操作步骤完成:
1.将Map的实例通过entrySet()方法变为Set接口对象
2.通过Set接口的实例的iterator() 将Iterator实例化
3.通过Iterator迭代输出,每个内容都是Map.Entry的对象
4.通过Map.Entry进行keyvalue的分离。getKey,getValue

HashMap:
  HashMap中的映射对的"键"如果是自定义的类,应该重写hashCode()和equals()方法。
  Hashtable 
  是同步的(线程安全的);
  不能有null键,也不能有null值,否则运行时出空指针异常;
  基于陈旧的Dictionary<K,V>类 ,有contains() 方法,用于测试此映射表中是否存在指定值,等同于HashMap类中的containsValue()

  HashMap
  是不同步的(线程非安全的);
  能存储最多一个null键,任意多个null值;
  有containsKey(),containsValue()方法,没有contains() 方法
  继承AbstractMap<K,V>
TreeMap:
  TreeMap内部使用红黑树结构对"key"进行排序存放,所以放入TreeMap中的"key-value"对的"key"必须是"可排序"的。
  TreeMap()使用键的自然顺序构造一个新的、空的树映射。
  TreeMap(Comparator<? super K>  comparator) 构造一个新的、空的树映射,该映射根据给定比较器进行排序。

Map代码实现

遍历:

(1)遍历key-Vaule值
     HashMap<String,String> map=new HashMap<String,String>();
        map.put("key1","value2");
        map.put("key2","value1");
        map.put("key4","value4");
        map.put("key3","value3");
        Iterator<Map.Entry<String,String>>it=map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry1 = it.next();
            Log.d(TAG, "onCreate: Map::entry"+entry1.getKey()+"*"+entry1.getValue());
        }
(2)keySet遍历key值
       Set<String> set1=map.keySet();
          Iterator<String> it2=set1.iterator();
          while (it2.hasNext())
            Log.d(TAG, "onCreate: KeySet::"+it2.next());

(3)遍历Value值

        for(String s:map.values()){
            Log.d(TAG, "onCreate: MapValue::"+s);
        }

(4)遍历key_Value

     for(Map.Entry<String,String> entry3:map.entrySet()){

            Log.d(TAG, "onCreate: Map3:::"+entry3.getKey()+"*"+entry3.getValue());
        }

 

Map排序

key值排序:
     TreeMap<PersonMap,String> map2=new TreeMap<>();
        map2.put(new PersonMap("gxw",23),"222");
        map2.put(new PersonMap("gxw2",23),"322");
        map2.put(new PersonMap("gxw3",24),"332");
        map2.put(new PersonMap("gxw",24),"342");
        map2.put(new PersonMap("gxwr",23),"3424");
        for(Map.Entry<PersonMap,String> en:map2.entrySet()){
            Map.Entry<PersonMap,String> it3=en;
            Log.d(TAG, "onCreate: Map210:"+it3.getKey().toString()+"*"+it3.getValue());
        }
public class PersonMap  implements Comparable<PersonMap>{

    String name;
    int age;

    public PersonMap(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(@NonNull PersonMap o) {
        int flag=this.age-o.age;
        if(flag==0){
          return   this.name.compareTo(o.name);
        }else{

        }
        return flag;
    }

    @Override
    public String toString() {
        return "PersonMap{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

Value排序
        TreeMap<PersonMap,String> map3=new TreeMap<>();
        map3.put(new PersonMap("gxw",23),"222");
        map3.put(new PersonMap("gxw2",23),"322");
        map3.put(new PersonMap("gxw3",24),"332");
        map3.put(new PersonMap("gxw",24),"342");
        map3.put(new PersonMap("gxwr",23),"3424");
        List<Map.Entry<PersonMap,String>>list3=new ArrayList<>(map3.entrySet());

        MapComapreble ma=new MapComapreble();
        Collections.sort(list3,ma);
        for (Map.Entry<PersonMap,String> keyss:list3)
            Log.d(TAG, "onCreate: Value230:::"+keyss.getKey().toString()+"**"+keyss.getValue());
public class MapComapreble implements java.util.Comparator<Map.Entry<PersonMap,String>>  {


    @Override
    public int compare(Map.Entry<PersonMap, String> o1, Map.Entry<PersonMap, String> o2) {
        return o1.getValue().compareTo(o2.getValue());
    }


}

小结:


Set: 无序,无下标,不能随机访问,不可重复
List:有序,有下标,可以随机访问,可重复
Map:"key-value"对,较多存放和查询,较少遍历
HashMap-读写两者都较高
ArrayList-读(指定下标随机访问)快,插入/删除元素慢
LinkedList-读(指定下标随机访问)慢,插入/删除元素快


 

 
 
 

 

 
posted @ 2017-03-22 15:34  咖喱不见不散啊  阅读(378)  评论(0编辑  收藏  举报