Java from entry to the master Chapter 14 collections

table of Contents

Collection Interface

List collection

Set collection

Map collection


Collection Interface

  • List and Set interfaces inherit the Collection interface, so these methods List and Set collections are common
  • By traversing iterator () method returns an iterator, Iterator the next () returns the Object

List collection

  • Allow duplicate, orderly and usable index query
  • Collection LIst interface extends the interface, all the methods contained in the Collection, further comprising two important methods get (int index); obtaining position specified element set (int index, Object obj); the collection specified position of the specified object is modified objects
  • Examples of collections List
    • List <E> list = new ArrayList <> (); fast random access can index, insert, delete, delete slow
    • List <E> list = new LinkedList <> (); random access speed is slow, insert, delete fast speed
package ex14_Collection;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Muster {
    public static void main(String args[]) {
        Collection<String> list = new ArrayList<>();  //实例化集合类对象
        list.add("a");
        list.add("b");
        list.add("c");
        Iterator<String> it = list.iterator();  //创建迭代器
        while (it.hasNext()) {  //判断是否还有下一个元素
            String str = (String) it.next();  //获取集合中的元素
            System.out.println(str);
        }

        //随机获取一个字母,删除索引为2的元素
        List<String> list1 = new ArrayList<>();  //创建集合对象
        list1.add("1");
        list1.add("2");
        list1.add("2");
        int i = (int) (Math.random() * list.size());
        System.out.println("随机访问:" + list1.get(i));
        list1.remove("2"); // 删除对象
        list1.remove(1);  //删除索引位置的对象
        for (int j = 0; j < list1.size(); j++) {  //遍历集合
            System.out.print(list1.get(j) + " ");
        }
    }
}

Set collection

  • Can not be repeated, disorderly
  • Set interface common implementation class has class HashSet and TreeSet class
    • Set HashSet iteration order does not guarantee that allows null elements
    • Set TreeSet implements the interface, the interface also implements java.util.SortedSet, TreeSet class implements Set set in ascending order according to the natural order during traversal, may be sorted according to the specified comparator
  • Are sequentially stored in the Set TreeSet class implements collection must implement Comparable interface, the interface compareTo (Object o) Compare this object with a method of the specified object

package ex14_Collection;


import java.util.Iterator;
import java.util.TreeSet;

public class UpdateStu implements Comparable<Object> {  //创建类实现Comparable接口
    String name;
    long id;

    public UpdateStu(String name, long id) {
        this.name = name;
        this.id = id;
    }

    public int compareTo(Object o) {
        UpdateStu upstu = (UpdateStu) o;
        int result = id > upstu.id ? 1 : (id == upstu.id ? 0 : -1);
        return result;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public static void main(String[] args) {
        UpdateStu stu1 = new UpdateStu("李同学", 1011);
        UpdateStu stu2 = new UpdateStu("陈同学", 1021);
        UpdateStu stu3 = new UpdateStu("王同学", 1031);
        UpdateStu stu4 = new UpdateStu("马同学", 1041);
        TreeSet<UpdateStu> tree = new TreeSet<>();
        tree.add(stu1);
        tree.add(stu2);
        tree.add(stu3);
        tree.add(stu4);
        Iterator<UpdateStu> it = tree.iterator();  //Set集合中的所有对象的迭代器
        System.out.println("Set中所有元素:");
        while (it.hasNext()) {  //遍历集合
            UpdateStu stu = (UpdateStu) it.next();
            System.out.println(stu.getId() + " " + stu.getName());
        }

        it = tree.headSet(stu2).iterator();  //截取stu2之前的对象
        System.out.println("截取前面部分的集合:");
        while (it.hasNext()) {  //遍历集合
            UpdateStu stu = (UpdateStu) it.next();
            System.out.println(stu.getId() + " " + stu.getName());
        }

        it = tree.subSet(stu2, stu4).iterator();  //截取stu2-stu4之间的对象
        System.out.println("截取中间部分的集合:");
        while (it.hasNext()) {  //遍历集合
            UpdateStu stu = (UpdateStu) it.next();
            System.out.println(stu.getId() + " " + stu.getName());
        }

    }

}

Map collection

  • Map interface does not inherit the Collection interface, providing a key to value mapping, Map can not contain the same key
  • Map interface has a common implementation class HashMap and TreeMap
    • HashMap is a realization Map interface hash table-based, high-efficiency add delete the mapping relationship, allows the use of null values and the null key, does not guarantee the mapping order
    • TreeMap not only to achieve Map interface, but also realized java.util.SortedMap interface mapping relationship set there is a certain order , and therefore does not allow the key is null, add low deletion mapping between efficiency
    • keySet () Returns Set collection, values ​​returned collection Collection
  • Map collections can be created through the HashMap, the output order when necessary, and then create an instance of the same class TreeMap complete mappings

package ex14_Collection;


import java.util.*;

public class UpdateStu2 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();  //创建Map实例
        map.put("01", "李同学");  //向集合中添加对象
        map.put("02", "魏同学");
        map.put("03", "魏同学");
        map.put("04", null); //会被打印出来
        Set<String> set = map.keySet();  //构建Map集合中所有key对象的集合
        Iterator<String> it = set.iterator();  //创建集合迭代器
        System.out.println("key集合中的元素:");
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        Collection<String> coll = map.values();  //构建Map集合中所有values值的集合
        it = coll.iterator();
        System.out.println("values集合中的元素");
        while (it.hasNext()) {
            System.out.println(it.next());
        }

    }
}
package ex14_Collection;

public class Emp {
    private String e_id;
    private String e_name;

    public Emp(String e_id, String e_name) {
        this.e_id = e_id;
        this.e_name = e_name;
    }

    public String getE_id() {
        return e_id;
    }

    public String getE_name() {
        return e_name;
    }

    public void setE_id(String e_id) {
        this.e_id = e_id;
    }

    public void setE_name(String e_name) {
        this.e_name = e_name;
    }
}
package ex14_Collection;

import java.util.*;

public class MapText {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();

        Emp emp = new Emp("351", "张三");  //创建Emp对象
        Emp emp2 = new Emp("512", "李四");
        Emp emp3 = new Emp("853", "王一");
        Emp emp4 = new Emp("125", "赵六");
        Emp emp5 = new Emp("341", "黄七");

        map.put(emp.getE_id(), emp.getE_name());  //添加对象到集合中
        map.put(emp2.getE_id(), emp2.getE_name());
        map.put(emp3.getE_id(), emp3.getE_name());
        map.put(emp4.getE_id(), emp4.getE_name());
        map.put(emp5.getE_id(), emp5.getE_name());

        Set<String> set = map.keySet();  //获取Map集合中的key对象集合
        Iterator<String> it = set.iterator();
        System.out.println("HashMap集合类实现的Map集合,无序:");
        while (it.hasNext()) {
            String str = (String) it.next();
            String name = (String) map.get(str);
            System.out.println(str + " " + name);
        }

        TreeMap<String, String> treeMap = new TreeMap<>();  //创建TreeMap集合对象
        treeMap.putAll(map);
        Iterator<String> iter = treeMap.keySet().iterator();
        System.out.println("TreeMap类实现的Map集合,键值升序:");
        while (iter.hasNext()) {
            String str = (String) iter.next();
            String name = treeMap.get(str);
            System.out.println(str + " " + name);
        }

    }
}

 

 

 

 

 

 

 

 

Published 46 original articles · won praise 0 · Views 1024

Guess you like

Origin blog.csdn.net/weixin_37680513/article/details/103482515