Skip to main content
added 65 characters in body
Source Link
Basil Bourque
  • 346.4k
  • 128
  • 950
  • 1.3k

tl;dr

In Java 21+:

strings.addFirst( "a" )

In earlier Java:

strings.add( 0, "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

By the way, if frequently inserting elements into a List, consider using LinkedList rather than ArrayList. See When to use LinkedList over ArrayList in Java?.

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…

tl;dr

In Java 21+:

strings.addFirst( "a" )

In earlier Java:

strings.add( 0, "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

By the way, if frequently inserting elements into a List, consider using LinkedList rather than ArrayList.

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…

tl;dr

In Java 21+:

strings.addFirst( "a" )

In earlier Java:

strings.add( 0, "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

By the way, if frequently inserting elements into a List, consider using LinkedList rather than ArrayList. See When to use LinkedList over ArrayList in Java?.

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…
added 65 characters in body
Source Link
Basil Bourque
  • 346.4k
  • 128
  • 950
  • 1.3k

tl;dr

In Java 21+:

strings.addFirst( "a" )

In earlier Java:

strings.add( 0, "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

By the way, if frequently inserting elements into a List, consider using LinkedList rather than ArrayList.

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…

tl;dr

strings.addFirst( "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…

tl;dr

In Java 21+:

strings.addFirst( "a" )

In earlier Java:

strings.add( 0, "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

By the way, if frequently inserting elements into a List, consider using LinkedList rather than ArrayList.

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…
added 381 characters in body
Source Link
Basil Bourque
  • 346.4k
  • 128
  • 950
  • 1.3k

tl;dr

strings.addFirst( "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…

tl;dr

strings.addFirst( "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

tl;dr

strings.addFirst( "a" )

List#addFirst

If the nested lists are modifiable, in Java 21+ we can simply add first element.

First, some example data.

List < List < String > > listOfListsOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
System.out.println( "listOfListsOfStrings = " + listOfListsOfStrings );

listOfListsOfStrings.toString() = [[Foo], [Bar]]

Then we prepend on each nested list.

listOfListsOfStrings.forEach( strings -> strings.addFirst( "a" ) );

listOfListsOfStrings.toString() = [[a, Foo], [a, Bar]]

Sequenced collections

The addFirst method is part of the Sequenced Collections feature.

See JEP 431. And see also the good presentation by Stuart Marks.

Class hierarchy diagram with sequenced collections, by Stuart Marks of Oracle Corp.

Indeed, you might want to declare your list of lists to be of the more general type, a sequenced collection of sequenced collections.

SequencedCollection < SequencedCollection < String > > seqcolOfSeqcolOfStrings =
        List.of(
                new ArrayList <>( List.of( "Foo" ) ) ,
                new ArrayList <>( List.of( "Bar" ) )
        );
…
added 976 characters in body
Source Link
Basil Bourque
  • 346.4k
  • 128
  • 950
  • 1.3k
Loading
Source Link
Basil Bourque
  • 346.4k
  • 128
  • 950
  • 1.3k
Loading