Home > Java > javaTutorial > In java, use anonymous inner classes to perform simpler double bracket initialization method

In java, use anonymous inner classes to perform simpler double bracket initialization method

巴扎黑
Release: 2017-05-21 14:05:03
Original
1556 people have browsed it

The editor of this article will introduce to you about the simpler double bracket initialization method using anonymous inner classes in java. Friends in need can refer to

java's collection collection framework, such as Set, map, and list do not provide any convenient methods for initialization. Every time you create a collection, you have to add the values ​​one by one. For example, the code for

is as follows:

Set<Character> letter=new HashSet<Character>();
letter.add(&#39;a&#39;);
letter.add(&#39;b&#39;);
//...
Copy after login

is very cumbersome.

But use anonymous inner classes. Could be a little simpler.

The code is as follows:

Set<Character> letter=new HashSet<Character>()
  {
   {
    add(&#39;a&#39;); add(&#39;b&#39;); add(&#39;c&#39;); add(&#39;d&#39;);
       add(&#39;e&#39;); add(&#39;f&#39;); add(&#39;g&#39;); add(&#39;h&#39;);
       add(&#39;i&#39;); add(&#39;j&#39;); add(&#39;k&#39;); add(&#39;l&#39;);
       add(&#39;m&#39;); add(&#39;n&#39;); add(&#39;o&#39;); add(&#39;p&#39;); 
       add(&#39;q&#39;); add(&#39;r&#39;); add(&#39;s&#39;); add(&#39;t&#39;); 
       add(&#39;u&#39;); add(&#39;v&#39;); add(&#39;w&#39;); add(&#39;x&#39;);
       add(&#39;y&#39;); add(&#39;z&#39;);
       add(&#39;A&#39;); add(&#39;B&#39;); add(&#39;C&#39;); add(&#39;D&#39;);
       add(&#39;E&#39;); add(&#39;F&#39;); add(&#39;G&#39;); add(&#39;H&#39;);
       add(&#39;I&#39;); add(&#39;J&#39;); add(&#39;K&#39;); add(&#39;L&#39;);
       add(&#39;M&#39;); add(&#39;N&#39;); add(&#39;O&#39;); add(&#39;P&#39;);
       add(&#39;Q&#39;); add(&#39;R&#39;); add(&#39;S&#39;); add(&#39;T&#39;);
       add(&#39;U&#39;); add(&#39;V&#39;); add(&#39;W&#39;); add(&#39;X&#39;);
       add(&#39;Y&#39;); add(&#39;Z&#39;);
   }
  };  //第一层括号为定义匿名内部类,第二层则为初始化模块
Copy after login

The above is the detailed content of In java, use anonymous inner classes to perform simpler double bracket initialization method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template