Think in Java (8): Holding object, thinkjava

Source: Internet
Author: User

Think in Java (8): Holding object, thinkjava
1. Differences between various maps
HashMap: Does not store its elements in any obvious Order (same as HashSet)
TreeMap: stores keys in ascending order of comparison results (same as TreeSet)
LinkedHashMap: stores keys in the insert Order (same as LinkedHashSet)

2. ListIterator is a more powerful sub-type of Iterator. Iterator can only move forward, but ListIterator can move in two directions.

Public class ListIteration {public static void main (String [] args) {List <Pet> pets = Pets. arrayList (8); ListIterator <Pet> it = pets. listIterator (); // Forwards: while (it. hasNext () {System. out. print (it. next () + "," + it. nextIndex () + "," + it. previusindex () + ";") ;}// Backwards: while (it. hasPrevious () {System. out. print (it. previous (). id () + "");}}}

3. The elements in the TreeSet are sorted in ascending Lexicographic Order by default. Of course, we can input a comparator to customize the sorting.

Public class UniqueWordsAlphabetic {public static void main (String [] args) {Set <String> words = new TreeSet <String> (String. CASE_INSENSITIVE_ORDER); words. add ("a"); words. add ("c"); words. add ("B"); System. out. println (words );}}



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.