This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

⭐ August more text challenge day 18 ⭐, review and consolidate Java😁 with friends

Code shrimp is a sand carving and funny boy who likes listening to music, playing games and writing as well as most of his friends. The days are still very long, let’s refuel our efforts together 🌈

Welcome to follow my public account: JavaCodes, the name is with Java but the scope can be more than Java field oh 😁, will share blog posts or welfare for a long time, looking forward to your attention ❤

🌈Java✨ learning notes ✨ (11)⏩ Collection (Collection, Map, Set, List)

😁 offer yourself

To everyone recommend their own column 😁, welcome small partners to collect attention 😊

Java column

MybatisPlus column

App crawler Column

PC side crawler column

Big factory interview question column

1. Overview of collection

What is a set? What’s the use?

A set is really just a container. Can be used to hold other types of data

Why are collections used more in development?

A collection is a container, a carrier, that can hold multiple objects at once. In the actual development, it was assumed that connect to the database, the database of 10 records, so suppose this article 10 records query, in Java programs will be 10 data encapsulation into 10 Java objects, and then put 10 Java objects in a collection of the collection to the front end, a data one by one.

What does a collection store?

Collections do not store basic data types directly, and collections do not store Java objects directly. Collections store the memory addresses of Java objects. (or the time reference stored in the collection).

Which package does the collection come under in the Java JDK?

Under the java.util.* package, all collection classes and collection interfaces are under the java.util package

Set frame diagram:


2. Common methods of Collection interface

What elements can a Collection hold?

Before “generics” is used, a Collection can hold all subtypes of an Object.

With generics, only a specific type can be stored in a Collection.


2.1. Add () and remove() methods

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(new Object());
collection.add(true);
System.out.println(collection);
Copy the code

Autoboxing (a new feature in java5) actually puts in the memory address of an object

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(new Object());
collection.add(true);

/ / remove
collection.remove(100); 
collection.remove(true);

System.out.println(collection);
Copy the code


2.2 size() method

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(new Object());
collection.add(true);

System.out.println(collection.size());
Copy the code


2.3. Clear () method

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(new Object());
collection.add(true);

collection.clear(); / / to empty
System.out.println(collection.size());
Copy the code


2.4 contains() method

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(new Object());
collection.add(true);


System.out.println(collection.contains(100));
System.out.println(collection.contains(3));
Copy the code


2.5. IsEmpty () method

Determines whether the number of elements in the set is 0

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(new Object());
collection.add(true);


System.out.println(collection.isEmpty());
// Clear the collection
collection.clear();
System.out.println(collection.isEmpty());
Copy the code


3. Iteration of Collection

** A Java Iterator is not a collection. It is a method for accessing collections that can be used to iterate over collections like ArrayList and HashSet.

Iterator is the simplest implementation of a Java Iterator. ListIterator is an interface in the Collection API that extends the Iterator interface **

Iterator Iterator mode 1

 Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(true);

// Get the iterator object
Iterator it = collection.iterator();

System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
Copy the code

An error is reported if it is out of range

Iterator Traversal Method 2 (not recommended)

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(true);
// Get the iterator object
Iterator it = collection.iterator();

for (int i = 0; i < collection.size(); i++) {
	System.out.println(it.next());
}
Copy the code

Iterator Iterator 3 (recommended)

Collection collection = new ArrayList();

collection.add(100);
collection.add(3.14);
collection.add(true);
// Get the iterator object
Iterator it = collection.iterator();

//hasNext() continues if it has the next one, exits if it doesn't
while (it.hasNext()) {
	System.out.println(it.next());
}
Copy the code


3.1 Iterator principle

Get the iterator from iterator() and loop while using hasNext(). If there is one, continue. If not, exit


4, List interface common methods

4.1. Add () and remove() methods

List list = new ArrayList();

list.add("a");
list.add("b");
list.add("c");
list.add("d");

list.add(1."Leathery shrimp");

Iterator iterator = list.iterator();

while (iterator.hasNext()) {
    System.out.println(iterator.next());
}
Copy the code

The add(int index, E Element) method is not used much because it is inefficient.

List list = new ArrayList();

list.add("a");
list.add("b");
list.add("c");
list.add("a");

System.out.println(list.size());
list.remove(1);
System.out.println(list.size());
Copy the code


4.2. Get () method

