Sie sind auf Seite 1von 8

The Collections Framework consists of three parts

1. interfaces, the abstract data types that the framework supports. 2. implementations, the concrete versions of these interfaces. 3. algorithms, the predefined actions that can be defined on either the interfaces or their implementations. 1. All implementations are unsynchronized. 2. All implementations are serializable and cloneable 3. All implementations support having null elements. The predefined algorithms for supporting the framework are found in the Collections and Arrays classes.

The framework consists of four core interfaces with two specializations for sorting 1. Collection 2. List 3. Set 4. Map 5. SortedSet 6. SortedMap

Interface type and its implementation


Interface Type Implemented by Set HashSet, LinkedHashSet, EnumSet SortedSet TreeSet List Vector, Stack, ArrayList, LinkedList Queue PriorityQueue, LinkedList Map Hashtable, HashMap, LinkedHashMap, WeakHashMap, IdentityHashMap SortedMap TreeMap

LinkedList Class

The LinkedList class is a doubly linked list, which internally maintains references to the previous and n element at each node in the list. Creating a LinkedList 1. public LinkedList(): creating an empty list 2. public LinkedList(Collection col): copy constructor

Get first and last elements from LinkedList


import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<String> lList = new LinkedList<String>(); lList.add("1"); lList.add("2"); lList.add("3"); lList.add("4"); lList.add("5"); System.out.println("First element of LinkedList is : " + lList.getFirst()); System.out.println("Last element of LinkedList is : " + lList.getLast()); } }

Get elements from LinkedList


import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<String> lList = new LinkedList<String>(); lList.add("1"); lList.add("2"); lList.add("3"); lList.add("4"); lList.add("5"); for (String str: lList) { System.out.println(str); } } }

Get SubList from LinkedList


import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] args) { LinkedList<String> lList = new LinkedList<String>(); lList.add("1"); lList.add("2"); lList.add("3");

lList.add("4"); lList.add("5"); System.out.println(lList); List lst = lList.subList(1, 4); System.out.println(lst); lst.remove(2); System.out.println(lst); System.out.println(lList); } }

Adding Elements: add a single element


Adding the element to the end of the list unless an index is specified.
public boolean add(Object element) public boolean add(int index, Object element)

To Treat the linked list as a stack or queue:


public boolean addFirst(Object element) public boolean addLast(Object element)

The addLast() method is equivalent to call add(), with no index.


import java.util.LinkedList; public class MainClass { public static void main(String[] a) { LinkedList list = new LinkedList(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.addFirst("X"); list.addLast("Z"); System.out.println(list); } }

Creating an ArrayList
1. For the first two constructors, an empty array list is created. 2. The initial capacity is ten unless explicitly specified by using the second constructor.
public ArrayList()

public ArrayList (int initialCapacity)

Input Output There are four types of streams: InputStream, OutputStream, Reader, and Writer. 1. Reader. A stream to read characters. 2. Writer. A stream to write characters. 3. InputStream. A stream to read binary data. 4. OutputStream. A stream to write binary data. For better performance, use the buffered I/O classes: BufferedInputStream, BufferedOutputStream, BufferedReader, and BufferedWriter. Reading from and writing to a stream dictate that you do so sequentially. The java.io.RandomAccessFile is for non-sequential operations. For object serialization and deserialization, you can use the ObjectInputStream and ObjectOutputStream classes.
.Reading Binary Data

You use an InputStream to read binary data.


InputStream | +-- FileInputStream | +-- BufferedInputStream

1. FileInputStream enables easy reading from a file. 2. BufferedInputStream provides data buffering that improves performance. 3. The ObjectInputStream class is used in object serialization. 4.

Writing Binary Data


The OutputStream abstract class represents a stream for writing binary data
OutputStream | +--FileOutputStream | +--BufferedOutputStream.

| +--ObjectOutputStream

1. FileOutputStream provides a convenient way to write to a file. 2. BufferedOutputStream provides better performance. 3. ObjectOutputStream class plays an important role in object serialization. OutputStream The OutputStream class defines three write method overloads, which are mirrors of the read method overloads in InputStream:
public void write (int b) public void write (byte[] data) public void write (byte[] data, int offset, int length)

1. The first overload writes the lowest 8 bits of the integer b to this OutputStream. 2. The second writes the content of a byte array to this OutputStream. 3. The third overload writes length bytes of the data starting at offset offset. 1. close() method closes the OutputStream. 2. flush() method forces any buffered content to be written out to the sink. The File Class A File object represents a file or directory pathname. You can pass an absolute path to a file or directory like this:
File file1 = new File ("C:\\temp\\myNote.txt"); // in Windows File file2 = new File ("/tmp/myNote.txt"); // in Linux/Unix

Testing and Checking File Objects: file name and path


getName(): Returns the name of the file without the path or the directory name
import java.io.File; public class MainClass { public static void main(String[] a) { File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.separator, " File.java"); System.out.println(myFile.getName()); System.out.println(myFile.getPath()); } }

FileInputStream

1. The FileInputStream class is a subclass of InputStream. 2. The FileInputStream class allows you to read binary data sequentially from a file. 3. The FileInputStream class's constructors allow you to pass either a File object or a path to a file.
public FileInputStream (String path) public FileInputStream (File file) import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class MainClass { public static void main(String[] args) { boolean areFilesIdentical = true; File file1 = new File("c:\\file1.txt"); File file2 = new File("c:\\file2.txt"); if (!file1.exists() || !file2.exists()) { System.out.println("One or both files do not exist"); System.out.println(false); } System.out.println("length:" + file1.length()); if (file1.length() != file2.length()) { System.out.println("lengths not equal"); System.out.println(false); } try { FileInputStream fis1 = new FileInputStream(file1); FileInputStream fis2 = new FileInputStream(file2); int i1 = fis1.read(); int i2 = fis2.read(); while (i1 != -1) { if (i1 != i2) { areFilesIdentical = false; break; } i1 = fis1.read(); i2 = fis2.read(); } fis1.close(); fis2.close(); } catch (IOException e) { System.out.println("IO exception"); areFilesIdentical = false; } System.out.println(areFilesIdentical); } }

Create FileInputStream and read, display data


import java.io.FileInputStream;

class FileInputStreamDemo { public static void main(String args[]) throws Exception { FileInputStream fis = new FileInputStream(args[0]); // Read and display data int i; while ((i = fis.read()) != -1) { System.out.println(i); } fis.close(); } }

BufferedInputStream
For faster reading you should use BufferedInputStream to wrap any InputStream. BufferedInputStream has two constructors that accept an InputStream:
public BufferedInputStream(InputStream in) public BufferedInput Stream (Input Stream in, int bufferSize)

For example, the following code wraps a FileInputStream with a BufferedInputStream.


FileInputStream fis = new FileInputStream(aFile); BufferedInputStream bis = new BufferedInputStream (fis);

Then, instead of calling the methods on fis, work with the BufferedInputStream bis.

Read from file with BufferedInputStream


import java.io.BufferedInputStream; import java.io.FileInputStream; public class Main { public static void main(String[] args) throws Exception { byte[] buffer = new byte[1024]; BufferedInputStream bufferedInput = new BufferedInputStream(new FileInputStream("fi lename.txt")); int bytesRead = 0; while ((bytesRead = bufferedInput.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead); System.out.print(chunk); } bufferedInput.close(); }

Das könnte Ihnen auch gefallen