자바 List 집합 집합 집합 상 감 원 생태 방법

1890 단어 Java
package com.bugyun.exception;

import java.util.ArrayList;
import java.util.List;

/**  
 *     :Test  
 *    :CollectionTest.java  
 *    :  
 *    :beyond  
 *     :2016 4 1    2:17:05  
 *    : beyond 
 *     :2016-4-26  11:19:50 
 *     :
 * @version   
*/
public class CollectionTest {
	public static void main(String[] args) {
		List listOne = new ArrayList();
		listOne.add("abc");
		listOne.add("123");
		listOne.add("ABC");

		List listTwo = new ArrayList();
		listTwo.add("abc");
		listTwo.add("123");
		listTwo.add("XYZ");

		//             ,     ;listOne        ,       
		// [ABC, abc, 123, XYZ]
		// System.out.println(intersection(listOne, listTwo));
		// //[abc, 123]
		// System.out.println(union(listOne, listTwo));
		// //[ABC]
		// System.out.println(subtract(listOne, listTwo));
	}

	/**
	 * @description:        
	 * @param listOne
	 * @param listTwo
	 * @return List
	 * @throws
	 * @author beyond
	 * @data:2016 4 1   2:37:01
	 */
	private static  List intersection(List listOne, List listTwo) {
		listOne.removeAll(listTwo);
		listOne.addAll(listTwo);
		return listOne;
	}

	/**
	 * @description:        
	 * @param listOne
	 * @param listTwo
	 * @return List
	 * @throws
	 * @author beyond
	 * @data:2016 4 1   2:37:20
	 */
	private static  List union(List listOne, List listTwo) {
		listOne.retainAll(listTwo);
		return listOne;
	}

	/**
	 * @description: listOne - ListTwo   
	 * @param listOne
	 * @param listTwo
	 * @return List
	 * @throws
	 * @author beyond
	 * @data:2016-4-26  11:19:50
	 */
	private static  List subtract(List listOne, List listTwo) {
		listOne.removeAll(listTwo);
		return listOne;
	}
}

좋은 웹페이지 즐겨찾기