A simplified method of double bracket initialization using anonymous inner classes in Java _java

Source: Internet
Author: User
Tags anonymous

Java's collection collection framework, such as set, map, and list, does not provide any easy way to initialize. Each time you set up a collection, you add the values into it. Such as

Copy Code code as follows:

Set<character> letter=new hashset<character> ();
Letter.add (' a ');
Letter.add (' B ');
//...

Very cumbersome.

But with anonymous inner class words. It can be a little easier.

Copy Code code as follows:

Set<character> letter=new hashset<character> ()
{
{
Add (' a '); Add (' B '); Add (' C '); Add (' d ');
Add (' e '); Add (' f '); Add (' g '); Add (' H ');
Add (' I '); Add (' J '); Add (' K '); Add (' l ');
Add (' m '); Add (' n '); Add (' O '); Add (' P ');
Add (' Q '); Add (' R '); Add (' s '); Add (' t ');
Add (' U '); Add (' V '); Add (' W '); Add (' x ');
Add (' Y '); Add (' Z ');
Add (' A '); Add (' B '); Add (' C '); Add (' D ');
Add (' E '); Add (' F '); Add (' G '); Add (' H ');
Add (' I '); Add (' J '); Add (' K '); Add (' L ');
Add (' M '); Add (' N '); Add (' O '); Add (' P ');
Add (' Q '); Add (' R '); Add (' S '); Add (' T ');
Add (' U '); Add (' V '); Add (' W '); Add (' X ');
Add (' Y '); Add (' Z ');
}
}; The first bracket defines the anonymous inner class and the second layer is the initialization module

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.