[Java] Collection-List / Set / Map / Stack / Queue

list

ArrayList


import java.util.ArrayList;
import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    var list = new ArrayList<Integer>(Arrays.asList(10, 15, 30, 60));
    var list2 = new ArrayList<Integer>(Arrays.asList(1, 5, 3, 6));
    var list3 = new ArrayList<Integer>(Arrays.asList(1, 2, 3));

    for (var i : list) {
      System.out.println(i / 5); //2,3,6,12
    }
    //Element count
    System.out.println(list.size()); //4
    //Get 0th element
    System.out.println(list.get(0)); //10
    //Does it contain the specified element?
    System.out.println(list.contains(30)); //true
    //Find location
    System.out.println(list.indexOf(30));  //2
    //Search for position from behind
    System.out.println(list.lastIndexOf(30)); //2
    //Is the list empty?
    System.out.println(list.isEmpty());    //false
    //Delete 0th element
    System.out.println(list.remove(0));   //10
    System.out.println(list);             //[15, 30, 60]
    //Insert collection
    list.addAll(list2);
    System.out.println(list); //[15, 30, 60, 1, 5, 3, 6]
    //Delete all elements in the collection
    list.removeAll(list3);
    System.out.println(list); //[15, 30, 60, 5, 6]
    //0th element set
    list.set(0, 100);
    var data = list.toArray();
    System.out.println(Arrays.toString(data)); //[100, 30, 60, 5, 6]
  }
}

LinkedList

import java.util.Arrays;
import java.util.LinkedList;

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

    var list = new LinkedList<String>(Arrays.asList("Ox", "Tiger", "Rabbit"));
    list.addFirst("Child");
    list.addLast("Dragon");
    System.out.println(list); //[Child,Ox,Tiger,Rabbit,Dragon]
    System.out.println(list.getFirst()); //Child
    System.out.println(list.getLast()); //Dragon
    System.out.println(list.removeFirst()); //Child
    System.out.println(list.removeLast()); //Dragon
    System.out.println(list); //[Ox,Tiger,Rabbit]
  } 
}

set

HashSet

import java.util.Arrays;
import java.util.HashSet;

public class Main {
  public static void main(String[] args) {
    //Ignore duplicate elements
    var hs = new HashSet<Integer>(Arrays.asList(1, 20, 30, 10, 30, 60, 15));
    var hs2 = new HashSet<Integer>(Arrays.asList(10 ,20 ,99));

    //System.out.println(hs);
    System.out.println(hs.size()); //6
    System.out.println(hs.isEmpty()); //false
    //Does it contain a specified element?
    System.out.println(hs.contains(1)); //true  
    //Does it include all specified elements?
    System.out.println(hs.containsAll(hs2)); //false
    //Delete the specified element
    System.out.println(hs.remove(1)); //true 
    System.out.println(hs); //[20, 10, 60, 30, 15]
    //If the specified element is not included in the set, add all (union)
    hs.addAll(hs2);
    System.out.println(hs); //[99, 20, 10, 60, 30, 15]
    //Delete all non-elements in the collection (intersection)
    hs.retainAll(hs2);
    System.out.println(hs); //[99, 20, 10]
    
    var hs3 = new HashSet<Integer>(Arrays.asList(1, 10 , 20));
    //Delete all specified elements(Difference set)
    hs.removeAll(hs3);
    System.out.println(hs);  //[99]
  }
}

TreeSet

import java.util.Arrays;
import java.util.TreeSet;

public class Main {
  public static void main(String[] args) {
    var ts = new TreeSet<Integer>(Arrays.asList(1, 20, 30, 10, 60, 15));
    System.out.println(ts); //[1, 10, 15, 20, 30, 60]
    //Sort in reverse order
    System.out.println(ts.descendingSet()); //[60, 30, 20, 15, 10, 1]
    //Get the smallest one above the specified element
    System.out.println(ts.ceiling(15));     //15
    //Get the largest one smaller than the specified element
    System.out.println(ts.lower(15));       //10
    //Get more than specified elements
    System.out.println(ts.tailSet(15));     //[15, 20, 30, 60]
    //Get the one smaller than the specified element, including the one equal with the second argument true
    System.out.println(ts.headSet(30, true)); //[1, 10, 15, 20, 30]
  }
}

map

HashMap

import java.util.HashMap;
import java.util.Map;


public class Main {
  public static void main(String[] args) {
    var map = new HashMap<String, String>(Map.of("Cat", "Cat",
      "Dog", "Dog", "Bird", "Bird"));
    //Is the specified key included?
    System.out.println(map.containsKey("Dog")); //true
    //Whether the specified value is included
    System.out.println(map.containsValue("Cat")); //true
    //Is the map empty?
    System.out.println(map.isEmpty()); //false
    //Get all keys
    for (var key : map.keySet()) {
      System.out.println(key); //Dog Bird Cat
    }
    //Get all values
    for (var value : map.values()) {
      System.out.println(value); //Dog Torineko
    }
    //Change the value of the specified key to value
    map.replace("Cat", "neko");
    //Specified key value/value(old)Value(new)change to
    map.replace("Bird", "Bird", "tori");
    //Get all elements
    for (var entry : map.entrySet()) {
      System.out.println(entry.getKey() + ":" + entry.getValue()); //Dog:Dog Bird:tori Cat:neko
    }
  }
}

