Java set (bottom)--the set frame and algorithm to explain __ algorithm

Source: Internet
Author: User
Tags addall data structures stub wrapper
Java Set (bottom)--the set frame and algorithm detailed

A framework is a set of classes that have many super classes and interfaces that implement many of the advanced mechanisms, functions, and policies in these superclass classes. Users of the framework can create subclasses to implement and extend the superclass without having to recreate these basic mechanisms. In our day-to-day work, the techniques we use are basically frameworks, and we use those packages to invoke those functions when we call them. After analyzing the data structure of the set in the set (i), today we will go on to discuss the framework of the set. (i) Collection data structure review

Basic type implementing Interfaces Description
List Linked list LinkedList Deque,list,queue The bidirectional linked list is realized by storing the reference of the front and back node.
Array list ArrayList List,randomaccess Automatically expands the size of an array in a dynamic array of data
Set Hash set HashSet Set Hashing to store data, but it's more efficient when it's unordered but lookup
Tree Set TreeSet Navigableset,set,sortedset Sort by a certain method, outputting a sequential set
Queue Priority Queue Priorityqueue Queue Queue tree sorted by heap method
Two-terminal queue Arraydeque Deque, Queue Can be added and removed at both ends, unable to manipulate the middle queue
Map Hash table HashMap Map Table of key-value mappings stored with a hash method
Tree Table TreeMap Map,navigablemap,sortedmap A table that sorts keys in a certain way
Note: The first six (excluding map) implements the collection and iterator interfaces because the space limit is not written out.
(ii) View

In the Collection class library, we have used a very large proportion to build the interface of the implementation class, so what is the use of these interfaces? If we directly write the method in the interface in the implementation class does not have the same effect. The reality is far from simple as we think, and there are a lot of complicated things we need to do with these interfaces. For example, we refer to objects of the public interface type of certain classes as view objects. The type of these objects is not a specific implementation class, but rather a public interface for implementing classes. Views have many functions, mainly the following: 1. View is a lightweight set wrapper

We can wrap an ordinary array into a View object of type list by means of a method. The type of the View object is list, which may sound strange, because the list is an interface and cannot be instantiated. But this is the advantage of the View object, which can be assigned to any collection class that implements the list interface, such as Common ArrayList, LinkedList, and so on, to the purpose of wrapping a common class into a collection class.

Package setviews;
Import Java.util.Arrays;
Import java.util.Collections;

Import java.util.LinkedList; /** * * @author Quinnnorris * View is a lightweight set wrapper with two examples * * * public class Views {/** * @param args * p Ublic static void Main (string[] args) {//TODO auto-generated Method Stub//-----------------------Example 1----
        ------------------int[] arr = new INT[10];
        Creates an ordinary int array arrays.aslist (arr);

        Using the Aslist method, we convert an ordinary array into a list-type View object. 
        -----------------------Case 2-----------------------list<string> ll = new linkedlist<> ();
        Create a LinkedList object LL.
        Ll.addall (collections.ncopies, "OK"));
        Ncopies is a static method of the collections class, you can copy "OK" 10 times and return a list-type View object.

        Here we can not directly convert the type of collections.ncopies to LinkedList, because the syntax can not be turned down//We will fill this content into the LinkedList object via the AddAll method.
        Ll.set (2, "no");
        Let's see if it works, we try to replace the string in the third list with No.
       Note that the list is best not to use get and set methods, just for experimentation. for (String s:ll) {System.out.println (s);
    //Output The third is no, the other is OK, there is no problem.
 }

}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 A.

This is a great advantage of the view, which is a lightweight array wrapper that wraps the array as a class object in the collection class library. Again, we can put those key-value pairs, the data that needs to be stored in the set ... are packaged through the view and passed into the appropriate class. So, we call this view's function a lightweight set wrapper. 2. Isolate a child range from a class by view

You can establish a child-scoped view of many collections that are a reference to an existing collection, and that if modified, these child scopes affect the original collection. If you do not want to have an impact, we need to use other similar addall methods to do replication. In the list, the method of splitting the example range is simple.

Package setviews;

Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;

/**
 * * 
 * @author quinnnorris
 * In the list of example scope, and operation/public
class Subviews {

    /**
     * @param Args
     */public
    static void Main (string[] args) {
        //TODO auto-generated method stub

        List<integer > al = new arraylist<> ();
        Create a ArrayList object Al
        Al.addall (collections.ncopies (1));
        Set 15 1
        List Subal = al.sublist (5) in Al;
        Sublist takes out the 6th element to the 10th element as a child range, returns a View object, and we store it in the Subal.
        subal.set (0, 0);
        The 6th number is changed to 0 for

        (Integer i:al)
            System.out.println (i);
        Except the 6th number is 0 other all 1

        subal.clear ();//clear Child range for

        (Integer i:al)
            System.out.print (i);
        There are only 10 1 in the Al list, and five in the middle are cleared
    }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

In addition, in other ordered sets and mapping tables, we can use the sort order instead of the element position to create a child scope. These methods are stated in SortedSet and SortedMap.

Sortedset<e> Headset (E toelement)
Returns a partial view of this set whose elements are strictly less than toelement.

Sortedset<e> subset (e fromelement, E toelement)
Returns a partial view of this set whose elements are from fromelement (including) to Toelement (not included).

Sortedset<e> Tailset (E fromelement)
Returns a partial view of this set whose element is greater than or equal to fromelement.

The above three methods are the methods declared in SortedSet and we are available in TreeSet. Similarly, if you change all of the set in the method to map, we can use the same three methods in TreeMap, and note that all the actions in the map are performed with a key, not a value. 3. Non-modifiable security view

In real life, if you put your collection object into your colleagues ' methods, you will find that he incorrectly modifies your object, which is bound to annoy you. So how do we solve these problems and make our data structures more secure? In the view, we can lock our object with a non modifiable view, and if we find an attempt to modify the collection, throw an exception and restore the collection back to the unmodified state.

In the collections class, we provide several ways to get a view that cannot be modified:

Unmodifiablecollection: Returns the collection view (same below) of the specified.
Unmodifiablelist
Unmodifiablemap
Unmodifiableset
Unmodifiablesortedmap
Unmodifiablesortedset

These methods are very simple and clear, so let's look at a concrete example of how these methods work.

Package setviews;

Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;

/**
 * * 
 @author quinnnorris
 * Non-modifiable security View Example
/public class Unmodifaiableviews {

    //This is the method of invoking the list
    private static void

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.