Java Collection class
Collection classes can be understood as a dynamic array, and objects in the set can be expanded at will. The characteristics of the set are high performance and easy to expand. Common Collection subclass: List, Set, Map 1, List Interface
The list interface can store any type of data, and the content in the List interface can be repeated.
List interfaces are commonly used subclass: ArrayList, Vector.
1.1 ArrayList
The following Demo shows how to create an ArrayList object, add elements, access elements, and remove elements.
Package ucas. collection. demo; import java. util. ArrayList; import java. util. List; public class ListDemo {public static void main (String [] args) {List
Lists = null; lists = new ArrayList
(); // The list can be repeated // Add lists. add ("A"); lists. add ("B"); lists. add ("A"); for (int I = 0; I <lists. size (); I ++) {System. out. println (lists. get (I);} System. out. println ("judge whether the lists set is empty:" + lists. isEmpty (); // returns the first occurrence location System. out. println ("whether A exists:" + lists. indexOf ("A"); // remove lists. remove (0); System. out. println ("after the first item is removed:"); for (int I = 0; I <lists. size (); I ++) {System. out. println (lists. get (I ));}}}
1.2 Vector
Vector and ArrayList are used in the same way. The difference mainly lies in the internal implementation performance and whether it is thread security:
Comparison item |
ArrayList |
Vector |
Release Date |
Jdk1.2 |
Jdk1.0 |
Processing Method |
Asynchronous processing, high performance |
Synchronous processing, low performance |
Is it thread-safe? |
Non-thread security |
Thread Security |
VectorDemo
Package ucas. collection. demo; import java. util. list; import java. util. vector; public class VectorDemo {public static void main (String [] args) {// ArrayList jdk1.2 was launched later. It adopts asynchronous processing and has high performance and is non-thread-safe; // Vector jdk1.0 was launched later. It adopts the synchronous processing method with low performance and is thread-safe. // Method usage is the same as List
Lists = null; lists = new Vector
(); Lists. add ("A"); lists. add ("B"); for (int I = 0; I <lists. size (); I ++) {System. out. println (lists. get (I ));}}}
Ii. Set Interface
Duplicate elements cannot be added to the Set interface, but they can be sorted. There are two common Set sub-classes. HashSet is used for hashed storage and TreeSet is used for ordered storage.
Package ucas. collection. demo; import java. util. hashSet; import java. util. iterator; import java. util. set; import java. util. treeSet; public class SetDemo {public static void main (String [] args) {/** duplicate elements cannot be added to the Set interface, but the common sub-classes of Set can be sorted for hashed storage: hashSet * sequential storage: TreeSet */Set
S = null; s = new TreeSet
(); S. add ("B"); s. add ("A"); s. add ("C"); s. add ("D"); s. add ("E"); s. add ("F"); s. add ("G"); System. out. println (s );}}
Output:
[A, B, C, D, E, F, G]
HashSet:
package ucas.collection.demo;import java.util.HashSet;import java.util.Set;public class HashSetDemo { public static void main(String[] args) { Set
hs=new HashSet
(); hs.add("two"); hs.add("one"); hs.add("three"); hs.add("four"); hs.add("five"); hs.add("six"); System.out.println(hs); }}
Output
[six, four, one, two, three, five]
Iii. Map Interface
The Map interface is saved as key-> value.
Common subclass:
HashMap: unordered storage. duplicate key values are not allowed.
HashTable: unordered storage, duplicate key values are not allowed
Package ucas. collection. demo; import java. util. collection; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; public class MapDemo01 {public static void main (String [] args) {Map
Map = new HashMap
(); Map. put ("key1", "java advanced 1"); map. put ("key2", "java advanced 2"); map. put ("key3", "java advanced 3"); map. put ("key4", "java advanced 4"); map. put ("key5", "java advanced 5"); map. put ("key6", "java advanced 6"); System. out. println (map. get ("key1"); // determines whether the key value exists if (map. containsKey ("key12") {System. out. println ("key exists");} else {System. out. println ("key does not exist");} // determines whether the value exists if (map. containsValue ("java advanced 5") {System. out. println ("value exists");} else {System. out. println ("value does not exist");} // Iterator cyclically outputs System. out. println (map. keySet (); Set
S = map. keySet (); Iterator
Iter = s. iterator (); while (iter. hasNext () {System. out. println (iter. next ();} // get all value collections
C = map. values (); Iterator
Iter2 = c. iterator (); while (iter2.hasNext () {System. out. println (iter2.next ());}}}
Iterator
Iterator is used to iterate the elements in the output set. Iterative output is to judge the elements one by one, and retrieve the content when determining the content.
Package ucas. collection. demo; import java. util. arrayList; import java. util. iterator; import java. util. list; public class IteratorDemo {public static void main (String [] args) {List
Lists = new ArrayList
(); Lists. add ("Q"); lists. add ("W"); lists. add ("E"); lists. add ("A"); lists. add ("B"); Iterator
Iter = lists. iterator (); while (iter. hasNext () {// lists. remove (1); elements cannot be deleted during the Iterator iteration output set; otherwise, an error occurs. out. println (iter. next ());}}}