알 리 바 바 자바 개발 매 뉴 얼 노트

5266 단어 KeLa
package com.key.rule.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 *     .   
 * 
 *   hashCode   equals    :
 * 
 * 1.     equals,      hashCode;
 * 
 * 2.   Set          !!!    hashCode equals     ,     Set                ;
 * 
 * 3.           Map   (        ),              hashCode equals   ;
 * 
 * &&   String   hashCode   equals   ,         String   Map  Key ;
 * 
 * @author Key.Xiao
 * @version 1.0
 */
public class CollectionRuleTest {

	public static void main(String[] args) {
		CollectionRuleTest test = new CollectionRuleTest();
		test.collectionToArray();
		test.arrayToList();
		test.foreachTest();
		test.subListTest();
		test.specifiesListSize();
		test.entrySetTest();
		test.removeDuplicateValues();
	}

	/**
	 * [  ]         ,         toArray(T[] array).              ,    list.size
	 */
	private void collectionToArray() {
		List list = new ArrayList();
		list.add("a");
		list.add("b");
		list.add("c");
		String[] array = new String[list.size()];
		array = list.toArray(array);
		System.out.println(array);
	}

	/**
	 * [  ]      Arrays.asList()         ,               ,  remove/add/clear     unsupportedOperationException  
	 * 
	 * asList        ArrayList    ,            ,Arrays.asList          ,      ,         。
	 */
	private void arrayToList() {
		String[] str = new String[] { "a", "b" };
		List list = Arrays.asList(str);
		// list.add("c");      :java.lang.UnsupportedOperationException
		str[0] = "c"; // list.get(0)      .
		System.out.println(list.get(0));
	}

	/**
	 * [  ]    foreach        remove add  , remove     Iterator  ,       ,    Iterator    
	 */
	private void foreachTest() {
		List list = new ArrayList();
		list.add("a");
		list.add("b");
		list.add("c");
		/*   ,    。
		 * for (String string : list) {
			if ("b".equals(string)) {
				list.remove(string);
			}
			System.out.println(string);
		}*/
		/**   : */
		Iterator it = list.iterator();
		while (it.hasNext()) {
			String temp = it.next();
			if ("b".equals(temp)) {
				it.remove();
			}
		}
		for (String string : list) {
			System.out.println(string);
		}
	}

	/**
	 * [  ] ArrayList  subList        ArrayList,       ClassCastException  .
	 * 
	 * java.util.ArrayList$SubList cannot be cast to java.util.ArrayList
	 * 
	 * subList     ArrayList    SubList,   ArrayList,   ArrayList     ,
	 * 
	 *  SubList                   。
	 */
	private void subListTest() {
		ArrayList arrayList = new ArrayList();
		arrayList.add("a");
		arrayList.add("b");
		arrayList.add("c");
		arrayList.add("d");
		// ArrayList subList = (ArrayList) arrayList.subList(0, 2);     
		List subList = arrayList.subList(0, 2); //   
		/**  subList       arrayList 。 */
		subList.add("xiao");
		for (String string : arrayList) {
			System.out.println(string);
		}
		/**        ,         、  、         ConcurrentModificationException   。 */
		arrayList.add("3");
		/*       
		 * for (String string : subList) {
			System.out.println(string);
		}*/
	}

	/**
	 * [  ] JDK1.7     , Comparator        ,   Arrays.sort()  Collections.sort()   IllegalArgumentException  。
	 * 
	 * 1. x,y         y,x       ;
	 * 
	 * 2. x>y, y>z,   x > z
	 * 
	 * 3. x=y,  x,z      y,z      ;
	 * 
	 *           ,         ;
	 */
	private void comparatorTest() {
		//
	}

	/**
	 * [  ] Specifies the list size,            。
	 */
	private void specifiesListSize() {
		int initialCapacity = 100;
		// Constructs an empty list with the specified initial capacity.
		ArrayList list = new ArrayList<>(initialCapacity);
		System.out.println(list.size());
	}

	/**
	 * [  !]   entrySet    Map      Key、Value,     keySet      。
	 * 
	 * keySet        !!!      Iterator  ,      hashMap    key     value
	 * 
	 * keySet()     K   ,    Set     ; values()       V   ,   list    ,
	 * 
	 * entrySet()     K-V      。
	 * 
	 *  entrySet          key value    entry ,     。    JDK8,      Map.foreach  !!!
	 */
	private void entrySetTest() {
		Map map = new HashMap();
		map.put(10, "A");
		map.put(11, "B");
		map.put(12, "C");
		map.put(13, "D");
		for (Entry i : map.entrySet()) {
			System.out.println(i.getKey());
			System.out.println(i.getValue());
		}
	}

	/**
	 *   Set       ,                ,     List contain      、      。
	 */
	private void removeDuplicateValues() {
		List list = new ArrayList();
		list.add(1);
		list.add(1);
		list.add(2);
		list.add(2);
		list.add(3);
		HashSet set = new HashSet(list); //  list    set ,    
		list.clear();
		list.addAll(set);
		for (Object object : list) {
			System.out.println(object);
		}
	}
}

좋은 웹페이지 즐겨찾기