Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 2
HashSet | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 17
HashSet
HashSetはSetインターフェースを実装したクラスで、値の挿入順を保持しません。
要素を取得する順序は順不同です。また、重複する要素の追加は許されません。
以下、インターフェースベースの手法で記述しています。

×HashSet set = new HashSet();
○Set set = new HashSet();
このようにSetインターフェースで実装することによって、後でHashSetではなく、LinkedHashSetなどの同じインターフェースに書き換えたりと、メンテナンスしやすくなります。

以下、サンプルです。

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Test {
    public static void main(String[] args) {

        Set set = new HashSet();
        set.add("2");
        set.add("1");
        set.add("1");
        set.add("3");
        set.add("1");
        set.add("4");
        
        Iterator itr = set.iterator(); 
        while (itr.hasNext()) {
            String element = (String)itr.next();
            System.out.println(element);
        }  
    }
}
実行結果は以下のようになります。

3
2
4
1

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 94

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 94

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 103

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/set/hashset/hashset.html on line 103