Java Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Recent Blog Posts
Wiki Content
Recently Changed Pages
Installing JDK and/or JRE
Java Wiki
Spring Boot Tutorials
Java Indonesia
Gaming:- Minecraft Java
Java Training
It:
Exams
Applications (Product)
Database (Product)
Enterprise Management (Product)
Industries (Product)
Java and Middleware (Product)
Operating Systems (Product)
Systems (Product)
Tutorials
Tutorials
SCWCD
Data Structures
Learning Object Oriented Programming with Java
Classes
Spring Boot Tutorials
Community
Help
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
FANDOM
Explore
Current Wiki
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Java Wiki
75
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Recent Blog Posts
Wiki Content
Recently Changed Pages
Installing JDK and/or JRE
Java Wiki
Spring Boot Tutorials
Java Indonesia
Gaming:- Minecraft Java
Java Training
It:
Exams
Applications (Product)
Database (Product)
Enterprise Management (Product)
Industries (Product)
Java and Middleware (Product)
Operating Systems (Product)
Systems (Product)
Tutorials
Tutorials
SCWCD
Data Structures
Learning Object Oriented Programming with Java
Classes
Spring Boot Tutorials
Community
Help
Editing
Data Structures
Back to page
Edit
Edit source
View history
Talk (1)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== General == Every application larger than the typical [[Hello world program]] needs some sort of data structure to hold the application data. Like other languages Java has its own framework to handle exactly this. THE data structure framework in Java is known as the [http://java.sun.com/docs/books/tutorial/collections/index.html Java Collection Framework]. [http://java.happycodings.com/beginners-lab-assignments/ Java Codes for Beginners] == Collection Framework == A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following: * '''Interfaces:''' These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy. * '''Implementations:''' These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures. * '''Algorithms:''' These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.Β Apart from the Java Collections Framework, the best-known examples of collections frameworks are the C++ Standard Template Library (STL) and Smalltalk's collection hierarchy. Historically, collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve. '''''This information was referenced from [http://java.sun.com Sun Microsystems Java Documenation]''''' ==== Core Collection Interfaces ==== Java collection framework is made up of core interfaces which allow the user to implement different types of collections based on these interfaces. The core interfaces form a hierarchy which makes it easy to implement the type of collection you need. The list below describes the core collection interfaces:- '''Collection''' - the root of the collection interfaces. This interface describes a group of objects known as elements. The collection interface is the most general interface and does not have any direct implementations in the java language. If you data structure requires very general data storage this interface will allow you to implement any type of collection of data. The java language uses this interface to provide higher level collections such as Set and List which provide more specific functionality and algorithms. '''Set''' - a set interface is a collection of data that does not allow duplicate entries. A set could be compared to a set of baseball cards. When you have the complete set you have one of each card in that series. '''List''' - a list interface is a collection that allows storage of elements usually in a sequential order. List allow duplicate entries and allow you to retrieve data in the collection by sequence numbers. '''Map''' - a map interface allows storage of elements by mapping a value to a key. Maps do not allow duplicate keys because they use one to one mappings. '''SortedSet''' - a set interface that stores its elements in ascending order. SortedSets are ordered by using works or natual ordering processes. '''SortedMap''' - a map interface that sorts its keys in ascending order. SortedMaps can be used for things like addressbooks or music listings. ==== Set Example ==== <code>package wikia; </code> <code>import java.util.HashSet; import java.util.Iterator; import java.util.Set; </code> <code>public class SetExample { </code> <code> /** Creates a new instance of SetExample */ public SetExample() { // Create a new Set object Set landRoverSet = new HashSet(); // Populate the set landRoverSet.add("Discovery"); landRoverSet.add("Range Rover Sport"); landRoverSet.add("LR3"); landRoverSet.add("LR2"); landRoverSet.add("Range Rover"); landRoverSet.add("D90"); // Get the number of elements in the set System.out.println("The land rover set size is now " + landRoverSet.size()); // Remove an element from the set landRoverSet.remove("D90"); // Check the size again System.out.println("The land rover set size is now " + landRoverSet.size()); // Attempt to add an element to the set that already exist landRoverSet.add("Discovery"); // The addition will have not effect System.out.println("The land rover set size is now " + landRoverSet.size()); // Find out if the element is in the set if(landRoverSet.contains("LR3")) { System.out.println("The LR3 is inside the set"); } else { System.out.println("The LR3 is not inside the set"); } // Iterate over the set and print out all values Iterator setIter = landRoverSet.iterator(); while(setIter.hasNext()) { Object setValue = setIter.next(); System.out.println("Land Rover: " + setValue); } // Convert the set into an array String[] landRoverArray = (String[])(new String[landRoverSet.size()]); } </code> <code>} </code> ==== List Example ==== <code>package wikia; </code> <code>import java.util.ArrayList; import java.util.Iterator; import java.util.List; </code> <code>public class ListExample { </code> <code> /** Creates a new instance of ListExample */ public ListExample() { // Create a List object List landRoverList = new ArrayList(); // Populate the list landRoverList.add("Discovery"); landRoverList.add("Range Rover Sport"); landRoverList.add("LR3"); landRoverList.add("LR2"); landRoverList.add("D90"); // Get the size of the list System.out.println("The size of the list is " + landRoverList.size()); // Remove an element from the list landRoverList.remove("D90"); // Get the size again System.out.println("The size of the list is " + landRoverList.size()); // Add a duplicate element landRoverList.add("LR2"); // Check the size to see if it allows duplicates System.out.println("The size of the list is " + landRoverList.size()); // Iterator over the collection/list Iterator listIter = landRoverList.iterator(); while(listIter.hasNext()) { Object listValue = listIter.next(); System.out.println("The value of the list is: " + listValue); // Check to see if the list value is Discovery if it is remove if(listValue.equals("Discovery")) { System.out.println("Removing " + listValue); listIter.remove(); } } } </code> <code>} </code> == Arrays == ==== Problem ==== When you need to store a set amount of data and retrieve it in a sequential order (usually). ==== Solution ==== Use an array ==== Description ==== An array is an object in java that stores a preset amount of data in a sequential order. For example lets say we have an application that needs to allow the user to choose between 5 different colors. We know that there will always only be 5 allowed colors and we know exactly what colors they are and what order they should be in. Lets take this knowlege and create a simple application that utilizes an array as a data structure to store the needed color options. ==== Example ==== <code> package wikia; import java.util.Arrays; public class ColorPickerArrayExample { private final String[] colors = { "Black", "Blue", "Red", "Green", "Brown" }; /** Creates a new instance of ColorPickerArrayExample */ public ColorPickerArrayExample() { printColorsInOrder(); printSelectColors(); } private void printColorsInOrder() { // Print out all colors in the order they are stored System.out.println("Your color choices are: "); for(int i=0; i < colors.length; i++) { System.out.println("Color: " + colors[i]); } } private void printSelectColors() { // Print out colors by selectivly picking an index System.out.println("Your reverse color choices are: "); // Lets get the value of 3 System.out.println("Color: 3 " + colors[2]); // Lets get the value of 5 System.out.println("Color 5: " + colors[4]); // Lets get the value of 1 System.out.println("Color 1: " + colors[0]); } } </code> [[Category:Tutorials]] [[Category: Data Structure]]
Summary:
Please note that all contributions to the Java Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Follow on IG
TikTok
Join Fan Lab