[Java] Collection framework

What is a collection?

Collection interface

Collection instantiation

** Interface type **

** Generic syntax **

idiom

Initialize the collection at the timing of instantiation

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    //Anonymous class
    var data = new ArrayList<String>() {
      //Initialization block
      {
        add("neko");
        add("inu");
        add("tori");
      }
    };
    System.out.println(data);//[neko, inu, tori]
  }
}

Process under collection in order with extension for

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    var data = new ArrayList<String>() {
      {
        add("neko");
        add("inu");
        add("kuma");
      }
    };
    //Process under collection in order
    for (var s : data) {
      System.out.println(s); //neko inu kuma
    }
    //When using an iterator
    //     var itr = data.iterator();
    //     while (itr.hasNext()) {
    //      System.out.println(itr.next());
    //     }
  }
}

Delete elements in a loop

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    var data = new ArrayList<String>() {
      {
        add("neko");
        add("inu");
        add("niwatori");
      }
    };

    var ite = data.iterator();
    while (ite.hasNext()) {
      System.out.println(ite.next()); //neko inu niwatori
      ite.remove();
    }
    System.out.println(data); //[]
  }
}
//NG example:Extension for
    for (var s : data) {
      System.out.println(s);
      data.remove(s);
    }

Read the list in reverse order

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    var data = new ArrayList<String>() {
      {
        add("neko");
        add("inu");
        add("shimaenaga");
      }
    };
    //Set the end to the start position
    var ite = data.listIterator(data.size());
    while (ite.hasPrevious()) {
      System.out.println(ite.previous()); //shimaenaga inu neko
    }
  }
}

Array and collection transformations

From array to list

//NG example: Convert existing character string to list, add and delete value to converted list
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    var data = new String[] { "cat", "dog", "bird"};
    var list = Arrays.asList(data);
    list.set(0, "neko");
    System.out.println(list); //[neko, dog, bird]
    System.out.println(Arrays.toString(data)); //[neko, dog, bird]
    //list.add("inu"); //UnsupportedOperationException
    //list.remove(0);  //UnsupportedOperationException
  }
}
//OK Example: Convert existing character string to list, add and delete value to converted list
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    var data = new String[] { "cat", "dog", "bird" };
    var list = new ArrayList<String>();
    Collections.addAll(list, data);
    list.set(0, "neko");
    list.add("inu");
    System.out.println(list); //[neko, dog, bird, inu]
    list.remove(1);

    System.out.println(list); //[neko, bird, inu]
    System.out.println(Arrays.toString(data)); //[cat, dog, bird]
  }
}

From collection to array

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    var data = new ArrayList<String>(Arrays.asList("cat", "dog", "bird"));
    //Copy the contents to an array strs of the same size as the list
    var strs = new String[data.size()];
    data.toArray(strs);
    //Add data only to the conversion source list
    data.set(0, "neko");
    System.out.println(Arrays.toString(strs)); //[cat, dog, bird]
    //Only the conversion source list has changed
    System.out.println(data); //[neko, dog, bird]
  }
}

Convert to an immutable collection

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    var data = new ArrayList<String>(Arrays.asList("cat", "dog", "bird"));
    //Generate an immutable list
    var udata = Collections.unmodifiableList(data);
    udata.set(0, "neko"); //UnsupportedOperationException
    udata.add("inu");  //UnsupportedOperationException
  }
}
//Collection element contents can be changed in StringBuilder
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    var data = new ArrayList<StringBuilder>(Arrays.asList(
        new StringBuilder("Hello"),
        new StringBuilder("Kamesan"),
        new StringBuilder("Yo")));
    var udata = Collections.unmodifiableList(data);
    udata.get(0).append("Kameyo");
    System.out.println(udata); //[Hello,Kamesan,Yo]
  }
}
//Immutable collection directly generated by the of method
import java.util.List;
public class Main {
  public static void main(String[] args) {
    var data = List.of("cat", "dog", "bird");
    System.out.println(data);
    data.set(0, "neko"); //UnsupportedOperationException
  }
}

Empty collection generation

Synchronized collection generation

Recommended Posts

[Java] Collection framework
☾ Java / Collection
java framework
Java9 collection
Java Reintroduction-Java Collection
Java framework comparison
Play Framework2.5 (Java) Tips
Java collection interview questions
[Development] Java framework comparison
Learning Java framework # 1 (Mac version)
Getting Started with Java Collection
I summarized the collection framework.
Java parallelization code sample collection
Java Collections Framework Review Notes
[Java] Comparator of Collection class
Java test code method collection
Java
Java
Consideration on Java Persistence Framework 2017 (Summary) -1
Consideration on Java Persistence Framework 2017 (6) Ebean
Root cause of java framework bugs
Consideration on Java Persistence Framework 2017 (5) Iciql
Play Framework 2.6 (Java) development environment creation
Consideration on Java Persistence Framework 2017 (7) EclipseLink
Java --Jersey Framework vs Spring Boot
Consideration on the 2017 Java Persistence Framework (1)
Spring Framework tools for Java developer
I tried the Java framework "Quarkus"
JNA (Java Native Access) pattern collection
Consideration on Java Persistence Framework 2017 (2) Doma2
Java Performance Chapter 5 Garbage Collection Basics
Studying Java ―― 3
[Java] array
Try using Java framework Nablarch [Web application]
Java protected
[Java] Annotation
[Java] Module
Java array
Significance of interface learned from Java Collection
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Guess about the 2017 Java Persistence Framework (3) Reladomo
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
java shellsort
[Java] compareTo
java reflexes