Think In Javaノート7 Holding Your Objects


Collection 1.Collectionは、リストなどのすべてのシーケンスのコンテナのroot interfaceです.
Collection<Integer> c = new ArrayList<Integer>();
  • タイプは
  • あります
    ArrayList<String>()
    LinkedList<String>()
    HashSet<String>()
    TreeSet<String>()
    LinkedHashSet<String>()
  • はaddで値を保存し、getで値Map
  • を取る.
  • MapのタイプはHashMap
  • です
    public Iterator<Pet> iterator() {
        return new Iterator<Pet>() {
            private int index = 0;
            public boolean hasNext() {
                return index < pets.length;
            }
            public Pet next() { return pets[index++]; }
            public void remove() { // Not implemented
                throw new UnsupportedOperationException();
            }
        };
    }
  • クラスに順方向のIteratorがある場合は、逆方向のreverseを逆方向のIteratorとして定義します.書き方は順方向のIteratorと似ています.ListIterator
  • 特性(1)双方向(2)前と後のインデックスが分かる(3)現在の項目をsetで置き換えることができる(4)listIterator(n)を用いてn番目からLinkedList
  • を開始することができる
  • getFirst()and element()はいずれも最初の要素を返し、空の場合throw NoSuchElementException peek()も最初の要素を返しますがnullを返しますlistが空の場合
  • を返します.
  • removeFirst()and remove()はいずれも最初を削除して最初の要素を返し、空であればthrow NoSuchElementException poll()も最初を削除し、空であればnull
  • を返します.
  • offer()is the same as add()and addLast()の3つは、list Stack Set
  • を末尾に挿入する
  • TreeSetのデフォルト構造は辞書順で、大文字と小文字が分かれており、String.CASE_INSENSITIVE_ORDER Comparator、大文字と小文字を敏感にしない
  • 普通のsetはsortedのない重複のない集合Map
  • である.
  • int、doubleなどの基本タイプは、containerでは基本タイプが使用できないため、自動的にIntegerまたはDoubleタイプに変換されます.
  • get(key)keyが存在する場合は対応するvalueを返し、そうでない場合null
  • を返す
  • はput(key,value)で値
  • を設定する.
  • このようなpublic static Map