logo

Java でのリストの初期化

Java.util.List の子インターフェイスです コレクション 。これは、重複した値を格納できる、順序付けられたオブジェクトのコレクションです。 List は挿入順序を保持するため、位置へのアクセスと要素の挿入が可能になります。リストインターフェイスは次のように実装されています。 配列リストリンクリストベクター そして スタック クラス。

リストインターフェイスjava



List はインターフェイスであり、List のインスタンスは次の方法で作成できます。

 List a = new ArrayList(); List b = new LinkedList(); List c = new Vector(); List d = new Stack();>

リストを初期化する方法は次のとおりです。

  1. List.add() メソッドの使用

    list はインターフェースであるため、直接インスタンス化することはできません。ただし、このインターフェイスを実装したクラスのオブジェクトを作成し、インスタンス化することはできます。



    List インターフェースを実装しているクラスはほとんどありません。 スタック、配列リスト、リンクリスト、ベクトル

    構文:

     List list=new ArrayList(); List llist=new LinkedList(); List stack=new Stack();>

    例:






    import> java.util.*;> import> java.util.function.Supplier;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// For ArrayList> >List list =>new> ArrayList();> >list.add(>1>);> >list.add(>3>);> >System.out.println(>'ArrayList : '> + list.toString());> > >// For LinkedList> >List llist =>new> LinkedList();> >llist.add(>2>);> >llist.add(>4>);> >System.out.println(>'LinkedList : '> + llist.toString());> > >// For Stack> >List stack =>new> Stack();> >stack.add(>3>);> >stack.add(>1>);> >System.out.println(>'Stack : '> + stack.toString());> >}> }>

    >

    >

    出力:

     ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]>

    二重中括弧の初期化 上記の作業を行うためにも使用できます。

    構文:

     List list=new ArrayList(){{ add(1); add(2); add(3); }};>

    例:




    import> java.util.*;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// For ArrayList> >List list =>new> ArrayList() {{> >add(>1>);> >add(>3>);> >} };> >System.out.println(>'ArrayList : '> + list.toString());> > >// For LinkedList> >List llist =>new> LinkedList() {{> >add(>2>);> >add(>4>);> >} };> >System.out.println(>'LinkedList : '> + llist.toString());> > >// For Stack> >List stack =>new> Stack() {{> >add(>3>);> >add(>1>);> >} };> >System.out.println(>'Stack : '> + stack.toString());> >}> }>

    >

    >

    出力:

     ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]>
  2. Arrays.asList() の使用

    • 不変リストの作成

      Arrays.asList() 配列から不変のリストを作成します。したがって、配列を使用してリストをインスタンス化するために使用できます。

      構文:

      List list=Arrays.asList(1, 2, 3);>

      例:




      import> java.util.Arrays;> import> java.util.List;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Instantiating List using Arrays.asList()> >List list = Arrays.asList(>1>,>2>,>3>);> > >// Print the list> >System.out.println(>'List : '> + list.toString());> >}> }>

      >

      >

      出力:

       List : [1, 2, 3]>
    • 可変リストの作成

      構文:

      List list=new ArrayList(Arrays.asList(1, 2, 3));>

      例:




      import> java.util.ArrayList;> import> java.util.Arrays;> import> java.util.List;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Creating a mutable list using Arrays.asList()> >List list =>new> ArrayList(> >Arrays.asList(>1>,>2>,>3>));> > >// Print the list> >System.out.println(>'List : '> + list.toString());> > >list.add(>5>);> > >// Print the list> >System.out.println(>'Modified list : '> + list.toString());> >}> }>

      ネットワークトポロジー

      >

      >

      出力:

       List : [1, 2, 3] Modified list : [1, 2, 3, 5]>
  3. Collections クラスのメソッドの使用

    Collections クラスには、リストをインスタンス化するために使用できるさまざまなメソッドがあります。彼らです:

    1. Collections.addAll() の使用

      コレクション クラスには静的メソッドがあります 全て追加する() これはリストを初期化するために使用できます。 Collections.addAll() 要素が挿入されるコレクションで指定された後、任意の数の要素を取り込みます。

      構文:

      List list = Collections.EMPTY_LIST; Collections.addAll(list = new ArrayList(), 1, 2, 3, 4);>

      例:




      import> java.util.*;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Create an empty list> >List list =>new> ArrayList();> > >// Instantiating list using Collections.addAll()> >Collections.addAll(list,>1>,>2>,>3>,>4>);> > >// Print the list> >System.out.println(>'List : '> + list.toString());> >}> }>

      >

      >

      出力:

       List : [1, 2, 3, 4]>
    2. Collections.unmodifiableList() の使用

      Collections.unmodifiableList() 変更できないリストを返します。つまり、要素の追加も削除もできません。リストを変更しようとすると、UnsupportedOperationExample が発生します。

      構文:

      List list = Collections .unmodifiableList(Arrays.asList(1, 2, 3));>

      例 1:




      import> java.util.*;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Creating the list> >List list = Collections.unmodifiableList(> >Arrays.asList(>1>,>2>,>3>));> > >// Print the list> >System.out.println(>'List : '> + list.toString());> >}> }>

      >

      >

      出力:

       List : [1, 2, 3]>

      例 2:




      import> java.util.*;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >try> {> >// Creating the list> >List list = Collections.unmodifiableList(> >Arrays.asList(>1>,>2>,>3>));> > >// Print the list> >System.out.println(>'List : '> + list.toString());> > >// Trying to modify the list> >System.out.println(>'Trying to modify the list'>);> >list.set(>0>, list.get(>0>));> >}> > >catch> (Exception e) {> >System.out.println(>'Exception : '> + e);> >}> >}> }>

      >

      >

      出力:

       List : [1, 2, 3] Trying to modify the list Exception : java.lang.UnsupportedOperationException>
    3. Collections.singletonList() の使用

      Collections.singletonList() 1 つの要素のみからなる不変のリストを返します。

      構文:

      List list = Collections.singletonList(2);>

      例 1:




      import> java.util.*;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Creating the list> >List list = Collections.singletonList(>2>);> > >// Print the list> >System.out.println(>'List : '> + list.toString());> >}> }>

      >

      >

      出力:

       List : [2]>
  4. Java 8 ストリームの使用

    Java 8 でのストリームと関数型プログラミングの導入により、オブジェクトの任意のストリームを構築し、それらをリストとして収集できるようになりました。

    構文:

     1. List list = Stream.of(1, 2, 3) .collect(Collectors.toList()); 2. List list = Stream.of(1, 2, 3) .collect(Collectors.toCollection(ArrayList::new)); 3. List list = Stream.of(1, 2, 3, 4) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));>

    例:




    import> java.util.*;> import> java.util.stream.Collectors;> import> java.util.stream.Stream;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Creating a List using Syntax 1> >List list1 = Stream.of(>1>,>2>,>3>)> >.collect(Collectors.toList());> > >// Printing the list> >System.out.println(>'List using Syntax 1: '> >+ list1.toString());> > >// Creating a List using Syntax 2> >List list2 = Stream> >.of(>3>,>2>,>1>)> >.collect(> >Collectors> >.toCollection(ArrayList::>new>));> > >// Printing the list> >System.out.println(>'List using Syntax 2: '> >+ list2.toString());> > >// Creating a List using Syntax 3> >List list3 = Stream> >.of(>1>,>2>,>3>,>4>)> >.collect(> >Collectors> >.collectingAndThen(> >Collectors.toList(),> >Collections::unmodifiableList));> > >// Printing the list> >System.out.println(>'List using Syntax 3: '> >+ list3.toString());> >}> }>

    >

    >

    出力:

     List using Syntax 1: [1, 2, 3] List using Syntax 2: [3, 2, 1] List using Syntax 3: [1, 2, 3, 4]>
  5. Java 9 List.of() の使用

    Java 9 では、任意の数の引数を受け取り、それらからコンパクトで変更不可能なリストを構築する List.of() メソッドが導入されました。

    構文:

    List unmodifiableList = List.of(1, 2, 3);>

    例:




    import> java.util.List;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Creating a list using List.of()> >List unmodifiableList = List.of(>1>,>2>,>3>);> > >// Printing the List> >System.out.println(>'List : '> >+ unmodifiableList.toString());> >}> }>

    >

    >

    出力:

     [1, 2, 3]>