Topic one:
Create two linear form, are stored { "chen", "wang", "liu", "zhang"} and { "chen", "hu", "zhang"}, intersection of these two tables and linear and set.
Code:
List_Test.java
1 /** 2 * 利用List<E>和Set<E>,求交集和并集; 3 */ 4 package cn.edu.ccut1; 5 import java.util.*; 6 7 public class Test_List { 8 9 public static void main(String[] args) { 10 ArrayList<String> L1 = new ArrayList<String>(); 11 ArrayList<String> L2 = new ArrayList<String>(); 12 L1.add("chen"); 13 L1.add("wang"); 14 L1.add("liu"); 15 L1.add("zhang"); 16 L2.add("chen"); 17 L2.add("Hu"); 18 L2.add("zhang"); 19 ArrayList<String> Jj = new ArrayList<String>(); 20 Jj.addAll(L1); 21 Jj.retainAll(L2); //Jj with the same elements in L2 is not all removed; 22 is the System. OUT .println ( " Intersection is: " + Jj.toString ()); 23 is HashSet <String> of Bj = new new HashSet <String> (); 24 of Bj .addAll (Ll); 25 Bj.addAll (L2 of); // use set <E> and unique features elements acquire and remove duplicate set; 26 is the System. OUT .println ( " and set is: " + Bj.toString ()); 27 } 28 }
operation result: