Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
Data structures and algorithms in the collection framework 1 Part 2.
1 Concrete collections II. 2 HashSet hash codes are used to organize elements in the collections, calculated from the state of an object –hash codes are.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
Data structures Abstract data types Java classes for Data structures and ADTs.
Data structures and algorithms in the collection framework 1.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Comparable and Comparator Nuts and Bolts. Sets A set is a collection in which all elements are unique—an element is either in the set, or it isn’t In.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Hash Tables and Hash Maps. DCS – SWC 2 Hash Tables A Set and a Map are both abstract data types – we need a concrete implemen- tation in order to use.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Starting Out with Java From Control Structures through Data Structures by Tony Gaddis and Godfrey Muganda Collections in Java.
AD Lecture #3 Java Collection Framework 1.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sets and Maps Chapter 9.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
Collections Framework
Sets and Maps Chapter 9.
Presentation transcript:

Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA

What is JavaFX FXML  An XML-based language that allows you to separate your user interface from your application code  XML is similar to HTML, but with a stricter syntax  You create a.fxml file, using Scene Builder or a tool/plugin from your IDE  Java uses reflection to dynamically create the Nodes, such as buttons/text, so you can access them in your controller code.  Reflection: code which inspects other code. Examples including creating a object from a String class name, look up method names/bodies, etc.  Tutorial:  Java Scene Builder: html html  Window -> Preferences -> JavaFX -> Scene Builder Executable

The MVC Pattern  Separating your application logic from your user interface code  You should be able to switch out you user interface without affecting your application logic code  I can change the look and feel of your recipe application without you rewriting your code to parse the recipe save files/adding items/etc.  You create model classes to model your data (Student, Shape, Ingredient, etc.)  You create FXML files to define you view, the user interface  You create controller classes which primary purpose is to handle the input events

What is a Data Structure?  A particular way of organizing data  How do we hold information?  It is a abstract concept  Same concept applies in all other languages  They existed before computers existed  Mathematical formal definitions  There’s always a tradeoff for whatever you’re doing  You’ve already used a data structure: arrays  We’re going to cover the basics some of the most useful data structures implemented in the Java Collection Framework  More in-depth in CS203

Java Collections To Cover  Lists  ArrayLists, LinkedLists  Sets  HashSet, LinkedHashSet, TreeSet  Maps  HashMap, LinkedHashMap, TreeMap

6 The List Interface  A list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index.

7 ArrayList and LinkedList  The ArrayList class and the LinkedList class are concrete implementations of the List interface.  ArrayList:  Good for common uses 99.9% of the time  Fast Random Access (getting elements)  LinkedList:  Use only when there is a compelling reason  Fast Insert/Deletes at arbitrary locations  Slower Random Access

How Does LinkedList Work?

How Does ArrayList Work?  Internally, it’s an array, starts with length 10 (Object[])  Before an element is added, if the array is at maximum capacity, create a new array with 1.5 * currentSize, then copy over the old array and insert new data  Different from LinkedList (just add node to end)  In Most cases, ArrayList is good enough and the insertion/copying overhead cost is not a big deal  When doing large numbers of insertions, take a look at LinkedList and do a performance benchmark

10 The Set Interface  Set  Same Concept from math  A collection of distinct objects, i.e. no duplicates  Examples:  {1, 2, 3}  {“Bob”, “Cat”, “John”}  Implementations to Cover:  HashSet  LinkedHashSet  TreeSet  Use Cases:  Track a list of URLs that have been processes, track the distinct words in a document  Relational Database queries return result sets (a set of rows), and they use relational algebra to evaluate the result sets

11 The HashSet Class  The HashSet class is a concrete class that implements Set. It can be used to store duplicate-free elements. For efficiency, objects added to a hash set need to implement the hashCode method in a manner that properly disperses the hash code.  Insertion Order is not preserved: you cannot do a forloop over a HashSet and expect the order returned to be the same as the order you inserted

How to Use HashSet  Requirements:  the following two methods from java.lang.Object must be overridden: public int hashCode() and public Boolean equals(Object obj)  If you’re adding built-in Java Objects to a HashSet, you don’t have to worry about overriding the two methods  If you’re adding your custom objects to a HashSet, you must override hashCode and equals.  In order to keep the objects distinct, HashSet compares the object being added and make sure it is not present by calling.equals and comparing the result of the hashCode method  If you don’t override it Java will compare the memory addresses by default, so different objects instances are all considered different  For example, two different Student objects with the exact same fields would be considered different by the HashSet if you don’t override those two methods.

LinkedHashSet  Similar to HashSet, but preserves the insertion order  Just like ArrayList, You can simply use the for-each loop construct to loop through the contents, and the items will be returned in the insertion order  If you want access the index, use a Iterator along with your own variable int index=0; Iterator itr = lhs.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); index++; }

14 The SortedSet Interface and the TreeSet Class  SortedSet is a subinterface of Set, which guarantees that the elements in the set are sorted.  TreeSet is a concrete class that implements the SortedSet interface. You can use an iterator to traverse the elements in the sorted order.  Sorting happens when a new element is inserted.  The elements can be sorted either by implementing Comparable or a comparator

15 The Map Interface  The Map interface maps keys to the elements. The keys are like indexes. In List, the indexes are integer. In Map, the keys can be any objects.  Keys are distinct/unique, so you must override equals/hashcode for the key object type  They are sometimes called “dictionaries” in other languages (i.e. Python), you looks up “definitions” (values) using a key

16 Map Interface and Class Hierarchy An instance of Map represents a group of objects, each of which is associated with a key. You can get the object from a map using a key, and you have to use a key to put the object into the map.

17 HashMap, LinkedHashMap, and TreeMap  The HashMap and TreeMap classes are two concrete implementations of the Map interface.  HashMap, which you will use 90% of the time, works similarly to HashSet: the order is not preserved  LinkedHashMap preserves the insertion order  The TreeMap class, implementing SortedMap, is efficient for traversing the keys in a sorted order. Again, you use either a comparator or implement comparable for the key object type.