Java容器

1. 集合转换

Set-->List:ArrayList<BigDecimal> tempArrayList = new ArrayList<>(ss);  
List-->Set: Set<String> listSet = new HashSet<String>(list);  
Set-->Array: set.toArray(arr);  
Array-->Set: Set<String> set = new HashSet<String>(Arrays.asList(arr));  
List-->Array: list.toArray();  
Array-->List: Arrays.asList(array);  
Map-->Set: Set<String> mapValuesSet = new HashSet<String>(map.values()); 
Map-->List: List<String> mapKeyList = new ArrayList<String>(map.keySet());
String-->Array: char[] chars= xx.toCharArray();
Array-->String: new String(chars)

2. List

2.1 ArrayList 常用,查询快,增删慢,非线程安全(底层数组)

//内部Object[]实现
1) 容量不足时每次扩容1/2,会触发一次数组复制动作

2.2 LinkedList 查询慢,增删快,非线程安全(底层链表)

//可当作堆栈、队列和双向队列使用, addFirst() 、getLast() 、 removeFirst()
//链表实现,Node<E>
Node(Node<E> prev, E element, Node<E> next) {
    this.item = element;
    this.next = next;
    this.prev = prev;
}

2.3 过滤list

//1--
Iterator<Foo> it = col.iterator();
while( it.hasNext() ) {
  Foo foo = it.next();
  if( !condition(foo) ) it.remove();
}
//2--java8-steam
List<Person> olderThan30 = personList.stream().filter(p -> p.age >= 30).collect(Collectors.toList());
//3--Use CollectionUtils.filter(Collection,Predicate), from Apache Commons.

2.4 List排序

//默认逆排序,List内的Object都必须实现了Comparable接口,否则报错
Collections.sort(arrayList );
Collections.reverse(arrayList );

//自定义排序
//1) 临时声明一个Comparator来实现排序, public int compare(Object a, Object b){} 返回值大于0则a在后
//按离160最近距离排序
Integer[] zz = {100, 150, 200, 0};
List<Integer> xx = Arrays.asList(zz);
System.out.println(xx);
Collections.sort(xx, new Comparator<Integer>()
{
    public int compare(Integer o1, Integer o2)
    {
        return Math.abs(o1 - 160) - Math.abs(o2 - 160);
    }
});

//2)自定义class实现Comparable接口
class Employee implements Comparable<Employee>
{
    private int id;
    //返回值>0,则this被排在后面,arg0放前面; <0则this在前面。
    public int compareTo(Employee other)
    {
        if (id < other.id)
            return -1;
        if (id > other.id)
            return 1;
        return 0;
    }
}

3. MAp

1)负载因子--空表是0,半满状是0.5,默认0.75,每次扩容容量加倍

3.1 遍历map

1) for (Map.Entry<Integer, Integer> entry : map.entrySet()) {}
2) for (Integer key : map.keySet()) { }
3) Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
   while (entries.hasNext()) {
      Map.Entry<Integer, Integer> entry = entries.next();
   }

3.2 HashMap 常用,可null

HashMap<String , Double> map = new HashMap<>();
map.put("语文" , 80.0);

3.3 HashTable 线程安全,继承Dictionary,key/value非null

3.4 LinkedHashMap按插入顺序遍历

遍历次序按照插入顺序进行,比HashMap慢一点,但迭代访问更快,因其内部是使用数组实现。

3.5 TreeMap 可自定义排序

基于红黑树的实现,次序由Comparator决定。详见List中自定义顺序例子  

3.6 Properties *.properties文件

	Properties prop = new Properties();
	FileInputStream fis = new FileInputStream("prop.properties");
	prop.load(fis);

4. Set

定义equals()方法(int、string已自带)以确保对象唯一性。无序。

4.1 HashSet 常用,可null(HashMap实现)

HashSet<BigDecimal> ss = new HashSet<>();
ss.add(new BigDecimal(123));

4.2 LinkedHashSet 按插入顺序迭代

1)兼具速度与顺序,节点上维护着双重列表,即可知道插入顺序  
2)元素必须定义hashCode()方法来确保唯一性(或默认)

4.3 TreeSet 按指定方式排序

1)按插入顺序保存,将元素存储在红-黑树数据结构中。
2)自定义排序方式,实现Comparable接口,详见见List中排序

5. Queue队列

PriorityQueue-优先队列-【可】自定义优先级,实现Comparator接口来改变优先级。当调用peek()、poll和remove()方法时,获取的将是队列中优先级最高的元素!(详见list自定义排序)

6. Stack栈

posted @ 2017-07-24 14:22  Desneo  阅读(203)  评论(0编辑  收藏  举报