Collection. A sequence of independent elements that obey one or more rules. The list must save the element in the order in which it was inserted, and set cannot have duplicate elements. Map. A pair of key-value pairs of objects that allow keys to be used to look up values. There are 2 types of lists: basic ArrayList, longer than random-access elements, but slower when inserting and removing elements in the middle of a list. LinkedList, it provides optimized sequential access by inserting and deleting operations in the middle of the list at a lower cost. LinkedList is relatively slow in terms of random access. The iterator Iterator1, using method iterator () requires the container to return a iterator.iterator that will be ready to return the first element of the sequence. 2. Use next () to get the next element in the sequence. 3. Use Hashnext () to check if there are elements in the sequence. 4. Use remove () to delete the newly returned elements of the iterator. Set: Duplicate elements are not saved. HashSet: No sorting, use hash function. Provides the fastest query speed TreeSet: Storing elements in a red-black tree data structure with sort map: key-value pairs HashMap for quick access, TreeMap keep the key always in the sorted state. The stack stack usually refers to a last-in, first-out container (LIFO), which linkedlist the ability to implement all functions of the stack. Queue: Queues (FIFO) public static void Testlist () {list<integer> List = new arraylist<> (); List.add (1); List.add (2); List.add (3); List.add (4); List.add (5); List.add (6); List.add (7); System.out.println (List.tostring ()); Iterator it = List.iterator (); while (It.hasnext ()) {int i = (Integer) it.next (); System.out.println (i); } System.out.println (List.tostring ()); it = List.iterator (); for (int i = 0; i < 3; i++) {it.next (); It.remove (); } System.out.println (List.tostring ()); } public static void Testlinkedlist () {linkedlist<integer> list = new linkedlist<> (); List.add (1); List.add (2); List.add (3); List.add (4); List.add (5); List.add (6); List.add (7); System.out.println (List.tostring ()); System.out.println ("GetFirst ():" +list.getfirst ()); System.out.println ("GetLast ():" +list.getlast ()); System.out.println ("Element ():" +list.element ()); System.out.println ("Peek ():" +list.peek ()); System.out.println ("Remove ():" +list.remove ()); System.out.println ("Removefirst ():" +list.removefirst ()); System.out.println ("poll ():" +list.poll ()); System.out.println (List.tostring ()); System.out.println (List.pop ()); System.out.println (List.tostring ()); List.addfirst (8); List.offer (9); List.addlast (10); System.out.println (List.tostring ());List.push (11); System.out.println (List.tostring ()); List.pop (); System.out.println (List.tostring ()); } public static void TestMap () {map<integer,string> Map = new hashmap<> (); Map.put (1, "ten"), Map.put (2, "a"), Map.put (3, "a"), Map.put (4, "40"); System.out.println (Map.keyset ()); Get Key System.out.println (Map.values ()); Gets the value//KeySet for (Integer I:map.keyset ()) {//Gets the value according to the key System.out.println (i+ "" +map.get (i )); }//EntrySet Iterator Iterator = Map.entryset (). Iterator (); Gets the set while (Iterator.hasnext ()) {Map.entry Entry = (map.entry) iterator.next () of key and value; System.out.println (Entry.getkey ()); System.out.println (Entry.getvalue ()); }//KeySet Iterator it = Map.keyset (). Iterator (); while (It.hasnext ()) {int key = (int) it.next (); String value = Map.get (key); System.oUt.println (key+ "" +value); }}/** * queue: FIFO * offer (Object o) * POLL () */public static void Testqueue () {Queue Queu E = new LinkedList (); Queue.offer (1); Queue.offer (2); Queue.offer (3); Queue.offer (4); System.out.println (Queue.tostring ()); System.out.println (Queue.poll ()); System.out.println (Queue.tostring ()); }/** * Stack: Advanced post-out * push (Object 0) * POP () */public static void Teststack () {LinkedList stack = New LinkedList (); Stack.push (1); Stack.push (2); Stack.push (3); Stack.push (4); System.out.println (Stack.tostring ()); System.out.println (Stack.pop ()); System.out.println (Stack.tostring ()); }
Java Collection List,map,set,queue use