Analysis: If you don't know how many objects you need when the program runs, or if you need to store objects in a more complex way, you can use the Java collection framework.
If the deletion method of the collection is enabled, the indexes of all elements in the collection are automatically maintained.
The collection completely compensates for the shortcomings of the array.
02. Contents of collection frameworks
The collection framework all contains three major contents: external interfaces, interface implementations and algorithms for collection operations.
01. Interface: The abstract data type representing the collection
02. Implementation: Specific implementation of interfaces in collection framework
03. Algorithm: Complete some useful calculation method on an object that implements an interface of a certain collection framework.
Java collection framework diagram:
01. The Collection interface contains a group of ununique (repeated) and unordered objects.
02.Set interface inherits the Collection interface and stores a set of unique (repetition is not allowed) and unordered objects.
03. The List interface inherits the Collection interface and stores a set of objects that are not unique (repeat repetition allowed), ordered (place elements in the order of element insertion, and will not be rearranged)
04. The Map interface stores a pair of key-value objects, providing a mapping of key to value. The keys in the map do not require order and repetition is not allowed. The value also does not require order, but repetition is allowed.
05. The Iterator interface is the interface responsible for defining access and traversing elements.
Let’s start with the introduction:
1.List interface
The List interface inherits the Collection interface and stores a set of objects that are not unique (repeatable) and are ordered (place elements in the order of element insertion, and will not be rearranged)
Common classes that implement List interfaces include ArrayList and LinkedList
ArrayList encapsulates arrays and implements arrays of variable lengths
LinkedList uses linked list storage method, and its advantage is that it is relatively efficient when inserting and deleting elements.
01.ArrayList collection class
package cn.day001;public class Penguin { private String name="Anonymous"; private String sex="Q"; public Penguin() { } public Penguin(String name, String sex) { this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }Test class:
package cn.day001;import java.util.ArrayList;import java.util.Iterator;import java.util.List;//On-machine 1 ArrayList collection class public class Test { public static void main(String[] args) { //1. Create multiple penguin objects Penguin penguin=new Penguin("Nannan","Qzi"); Penguin penguin2=new Penguin("Huahua","Qmei"); Penguin penguin3=new Penguin("Zhezhe","Qmei"); //2. Create an Arraylist collection object and put 2 penguin objects into it List<Penguin> penguins=new ArrayList<Penguin>(); penguins.add(penguin); penguins.add(penguin2); penguins.add(penguin3); //3. The number of penguins in the output set System.out.println("Total" + penguins.size()+"penguin"); //4. Display for (int i = 0; i < penguins.size(); i++) { Penguin pg=(Penguin)penguins.get(i); System.out.println(pg.getName()+"/t"+pg.getSex()); } //5. Delete the zhe Penguin penguins.remove(penguin3); //6. Determine whether there is still zhe Penguin in the set if (penguins.contains(penguin3)) { System.out.println("existence zhe Penguin"); }else { System.out.println("existence zhe Penguin"); } System.out.println(); //7. Use iterator to iterate over System.out.println("using iterator"); Iterator<Penguin> its=penguins.iterator(); while (its.hasNext()) { Penguin pg =its.next(); System.out.println("name"+pg.getName()+"/t gender"+pg.getSex()); } System.out.println(); //8. Use foreach to traverse System.out.println("use foreach to traverse"); for (Penguin pg : penguins) { System.out.println("name"+pg.getName()+"/t gender"+pg.getSex()); } }}02.LinkedList collection class
package cn.day001;import java.util.LinkedList;//Test multiple special methods of LinkedList public class Test2 { public static void main(String[] args) { //1. Create multiple penguin objects Penguin penguin=new Penguin("Nannan","Qzi"); Penguin penguin2=new Penguin("Huahua","Qmei"); Penguin penguin3=new Penguin("Zhezhe","Qmei"); //2. Create an Arraylist collection object and put two penguin objects into it //List<Penguin> penguins=new ArrayList<Penguin>(); LinkedList<Penguin> penguins=new LinkedList<Penguin>(); penguins.add(penguin); penguins.add(penguin2); penguins.add(penguin3); //3. Output the number of penguins in the set System.out.println("Total" + penguins.size() + "penguin"); //4. View the nickname of the first penguin in the set Penguin firstpenguins=penguins.getFirst(); System.out.println("The nickname of the first penguin is: "+firstpenguins.getName()); //5. Check the nickname of the last penguin in the collection Penguin lastpenguins=penguins.getLast(); System.out.println("The nickname of the last penguin is: "+lastpenguins.getName()); //6. Delete the first penguin and the last penguin penguins.removeFirst(); penguins.removeLast(); System.out.println("Number of penguins after deletion"+penguins.size()); }} 2.Map interface
01.HashMap collection class
package cn.day002;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Scanner;import java.util.Set;//On-computer2 Find pets on the computer based on the pet nickname 3 Use iterator to iterate public class Test { public static void main(String[] args) { Map<String, String> pet=new HashMap<String, String>(); pet.put("Hehe", "Dog"); pet.put("Haha", "Penguin"); pet.put("Haha", "Cat"); Scanner input=new Scanner(System.in); System.out.println("Please enter the pet nickname: "); String name=input.next(); if (pet.containsKey(name)) { String petname=pet.get(name); System.out.println(name+"corresponding pet"+petname); }else { System.out.println("Sorry, there is no pet corresponding to this nickname"); } System.out.println(); //Iterate over System.out.println("Use iterator traversal"); Set<String> keys=pet.keySet();//Fetch out all key values Iterator<String> it=keys.iterator();//Get the Iteratoer object while (it.hasNext()) { String key = (String) it.next();//Fetch out the key value String pets=pet.get(key);//Fetch out the corresponding value according to the key System.out.println("key value: "+key+"/tvalue value: "+pets); } }}The above article in-depth analysis of the collection framework in Java is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.