java advanced level 7 (set advanced operation)

JavaSE advanced day 7

Syllabus:

  • aggregate

1, Collection architecture

1. Current column set

1.1 Collection interface

1.1.1 general

1.Is the top-level interface of a single column collection. It represents a set of objects, also known as Collection Element of
2.JDK Instead of providing any direct implementation of this interface, it provides more specific sub interfaces (such as set and List)realization

1.1.2 create object of Cokkection set

  • Polymorphic way
  • The specific implementation class is ArrayList
|-List Interface: it can store duplicate elements, have indexes, and ensure orderly access
    |-ArrayList Class: the bottom layer is an array
    |-LinkedList Class: the bottom layer is a two-way linked list
    |-Vector Class: the bottom layer is an array
|-Set Interface: duplicate elements cannot be stored, there is no index, and access order is not guaranteed
    |-TreeSet class
    |-HashSet class
 Collection<String> collection = new ArrayList<>();
 Collection<String> collection = new ArraySet<>();

1.1.3 membership method

//  Boolean add (E) add element
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");
collection.add("cccc");
System.out.println(collection);

//        boolean remove(Object o) removes (deletes) the specified element from the collection
//        If the deletion is successful, return true
//        If the deletion fails, false is returned
boolean result = collection.remove("aaa");
boolean result2 = collection.remove("ddd");
System.out.println(result);
System.out.println(result2);
System.out.println(collection);

//        boolean removeif(Object o) deletes according to conditions
//         boolean test(String t);
collection.removeIf(
    (String s) -> {
        return s.length() == 3;
    }
);
System.out.println(collection);

//        void clear clear collection
collection.clear();
System.out.println(collection);

//    boolean contains(Object o) determines whether the specified element exists in the collection
boolean result1 = collection.contains("a");
System.out.println(result1);
boolean result2 = collection.contains("aaa");
System.out.println(result2);

//        boolean isEmpty() determines whether the collection is empty
boolean result = collection.isEmpty();
System.out.println(result); //false
collection.clear();
boolean result2 = collection.isEmpty();
System.out.println(result2);

//        int size() the length of the set, that is, the number of elements in the set
System.out.println(collection.size());

//      Object[] toArray(): convert a collection into an array and fix it to get an array of object type
//    <T> T [] toArray (t [] a): convert a collection into an array to obtain an array of the specified type
Object[] objects = collection.toArray();
System.out.println("Array length is:"+objects.length);
for (Object object : objects) {
    System.out.println(object);

1.1.4 precautions and interview questions

Collection There is no index related method in the interface
 
Interview question: the following methods belong to Collection The method in is( A)
    A,boolean add(E e)
    B,E get(int index)
    C,E set(int index, E e)
    D,E remove(int index)

2. Traversal of collection

2.1 iterators

2.1.1 definitions

In fact, it is a tool specially used to traverse a single column set

2.1.1 construction method

//        1. Or get the object of the iterator
//        Once the iterator object is created, it points to the 0 index of the collection by default
		Iterator<String> it = list.iterator();
//        Iterator: creates an iterator object

2.1.2 membership method

boolean hasNext()
    If there are still elements to iterate over, returns true. 
E next()
    Returns the next element of the iteration.
void remove()
    Point from iterator collection Optionally, remove the last element returned by the iterator.
Collection<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
//        1. Or get the object of the iterator
//        Once the iterator object is created, it points to the 0 index of the collection by default
Iterator<String> it = list.iterator();
//        Iterator: creates an iterator object
//        Use the methods in the iterator to traverse
System.out.println(it.hasNext());   //Is there any element in the current position that can be taken out
System.out.println("Element at current location:"+it.next());//Take out the element of the current position + move the iterator to the position of the next index
while (it.hasNext()){
    System.out.println(it.next());
}

// Iterator traversal code
Iterator<String> it = list.iterator();
while(it.hasNext()){
    String s = it.next();
}
//Delete an element with an iterator
  ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("d");
 Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            if ("b".equals(s)){
                 it.remove();
            }
        }
        System.out.println(list);

2.2 enhanced for loop

2.2.1 definitions

Enhanced for: simplifies traversal of arrays and Collection collections

  • It appeared after JDK5, and its internal principle is an Iterator iterator
  • Only classes that implement the Iterable interface can use iterators and enhance for

2.2.2 format

  for(Data type variable name:Single column collection or array){
		//Just use the variable here, which is the element
  }

        for(String s:list){
        }

// Enhanced for is the syntax sugar of iterators
// Shortcut key: iter and enter or. for and enter

2.3 traversal mode of set

  • If you need to manipulate the index, use the normal for loop
  • If you need to delete elements during traversal, use iterators
  • If you just want to traverse, use enhanced for
1,ordinary for,combination get(int index)and size()method     [Only for List Set under system]

2,iterator                                   [Applicable to all Collection aggregate]
3,enhance for                                [Applicable to all Collection aggregate]

2, List interface

1. General

  • Ordered set, where order refers to access order
  • The user can precisely control the insertion position of each element in the list. The user can access the element through the integer index and search the element in the class table

2. Features

  • Ordered: the stored and retrieved elements are in the same order
  • Indexed: elements can be manipulated by indexing
  • Repeatable: stored elements can be repeated
 List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("world");
        System.out.println(list.toString());

        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            System.out.println(s);
        }

        System.out.println("------------");

        for (String s : list) {
            System.out.println(s);
        }

    }