IdentityHashMap

//In case of HashMap, if the value is the same, it will be overwritten and only one entry will be registered.
//For IdentityHashMap, key1,Since 2 is a different object, it is regarded as a different key.
import java.util.HashMap;
import java.util.IdentityHashMap;
public class Main {
  public static void main(String[] args) {
    var key1 = Integer.valueOf(256);
    var key2 = Integer.valueOf(256);

    //var data = new HashMap<Integer, String>() {
    var data = new IdentityHashMap<Integer, String>() {
      {
        put(key1, "Hoge");
        put(key2, "Foo");
      }
    };  
    //System.out.println(data); //{256=Foo} 
    System.out.println(data);   //{256=Foo, 256=Hoge}
  }
}

WeakHashMap

TreeMap

//Keys can be sorted lexicographically
import java.util.Map;
import java.util.TreeMap;

public class Main {
  public static void main(String[] args) {
    var data = new TreeMap<String, String>(Map.of("Cat", "Cat",
        "Dog", "Dog", "Bird", "Bird"));
    for (var key : data.keySet()) {
      System.out.println(key); //Bird Cat Dog
    }
  }
}

Key order

//Sort by key length
import java.util.TreeMap;
// import java.util.Comparator;

public class Main {
  public static void main(String[] args) {
    var data = new TreeMap<String, String>(
        (x, y) -> x.length() - y.length()
      );
//For anonymous classes
//    var data = new TreeMap<String, String>(new Comparator<String>(){
//      @Override
//      public int compare(String x, String y) {
//        return x.length() - y.length();
//      }
//    });

      data.put("Cat", "Cat");
      data.put("Shiba inu", "Shiba Inu");
      data.put("Bird", "Bird");
      System.out.println(data); //{Cat=Cat, Bird=Bird, Shiba inu=Shiba Inu}
  }
}

Array / list sorting

//Sort in ascending order by string length
import java.util.ArrayList;
import java.util.Arrays;

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

    var data = new String[] { "Cat", "Shiba inu", "Bird", "Poodle" };
    Arrays.sort(data, (x, y) -> x.length() - y.length());
    System.out.println(Arrays.toString(data)); //[Cat, Bird, Poodle, Shiba inu]

    var list = new ArrayList<String>(Arrays.asList("Cat", "Shiba inu", "Bird", "Poodle"));
    list.sort((x, y) -> x.length() - y.length());
    System.out.println(list); //[Cat, Bird, Poodle, Shiba inu]
  }
}

Fuzzy search

import java.util.TreeMap;

public class Main {
  public static void main(String[] args) {
    var data = new TreeMap<String, String>() {
      {
        put("nekko", "Nekko");
        put("nezumi", "mouse");
        put("night", "Night");
        put("nature", "Nature");
      }
    };

    var key = "neko";

    if (data.containsKey(key)) {
      System.out.println(key + "Is" + data.get(key) + "is.");
    } else {
      System.out.print("The word you are searching for");
      //Get the largest entry with a key smaller than the specified key
      System.out.print(data.lowerKey(key) + "Or");
      //Get the smallest entry with a key larger than the specified key
      System.out.print(data.higherKey(key));
      System.out.println("Is it?");
    }
  }
}

LinkedHashMap

import java.util.LinkedHashMap;

public class Main {
  public static void main(String[] args) {
    //Third argument=true is access order
    //Third argument=false is the insertion order
    var data = new LinkedHashMap<String, String>(10, 0.7f, false) {
      {
        put("aaa", "AIUEO");
        put("bbb", "Kakikukeko");
        put("ccc", "SA Shi Su Se So");
        put("ddd", "TA Chi Tsu Te to");
      }
    };
    System.out.println(data.get("ccc"));
    System.out.println(data.get("aaa"));
    System.out.println(data.get("bbb"));
    System.out.println(data.get("ddd"));
    //true
    System.out.println(data); //{ccc=SA Shi Su Se So, aaa=AIUEO, bbb=Kakikukeko, ddd=TA Chi Tsu Te to}
    //false
    //System.out.println(data); //{aaa=AIUEO, bbb=Kakikukeko, ccc=SA Shi Su Se So, ddd=TA Chi Tsu Te to}
  }
}

Stack / queue

ArrayDeque

import java.util.ArrayDeque;

public class Main {
  public static void main(String[] args) {
    //stack
    var data = new ArrayDeque<Integer>();
    data.addLast(10);
    data.addLast(15);
    data.addLast(30);

    System.out.println(data); //[10, 15, 30]
    System.out.println(data.removeLast()); //30
    System.out.println(data); //[10, 15]
    //queue
    var data2 = new ArrayDeque<Integer>(); 
    data2.addLast(10);
    data2.addLast(15);
    data2.addLast(30);

    System.out.println(data2); //[10, 15, 30]
    System.out.println(data2.removeFirst()); //10
    System.out.println(data2); //[15, 30]
  }
}

Recommended Posts

[Java] Collection-List / Set / Map / Stack / Queue
Java (set)
JAVA (Map)
List, Set, Map
[Java] Map comparison
[Java] Queue, Deque
[Java] Filter stack traces
[Java] Stream API / map
Enum reverse map Java
Java bidirectional map library
[Java] How to use Map
[Java] How to use Map
[Java] Stack area and static area
How to use Java Map
How to set Java constants
Log Java NullPointerException stack trace