Collections framework

MapCollection and Map are two systems

  • Collection: SINGLE-column data that defines a Collection of methods for accessing a set of objects
    • List: an ordered, repeatable collection of elements -> “dynamic” arrays
    • Set: unordered, non-repeatable Set of elements -> Set

-Map: stores the collection of key-value mapping -> functions

Collection

Map

Methods of the interface in Collection

  • Add (), add
  • AddAll (Collection coll1) adds all elements of another Collection to the current Collection
  • Size (), gets the number of elements
  • AddAll (Collection coll1) adds all elements of another Collection to the current Collection
  • Col.clear (), clears the collection elements
  • IsEmpty, whether the current collection isEmpty
  • Contains, whether it contains elements
  • ContainsAll, whether one set contains another set
  • Remove removes elements from the collection
  • RemoveAll removes all elements in Cool1 from the current collection
  • RetainAll, find the intersection of another set and the current set, return to the current set
  • Equals (Object obj) determines whether two collections are identical and have sequential comparisons
  • HashCode () returns the hash value of the current object
  • ToArray collection -> Array
  • Data -> Collections: Call the static method asList of the Arrays class
Package Collection C. import java.util.*; public class CollectionTest { public static void main(String[] args) { Collection coll = new ArrayList(); /* Add ("AA") to equals () */ /add ("AA"); coll.add("bb"); coll.add(123); coll.add(new Date()); System.out.println(col.size ()); //size(), get the number of elements system.out.println (col.size ()); //addAll(Collection coll1), addAll the elements of another Collection to the current Collection Collection coll1 = new ArrayList(); coll1.add(456); coll1.add("cc"); coll.addAll(coll1); System.out.println(coll.size()); System.out.println(coll.toString()); // Coll.clear (); System.out.println(col.isempty ())); System.out.println("************"); coll.add(new Person("Tom", 23)); //contains System.out.println(coll.contains(123)); System.out.println(coll.contains(new String("cc"))); System.out.println(coll.contains(new Person("Tom", 23))); System.out.println("*********"); System.out.println(coll.containsall (coll1)); Col.remove (123); // Equals = 123 system.out.println (coll); coll.remove(new Person("Tom", 23)); System.out.println(coll); Col.removeall (coll1); col.removeall (coll1); System.out.println(coll); // add("AA"); // coll1.add("AA"); // coll1.add("BB"); // coll.retainAll(coll1); // System.out.println(coll); //hashCode() returns the hash value of the current Object system.out.println (col.hashcode ()); //toArray collection -> array Object[] arr = col.toarray (); System.out.println(coll); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } System.out.println(); AsList List<String> List = array. asList(new String[]{"AA", "BB", "CC"}); System.out.println(list); //iterator(); Return an instance of the Iterator interface used to iterate over collection elements}}Copy the code