DEV Community

Cover image for Collections
Greg Ross
Greg Ross

Posted on

Collections

General idea without bloat of additional coding methods

Structures of Data

Ways to store information in an ordered manner so the data can be called upon when needed. The values can be referenced by index or iterated over where additional processing could be performed.

Common Sequences

Arrays

int[] wholeNumbers = new int[5]; // [item, item, item, item, item]            
String[] words = new String[5];  // [item, item, item, item, item]            
Enter fullscreen mode Exit fullscreen mode

Lists

import java.util.List;
import java.util.ArrayList;

List<String> listOfideas = new ArrayList<>();
listOfideas.add("serendipity");
listOfideas.add("synchronicity");
listOfideas.add("determinism");
System.out.println(listOfideas);

List<Integer> listOfWholeNumbers = new ArrayList<>(); 
listOfWholeNumbers.add(5);
listOfWholeNumbers.add(15);
listOfWholeNumbers.add(51);
System.out.println(listOfWholeNumbers);
Enter fullscreen mode Exit fullscreen mode
Documentation

List Interface
ArrayList Class

Associative Arrays

Using key:value pairs. Values can be accessed by calling the key of the associated value. Versus where you could need to loop over the entire collection to find the desired value.

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

Map<String, Boolean> animals = new HashMap<String, Boolean>();

animals.put("dog", true);
animals.put("libera", false);
animals.put("girazelle", false);
animals.put("cat", true);

System.out.println(animals);
Enter fullscreen mode Exit fullscreen mode
Documentation

Map Interface

Others

Beyond the scope of my journal

Stack<T>, Queue<T>, Set<T>

Top comments (0)