List list = new ArrayList();

list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add(1."Leathery shrimp");

System.out.println(list.get(1));
Copy the code


4.3, indexOf()lastIndexOf() and methods

List list = new ArrayList();

list.add("a");
list.add("b");
list.add("c");
list.add("a");

System.out.println(list.indexOf("A"));

// Get the index of the last occurrence of a
System.out.println(list.lastIndexOf("a"));
Copy the code


4.4. Set () method

List list = new ArrayList();

list.add("a");
list.add("b");
list.add("c");
list.add("a");

list.set(0."A");

for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
Copy the code


5, Set

Set is a subinterface of Collection. Seti == does not provide additional methods ==

A Set cannot contain identical elements. If you add two or more identical elements to the same Set, the add operation fails.

Set determines whether two objects are the same not by using the == operator, but by equals()


5.1, HashSet

== is the main implementation class for Set: it is not thread-safe to store null values ==

Set<Object> objects = new HashSet<>();

objects.add(true);
objects.add("hello3");
objects.add("hello1");
objects.add("hello4");
objects.add("hello2");
objects.add("hello1");
objects.add(18);

System.out.println(objects);
Copy the code

==1. Disorder: Not equal to randomness. The data stored in the underlying array is not added in the order of the index, but is determined by the hash value of the data

2. Non-repeatability: Ensure that added elements cannot return true according to equals(), i.e. only one == can be added to the same element


5.2, LinkedSet

A subclass of HashSet that traverses its internal data in the order in which it was added

Set<Object> list = new LinkedHashSet<>();

list.add(true);
list.add("hello3");
list.add("hello1");
list.add("hello4");
list.add("hello2");
list.add("hello1");
list.add(18);

System.out.println(list);
Copy the code

LinkedHashSet is a subclass of HashSet, and while adding data, each data maintains two references to the data before and after it


6. Map common methods

1. Map and Collection have no inheritance relationship. 2. Map sets store data in the form of ==Key== and ==Value== : both Key and Value refer to data types. Key and Value are the memory addresses of stored objects, and Value is an accessory of Key


6.1. Put () method

Map<Integer,Object> map = new HashMap<>();

map.put(1."Zhang");
map.put(2."Bill");
map.put(3."Fifty");

System.out.println(map);

Copy the code


6.2 containsKey() and containsValue() methods

Map<Integer,Object> map = new HashMap<>();

map.put(1."Zhang");
map.put(2."Bill");
map.put(3."Fifty");
map.put(4."Fifty");


System.out.println(map.containsKey(3));
System.out.println(map.containsKey(5));
System.out.println(map.containsValue("Fifty"));
System.out.println(map.containsValue("Leathery shrimp"));
Copy the code


6.3 remove() and get() methods

Map<Integer,Object> map = new HashMap<>();

map.put(1."Zhang");
map.put(2."Bill");
map.put(3."Fifty");

map.remove(2);
System.out.println(map);
Copy the code

Delete key-value pairs by key


6.4 Map Traversal (Important)

Method 1: Map traversal using entries in a for loop

Map<Integer,Object> map = new HashMap<>();

map.put(1."Zhang");
map.put(2."Bill");
map.put(3."Fifty");

for (Map.Entry<Integer,Object> entry: map.entrySet()) {
	System.out.println(entry);
}
Copy the code

This method is efficient because both keys and values are obtained directly from node objects

Method 2: Iterate through keys or values in the for loop. This method is generally used when only keys or values in the map are required. This method has better performance than entrySet.

Map<Integer,Object> map = new HashMap<>();

map.put(1."Zhang");
map.put(2."Bill");
map.put(3."Fifty");

for (Integer key : map.keySet()) {
	System.out.println(key);
}


for (Object value : map.values()) {
	System.out.println(value);
}
Copy the code

Method 3: Iterate through iterators

Map<Integer,Object> map = new HashMap<>();

map.put(1."Zhang");
map.put(2."Bill");
map.put(3."Fifty");

Iterator<Map.Entry<Integer, Object>> iterator = map.entrySet().iterator();

while (iterator.hasNext()) {
    System.out.println(iterator.next());
}
Copy the code


❤ finally

I am aCode pipi shrimp, a prawns lover who loves to share knowledge, will update useful blog posts in the future, looking forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == one key three even oh! Thank you for your support. See you next time