Java Core Technical notes for busy people (7, collection)

Source: Internet
Author: User

Points:

1. The collection interface provides common methods for all collection classes (except mappings, which are described by the map interface)

2, the list is an ordered set, where each element has an integer index

3. Set is optimized for efficient inclusion testing. Java provides the hashset and TreeSet implementations

4, for the mapping, you can choose HashMap or TREEMAP implementation. Linkedhashmap remembers the order in which elements are inserted.

5. Collection interface and collections class provide many useful algorithms: such as setting operation, querying, sorting, disrupting the original order, etc.

6. Views provide access to data stored elsewhere by using the standard collection interface

1. Introduction

ArrayList and LinkedList, one is a linked list, one is a linear table.

The list interface provides a way to access the nth element in the list, although this access may be somewhat inefficient. In other words, a collection class should implement the Randomaccess interface. This is a markup interface with no methods.

For example, ArrayList implements list and randomaccess, but LinkedList only implements the list interface.

Elements in set are not inserted into a particular location, and duplicate elements are not allowed. SortedSet allows sequential iterations, Navigableset contains methods to find the neighbor of the element.

The queue remembers the order of insertions, but only the elements are inserted at the end, and the headers are deleted. Deque (double-ended queue)

All of the collection interfaces are generic types with type parameters that represent the type of the element.

Useful methods for the collections class:

P237!

2. iterators

1 collection<string> coll = ...; 2 iterator<string> iter = coll.iterator (); 3  while (Iter.hasnext ()) {4    String element = iter.next ();
Iter.remove (); 5 }

The Remove method removes the last element returned by the iterator, not the element that the iterator points to. You cannot call remove twice in the middle without calling next or previous

1List<string> friends =NewLinkedlist<>();2Listiterator<string> iter =friends.listiterator ();3Iter.add ("a");//a|4Iter.add ("B");//a B |5Iter.previous ();//a|b6Iter.add ("C");//A | c7 //Listiterator with linked lists8 //add elements before the iteration, set the accessed element to a different value, backtrack backwards

If you use multiple iterators to access one data structure, one of which changes the structure of the data, other iterators may fail. An invalid iterator may throw concurrentmodificationexception if you continue to use it

3. Set

Set can efficiently detect whether a value is its element (including contains ()). However, the order in which elements are added is not remembered. Set is useful when the order does not matter.

If you want to facilitate the collection in order, you can use TreeSet. The elements of the set must implement the comparable interface, or provide comparator in the constructor

The TreeSet class implements the SortedSet and Navigableset interfaces

P241 SortedSet and Navigableset methods!

4. Map

Map stores the connection between the key and the value. Call the Put method to create a new contact, or change the value of the stored key (the Put method can update the value under the key).

Do not use HashMap in sequential access, if you want to order access with TreeMap

int count = Counts.get ("a"); Null pointer exception when the value is unboxing if the key does not exist get returns null

int count = Counts.getordefault ("A", 0);//Recommended Use

The counter, when present, adds one:

Counts.merge (Word, 1, integer::sum);

If the word key does not exist before, connect Word with 1, or use the Integer::sum function to add 1 to the previous value

P243 Map Method! I didn't read it.

1 // Iteration Map 2  for (map.entry<string, integer> entry:counts.entrySet ()) {3   String k = entry.getkey (); 4   Integer V = entry.getvalue ();     5 }6// or 7 Counts.foreach ((k,v)->{k,v processing});

5. Other collections

The properties class makes it easy to save and load mappings using plain text format. Typically used to store configuration options for a project.

1 New porperties (); 2 settings.put ("width", "$"); 3 settings.put ("title", "Hello"); 4 Try (OutputStream out = files.newoutputstream (Path)) {5     Settings.store (out, "Properties") ; 6 }

The properties file uses ASCII encoding instead of UTF-8, comments to # or! The word inode uniode escape format is not written in the range

1 // loading properties from a file 2 Try (InputStream in = files.newinputstream (path)) {3  settings.load (in);   4 }5 String tilte = Settings.getproperty ("Tilte", "New Document");

System.getproperties () Gets the System Properties object. System.getenv () Gets the Java Virtual machine environment variable.

  

Java Core Technical notes for busy people (7, collection)

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.