Java Collection

Manit Cholpinyo
NEWZY Technologies
Published in
3 min readNov 17, 2017

basic java collection, short explain

Basic of Java Collection is a base of developer in java While it’s importent almost because It’s manage data in array and adapt to your project so easy use any data

https://www.javatpoint.com/collections-in-java
  1. ArrayList class
  2. LinkedList class
  3. List interface
  4. HashSet class
  5. LinkedHashSet class
  6. TreeSet class
  7. PriorityQueue class
  8. Map interface
  9. HashMap class
  10. LinkedHashMap class
  11. TreeMap class
  12. Hashtable class
  13. Sorting
  14. Comparable interface
  15. Comparator interface
  16. Properties class in Java

Arraylist : flixible to store data and get data so easy
LinkList: easy to add remove
HashSet Set contains unique elements only.
Sorting: sorting the elements of collection
Maps : store data by Key and Value

Array list

Array list is a Collections that keep data as normally array but the think that different is Arraylist It can reduce and enlarge automaticly follow data inside that make is flixible for working because don’t declare max size forword with normal Array

import java.util.ArrayList;

public class ArrayListExample {

public static void main (String[] args) {

ArrayList<String> names = new ArrayList<String>();
names.add("Mateo");
names.add("Danny");
names.add("Joe");
names.add("Alex");

System.out.println(names.size() + " people in the list");

// Access list through it index
System.out.println("name[0] = " + names.get(0));
System.out.println("name[3] = " + names.get(3));

// Iterate through ArrayList usign foreach
for (String name: names) {
System.out.print(name + " ");
}
System.out.println();

// Iterate through ArrayList using index (reverse)
for (int i = names.size() - 1; i >= 0; i--) {
System.out.print(names.get(i) + " ");
}
System.out.println();

// Check if the list contain Mateo
if (names.contains("Mateo")) {
System.out.println("Mateo is in the list");
} else {
System.out.println("Mateo is not in the list");
}

// Get Danny's index
int dannyIndex = names.indexOf("Danny");
System.out.println("Index of Danny is " + dannyIndex);
// Change Danny to Max
names.set(dannyIndex, "Max");

// Remove joe from the list
names.remove("Joe");

// Iterate through ArrayList usign foreach
for (String name: names) {
System.out.print(name + " ");
}
System.out.println();

// Clear all list data
names.clear();

if(names.isEmpty()) {
System.out.println("List is now empty");
}

}

}

Maps

Maps is a Coolection that have a pattern store that is Key Value

import java.util.HashMap;

public class HashMapExample {

public static void main (String[] args) {

HashMap<String, String> country = new HashMap<>();
country.put("de", "German");
country.put("th", "Thailand");
country.put("us", "United State");
country.put("tr", "Turkey");

System.out.println("Map size = " + country.size());

System.out.println("de = " + country.get("de"));
System.out.println("th = " + country.get("th"));
System.out.println("th = " + country.get("uk"));

System.out.println("Iterate over each Entry in HashMap");
for (HashMap.Entry<String, String> entry : country.entrySet())
{
System.out.println(entry.getKey() + " = " + entry.getValue());
}

country.remove("tr");
if(country.containsKey("tr")) {
System.out.println("Turkey exist in the map");
} else {
System.out.println("Turkey does not exist in the map");
}

country.clear();
System.out.println("Map size = " + country.size());

}
}

Stack

Stack is structor of data that use store data by Pattern First in last out, data will be put to stack first to it’s removed later that is only push data one way for working with Stack when we put data in to Stack it’s called Push and when bring data out from stack it’s called Pop

 import java.util.Stack;

public class StackExample {

public static void main (String[] args) {

Stack<String> fruit = new Stack<>();

// Add four fruits to stack
fruit.push("Apple");
fruit.push("Banana");
fruit.push("Melon");
fruit.push("Peach");

System.out.println("Pop " + fruit.pop() + " from stack");
System.out.println("Pop " + fruit.pop() + " from stack");
System.out.println("Pop " + fruit.pop() + " from stack");
System.out.println("Pop " + fruit.pop() + " from stack");

if (fruit.isEmpty()) {
System.out.println("Stack is empty");
}

}
}

--

--