Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA."— Presentation transcript:

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

2 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: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htmhttp://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm  Java Scene Builder: http://www.oracle.com/technetwork/java/javafxscenebuilder-1x-archive- 2199384.html http://www.oracle.com/technetwork/java/javafxscenebuilder-1x-archive- 2199384.html  Window -> Preferences -> JavaFX -> Scene Builder Executable

3 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

4 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

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

6 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 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

8 How Does LinkedList Work?

9 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 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 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

12 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.

13 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 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 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 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 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.


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

Similar presentations


Ads by Google