Parsing JSON, Using Libraries, Java Collections, Generics

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
The Java Collections Framework (JCF) Introduction and review 1.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Building Java Programs Chapter 10 ArrayList Copyright (c) Pearson All rights reserved.
Using the Java Collection Libraries COMP 103 # T2
slides created by Marty Stepp
Sixth Lecture ArrayList Abstract Class and Interface
Collections.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
JCF Hashmap & Hashset Winter 2005 CS-2851 Dr. Mark L. Hornick.
Building Java Programs Chapter 14
Building Java Programs
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
HW-6 Deadline Extended to April 27th
Welcome to CSE 143!.
Building Java Programs
Java Collections Overview
Building Java Programs Chapter 10
CS 106A, Lecture 19 ArrayLists
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
ArrayLists.
Building Java Programs
Building Java Programs
CSE 143 Lecture 4 ArrayList Reading: 10.1.
ArrayLists.
Building Java Programs Chapter 10
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
CSE 143 Lecture 2 Collections and ArrayIntList
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Lecture 2: Implementing ArrayIntList reading:
Welcome to CSE 143! Go to pollev.com/cse143.
Building Java Programs
Lecture 13: ArrayLists AP Computer Science Principles
CSE 1020: The Collection Framework
Lecture 16: Arraylist Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists.
slides created by Marty Stepp
Building Java Programs
slides created by Marty Stepp
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Introduction to Java Collection
Presentation transcript:

Parsing JSON, Using Libraries, Java Collections, Generics

JSON (www.json.org) JavaScript Object Notation A lightweight data-interchange format Very commonly used by APIs It is easy for humans to read and write. It is easy for machines to parse and generate.

Example JSON object { name_of_a_string: “a string”, name_of_a_number: 2080.8827, objects_can_be_values: { here_is: “another object” }, an_array: [ 27, “word”, { objects_can: “be in arrays” } ] }

Using APIs (e.g., https://newsapi.org) API = Application Programming Interface Get an API key Grab some JSON: https://newsapi.org/v1/articles?source=associated-press&sortBy=top&apiKey=YOUR_API_KEY_HERE JSON formatter/pretty printer https://jsonformatter.curiousconcept.com There are a bunch of these, use your favorite

How much time did TicTacToe assignment take? 0 – 3 hours 4 – 6 hours 7 – 9 hours 10– 12 hours More than 12 hours

How much difficult was the TicTacToe assignment? Trivial Easy Reasonable Difficult Excessive

Parsing JSON in Java Use the GSON library from Google https://github.com/google/gson/blob/master/UserGuide.md Use Maven to add the library to your project Build classes with fields for the desired elements of the JSON Use the same names and get the types right Instantiate a Gson object Gson gson = new Gson(); Use the fromJSON method to parse the JSON Thing newThing = gson.fromJson(jsonString, Thing.class); Thing [] thingArray = gson.fromJson(jsonString, Thing[].class); Extended example using NewsAPI

Java Collections collection: an object that stores data; a.k.a. "data structure" the objects stored are called elements some collections maintain an ordering; some allow duplicates typical operations: add, remove, clear, contains (search), size examples found in the Java class libraries: ArrayList, HashMap, TreeSet all collections are in the java.util package import java.util.*;

Java Collection Framework

Lists list: a collection storing an ordered sequence of elements each element is accessible by a 0-based index a list has a size (number of elements that have been added) elements can be added to the front, back, or elsewhere in Java, a list can be represented as an ArrayList object

ArrayList Methods (partial list) add(value) appends value at end of list add(index, value) inserts given value just before the given index, shifting subsequent values to the right clear() removes all elements of the list indexOf(value) returns first index where given value is found in list (-1 if not found) get(index) returns the value at given index remove(index) removes/returns value at given index, shifting subsequent values to the left set(index, value) replaces value at given index with given value size() returns the number of elements in list toString() returns a string representation of the list such as "[3, 42, -7, 15]"

Map Allows lookups from one kind of object to find another object Map<KeyType, ValueType> myMap = HashMap<KeyType, ValueType>(); KeyType key = …; ValueType value = …; myMap.put(key, value); ValueType lookup = myMap.get(key); assert lookup == value;

Map Interface put(k,v) Associate v with k get(k) The value associated with k size() The number of pairs isEmpty() Whether it is empty remove(k) Remove the mapping for k clear() Remove all mappings containsKey(k) Whether contains a mapping for k containsValue(v) Whether contains a mapping to v

ArrayList<Type> name = new ArrayList<Type>(); Generics ArrayList<Type> name = new ArrayList<Type>(); When constructing an ArrayList, you must specify the type of elements it will contain between < and >. This is called a type parameter or a generic class. Allows the same ArrayList class to store lists of different types. Must be objects (vs. primitive types)

Boxed Primitive Types Can’t do ArrayList<int> Java provides “boxed primitives”: E.g., Integer Sub-class of object Can do: ArrayList<Integer> lengths = new ArrayList<Integer> lengths.add(7); // automatically promoted to boxed type Primitive Type Wrapper Type int Integer double Double char Character boolean Boolean