Understanding Java Collection ( Part 1 )

             What is Java Collections ?

Java collections are data structures  for  storing and processing set of datas.

How does java collections work?

It hide the complexity of data  size resizing and provide dynamic size
using internal array.

 Why do we need Java collections ?

1. When there is a need for dynamic  resizing of data

2.When you want to process objects of  different types with compile time safety

Types of Java collections ?

2017-02-20-at-17-02-56

( 1)

Example for List collection

Task A = "Develop a console application 
for a school data in .docx for extraction and manipulation of each 
student bio data and class performance."

Question : Provide a solution to Task A
StudentsData.docx
studentdataindocx

Solution 1 . Create a java class to read the docx file
package com.myexam.app;
import java.io.*;
import java.util.*;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class DocParser<T>
{
    public Collection<T> parse(String fileNameorFilePath )
    {
        Collection<String> fileContent = new ArrayList<>();
        try {
            XWPFDocument docx = new XWPFDocument(new FileInputStream(fileNameorFilePath));
            XWPFWordExtractor we = new XWPFWordExtractor(docx);
            fileContent.add(we.getText());
        }
        catch ( Exception error )
        {
            throw new RuntimeException(error);
        }
        return (Collection<T>) fileContent;
    }

}

The focus here is to show the  basic implementation of  Java collections  framework , not how to read docx or  doc file , you may read this on how to read , write , delete and update a doc ,docx xml, and ppt file…
To process any data in java collection  framework, you need to use the inbuilt  methods

package com.myexam.app;
import java.util.*;
public class Student
{
    private int age , defStudentSize = 1;
    List studentInfo = new ArrayList<>(defStudentSize);
    private String fileName;
    Scanner input = new Scanner(System.in);

    private DocReader docReader = new DocReader();
    public String getStudentInfo( )
    {
        getFileName();
        return String.valueOf ( studentInfo.add (docReader.parse( fileName )));
    }
    public void getFileName()
    {
        System.out.println("Enter file name or file llocation : ");
        fileName = input.next();
        List ok = Arrays.asList("ooo","allsl","skksksks");
    }
}

Line 5  create a studentSize of 1

Line 6 ,  parse ( or use ) the created  defDataSize to the ArrayList argument i.e

    List studentInfo = new ArrayList<>(1);

Now, if the data is more than one, the size  changes dynamically

line 6 , is a variable  studentInfo of String type inside a List  container , then instantiates it with  ArrayList ( Read this on  where and when to use ArrayList,  LinkedList… with List or any other  collections frmaework )
IF YOU DID NOT INSTANTIATE THE COLLECTION, YOU WILL GET NULL EXCEPTION ERROR while excecuting this function;

line 14  make use of the list method ( add(whatToAdd) )

return String.valueOf ( studentInfo.add (docReader.parse( fileName )));

the add method take a String value because  we specify the Container type on line
6 to be String ( we can change it to any type  ):)

and likewise , the docReader has a method  parse which also take up a String value for
the file location.

To return List studentInfo inside  the getStudentInfo method , we have to cast it
to the method return type .

// To run the program
package com.mytask.taska;
import java.util.*;
public class School
{
    private static Student student = new Student();
    public static void main ( String[] args )
    {
        student.getStudentInfo(); // This  method will request for the file location and process it ( not print it )
        student.studentInfo.forEach(System.out::println); // This  method will print the file content out as saved
    }
}



Ouput...


2017-02-20-at-16-12-26

Again , this article is not about casting,  you may read more on casting here… Suppose , we want to process the data , we  need specific regex for each field  and probably group them

You may read this on how to group data based on regex pattern

With our regex , we may use the List method to delete , add ,  remove and update the data

Task 2 : Match and group each student data field

Let’s build REGEX pattern for the docx file

Regex is simple , tricky and complex

This are the basic REGEX syntax

. = ANy charater except break line

\\d = Any digit   \\D = any non digit

\\w = any word     \\W = any non word

\\s = any space     \\S = any non space

+ = One or more time      \  = Match escape special character

* = zero or more time
[abcdf]* = single group matching
([abcdef]*) =  group matching
(abcd) = group matching 
^ Begging of line
$End of line 
| and
may read this on regex

To matching out the student field data

1. We have to study the text appearance
2. Build regex pattern for each filed

Now , filed one we have Name

Name can start with anything

so , we have ^[.]*(\\s*)*(.)[^\\d]{2}

Now this regex means

Start matching from the beginning of Any  character , match any space ( zero or one )
, match any character,….but stop at where  digit is two

we can repeat for other filed as …

Interesting ? Read part two of java collection

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: