Basic Data Structures in Java— Arrays, Lists, Sets, Queue, and Maps

  • an ordered collection
  • allows duplicate entries
  • access using the index
  • read/add/remove from both the beginning and/or end of the LinkedList
  • not a good structure for iteration
  • similar to the List
  • does not allow duplicates
  • We use Queue when we intend to sort the data before processing, or we intend to add or remove elements in a specific order.
  • First-In-First-Out (FIFO) — Queue
  • First-In-Last_out (FILO) — Stack
  • stores every object with a key (key-value collection)
  • HashMap: does not maintain insertion order, O(1)
  • LinkedHashMap: maintain insertion order, O(1)
  • TreeMap: sorted ascending order based on keys, not elements, O(log N)
Photo by Procreator UX Design Studio on Unsplash
String[] topThreeHobbies = {“Eat”, “Sleep”, “Code”};
List<String> bucketList = new ArrayList<>();bucketList.add("Visit Alaska");bucketList.add("Visit Hawaii");bucketList.add("Visit China");bucketList.set(2, "Visit Japan");   //replace visit Chinafor (String list : bucketList) {System.out.println(list);}
Set<String> codingJournal = new LinkedHashSet<>();codingJournal.add("2/10/2021");codingJournal.add("5/11/2021");codingJournal.add("7/31/2021");//Check whether have studied on that dateSystem.out.println(codingJournal.contains("7/30/2021"));    //falseSystem.out.println(codingJournal.contains("7/31/2021"));    //true//Traversing the LinkedHashSetIterator<String> trace = codingJournal.iterator();while(trace.hasNext()) {                 //elements iterate in insertion orderSystem.out.println(trace.next());   //printout dates that have logged for learning to code}
Map<String, ArrayList<String>> BU = new HashMap<>();ArrayList<String> languages = new ArrayList<>();languages.add("Java");languages.add("SQL");ArrayList<String> classes = new ArrayList<>();classes.add("CS520");classes.add("CS669");BU.put("Languages", languages);BU.put("Classes", classes);System.out.println(BU.get("Languages"));    //[Java, SQL]System.out.println(BU.get("Classes"));  //[CS520, CS669]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hanwen Zhang

Hanwen Zhang

Full-Stack Software Engineer at a Healthcare Tech Company | Document My Coding Journey | Improve My Knowledge | Share Coding Concepts in a Simple Way