添加所有,频率,不相交

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AddAllFrequencyDisjoint {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    // initialize list1 and list2
      String[] colors = {"red", "white", "yellow", "blue"};
      List list1 = Arrays.asList(colors);
      ArrayList list2 = new ArrayList<>();

      list2.add("black"); // add "black" to the end of list2
      list2.add("red"); // add "red" to the end of list2
      list2.add("green"); // add "green" to the end of list2
      list2.add("red"); // add "red" to the end of list2
      list2.add("red"); // add "red" to the end of list2
     
      System.out.print("Before addAll, list2 contains: ");

      // display elements in vector
      for (String s : list2)
         System.out.printf("%s ", s);

      Collections.addAll(list2, colors); // add colors Strings to list2

      System.out.printf("%nAfter addAll, list2 contains: ");

       for (String s : list2)
         System.out.printf("%s ", s);

       int frequency = Collections.frequency(list2, "red");
      System.out.printf(                                   
         "%nFrequency of red in list2: %d%n", frequency);  

      // check whether list1 and list2 have elements in common
      boolean disjoint = Collections.disjoint(list1, list2);

      System.out.printf("list1 and list2 %s elements in common%n", 
         (disjoint ? "do not have" : "have"));
}

}
Console:
Before addAll, list2 contains: black red green red red
After addAll, list2 contains: black red green red red red white yellow blue
Frequency of red in list2: 4
list1 and list2 have elements in common

你可能感兴趣的:(添加所有,频率,不相交)