Collection 105 Yola
To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps
Collection Queue - a collection of values awaiting a processing. FIFO List – an ordered collection Set – a collection without duplicates Map – use associative keys. Search by its value but not the index Two interfaces for the dynamic data structures
How to use ArrayList import java.util.ArrayList; import java.util.Scanner; public class ArrLstExt { public static void main(String[] args) { ArrayList city = new ArrayList (); String name=""; Scanner a = new Scanner(System.in); while(!name.equals("stop")){ System.out.println("Input a name:"); name = a.next(); city.add(name); } city.set(1, "first city"); // to modify an element; }
Assignment 1 1.Implement search on the array list 2.Implement sort (find a method) 3.Implement shuffle (find a method) 4.Find min and max (find a method) 5.Swap two elements of an ArrayList
How to use queue import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class QueueEx { public static void main(String[] args) { Queue city = new LinkedList(); String name=""; Scanner a = new Scanner(System.in); while(!name.equals("stop")){ System.out.println("Input a name:"); name = a.next(); city.add(name); } System.out.println(city.poll()); // to remove the first item System.out.println(city.poll()); // to see without removing }
Assignment Input several values to queue Input several values to another List Synchronize queue to the List (leave only elements that present in the List)
How to use Set import java.util.HashSet; import java.util.Set; public class SetEx { public static void main(String[] args) { Set city = new HashSet(); Set city1 = new HashSet(); city.add("a"); city.add("b"); city.add("c"); city1.add("c"); city.add("d"); System.out.println(city.contains("a")); System.out.println(city.hashCode()==city1.hashCode()); city.retainAll(city1);// intersection of sets }}
How to use Map? This is HOME ASSIGNMENT!