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('a'); letter.add('b'); //...
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('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 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!