3. Unique methods in list interface

	void add(int index, E element)
    E remove(int index)
    E set(int index, E element)
    E get(int index)
  List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("world");
        list.add("removeme");
// Void add (int index, e element) inserts the specified element at the specified position in this collection
        list.add(0,"Andy");
        System.out.println(list.toString());

//   E set (int index, e element) modifies the element at the specified index and returns the modified value
        list.set(1,"andy");
        System.out.println(list.toString());

//        E remove (int index) deletes the element at the specified index and returns the deleted element,
//        There are two deletion methods in the List collection
//        The other is e remove (object), which returns a boolean type
        boolean removeme = list.remove("removeme");
        String remove = list.remove(0);
        System.out.println(remove); //Returns the deleted element
        System.out.println(removeme);   //Returns a boolean type
        System.out.println(list.toString());

//        list.remove(2);
//        E get (int index) returns the element at the specified index
        System.out.println(list.get(0));
        System.out.println(list.toString());

4. Summary

	in the light of List Selection of three common implementation classes under
    In most cases, choose ArrayList
    There are only a lot of additions and deletions to choose from LinkedList
    Absolutely not Vector,It will only appear in old code and interview questions

5.List set implementation class

List collection common subclasses: ArrayList,LinkedList

  • ArrayList: the underlying data structure is an array, with fast query and slow addition and deletion
  • LinkedList: the underlying data structure is a linked list, with slow query and fast addition and deletion

6.LinkedList specific member method

  • public void addFirst (E) inserts the specified element at the beginning of the list
  • public void addLast (E) appends the specified element to the end of this list
  • public E getFirst() returns the first element in this list
  • public E getLast() returns the last element in this list
  • public E removeFirst() removes from this list and returns the first element
  • public E removeLast() removes from this list and returns the last element
  LinkedList<String> list = new LinkedList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
  //Inserts the specified element at the beginning of the list
        list.addFirst("andy");
        //        list.add(0,"amdy");
        System.out.println(list);

 //        Inserts the specified element at the end of the list
        list.addLast("www");
        System.out.println(list);

 //        Returns the first element in the list
        String first = list.getFirst();
        System.out.println(first);

 //        Returns the last element in the list
        String last = list.getLast();
        System.out.println(list);

//        Delete the first element and return it
        String s = list.removeFirst();
        System.out.println(s);
        System.out.println(list);

 //        Delete the last element and return it
        String s = list.removeLast();
        System.out.println(s);
        System.out.println(list);

3, Vector class

1. Interview questions

What is the difference between Vector and ArrayList?

1,ArrayList The method name has been improved to make it simpler Vector The method name of is long
2,ArrayList It is thread unsafe (asynchronous) and efficient Vector It is thread safe (synchronous) and inefficient

Conclusion: vector will not be used in future development. Vector usually lives in old code or interview questions because ArrayList replaces vector

 Vector<String> list = new Vector<>();
        list.add("hello");
        list.add("world");
        list.add("yes");

        Enumeration<String> elements = list.elements();
        while (elements.hasMoreElements()) {
            String s = elements.nextElement();
            System.out.println(s);
        }

2.Enumeration class

2.1 description

Enumeration, old iterator
Enumeration<String> e = list.elements();
while(e.hasMoreElements()){// it.hasNext()
    String s = e.nextElement();// it.next()
    System.out.println(s);
}

2.2 difference between enumeration and Iterator

  • Iterator has improved the method name, and the Enumeration is longer
  • Iterator has a remove method, and Enumeration is missing a remove method

Summary: Iterator replaces Enumeration

4, Data structure

1. Definitions

Data structure is a way for computers to store and organize data. It refers to the collection of data elements with one or more specific relationships. Generally, carefully selected data structures can bring higher operation or storage efficiency

  • Stack
  • queue
  • array
  • Linked list
Stack: first in, last out
 Queue: first in first out
 There was a dead end. Three cars stopped( ABC),A Go first, B Go in again, C Finally, go in and ask how to get out? CBA        Stack structure
 When the train passes through the tunnel, the front goes first, the front goes out of the tunnel first, the rear goes in, and the rear goes out of the tunnel                            Queue structure
 Array: query modification is fast, addition and deletion is slow
 Linked list: fast addition and deletion, slow query and modification

ts()){// it.hasNext()
String s = e.nextElement();// it.next()
System.out.println(s);
}



### 2.2 difference between enumeration and Iterator

- Iterator The method name has been improved,Enumeration longer 
- Iterator have remove method,Enumeration One is missing remove method

Summary: Iterator Replaced Enumeration





# 4, Data structure

## 1. Definitions

The data structure is computer storage,How data is organized. It refers to the collection of data elements with one or more specific relationships between them. Usually,Carefully selected data structures can lead to higher operation or storage efficiency



- Stack
- queue
- array
- Linked list

````java
 Stack: first in, last out
 Queue: first in first out
 There was a dead end. Three cars stopped( ABC),A Go first, B Go in again, C Finally, go in and ask how to get out? CBA        Stack structure
 When the train passes through the tunnel, the front goes first, the front goes out of the tunnel first, the rear goes in, and the rear goes out of the tunnel                            Queue structure
 Array: query modification is fast, addition and deletion is slow
 Linked list: fast addition and deletion, slow query and modification

Tags: Java linked list set

Posted on Fri, 19 Nov 2021 19:01:46 -0500 by rohithreddyk