Вы находитесь на странице: 1из 6

String str=new String(“Some value”);

//2 objects and one reference variable (pool + non pool)

String str2=”Some value”


// 1 object and one reference variable

String str3=str;
// then str3=str because 2 string objects referred to same
heap memory.

str.equalsIgnoreCase(str2) is true
// Value is same

str. concat(“add some”);


// System.out.println(str) is “Some value” only
str=str.concat(“add some”);
// System.out.println(str) is “Some value add some”
// In this case new String object will be created with value
“Some value add some” and str referred variable referred to
this and old String also available in memory but its lost
reference.

// String Buffer and String Builder (After 1.5 and not a


thread safe) are mutable String objects.
StringBuffer sb=new StringBuffer(“value1”);
sb.append(“value2”); // Then sb is value1value2

// objectoutputstream.writeObject( ) – Serialize and write


// objectinputstream.readObject( ) – read and deserialize
Ex:
FileOutputStream fos=new FileOutputStream(“test.ser”);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(object1);

FileInputStream fis=new fileInputStream(“test.ser”);


ObjectInputStream ois=new ObjectInputStream(fis);
Object1=(class1) ois.readObject();

Scanner class to searching


Scanner sc=new Scanner(System.in);
String Token;
Do{
Token=sc.findLine(args[0]);
System.out.println(“found:”+Token);
}while(Token != null) ……………………
}

Tokenizing with String split // Ie for basic tokenizing


String[] tokens=args[0].split(args[1]);
For(String s: tokens){
System.out.println(s);}

System.out.printf("%2$d + %1$d",333,456);
Out put is 456 + 123

Instance variables and objects live in heap. Local variables


live in stack.

Hashcode used to improve performance of large collections.


Need not unique but almost unique.

Hashcode uses to find right object and equals method uses


to find right element.
“Collection” interface has extended by “Set”,“ List”,“ Queue”
interfaces.

“Set” interface extended by “SortedSet” interface.

“Map” interface extended by “sortedMap”.[Not the part of


Collection interface).

“Set” implemented by “HashSet”,”LinkedHashSet”.

“SortedSet” implemented by “TreeSet”.

“List” implemented by “ArrayList”,”Vector” and “LinkedList”.

“Queue” implemented by “LinkedList”,”PriorityQueue”.

“Map” implemented by “Hashtable”,”LinkedHashMap”,


”HashMaP”.

“SortedMap” implemented by “TreeMap”.

Sets – Unique Maps- Unique by Pairs


Queue- ordered List- List Of things

List take care about index.


get(int index), indexOf(Object O), add(int index,Object o)

1. ArrayList you can assume as growable array.


Implements RandomAcessInterface(Marker).

Vector methods are Synchronized. Implements


RandomAcessInterface.

LinkedList ordered by index position and doubly linked.


Insertion and deletion is good. Iteration is bad compare with
ArrayList. From Java5 it implementing Queue for additional
methods.
2. HashSet is unordered.

LinkedHashSet is ordered. U can Iterate through the


insertion order.

TreeSet is ordered in natural means ascending.

3. HashMap is unordered and allowed one null key and


multiple null values.

HashTable is Synchronized one. Doesnot allow null any


where.

LinkedHashMap maintains insertion order.

TreeMap is natural sorted order.

4. PriorityQueue(Java5) in some priority.

import java.util.*;
public class List_al {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> al=new ArrayList<String>();
String str="Hi" ;
al.add("Some");
al.add(str);
al.add(str+str);
System.out.println("Size is"+al.size());
System.out.println(al.contains("Hi"));
System.out.println(al.contains("sss"));
System.out.println(al);
}

Output: Size is3


true
false
[Some, Hi, HiHi]

In java5 autoboxing taken care.


Al.add(42)// Valid

Collections.sort(al)//for Sorting

“Comparable” interface used by Collections.sort( ) and


Arrays.sort( ). We have method “comapreTo()”.
int val=object1.compareTo(object2);

“Comparator” interface gives you the capability of sorting


given collection in any number of different ways.

------------------- implements Comparator{


int compare(dvdinfo one, dvdinfo two){
return one.getxxx().compareTo(two.getxxx());
}
Additonal u can build a class whose instances you have to
sort in the case of “Comparable” you must modify the class.

Arrays.sort(arrayob);
Arrays.sort(arrayob, Comparator)

Arrays.binarysearch(sa,”one”);
//Convert array as List
List al=Arrays.asList(arrayob);

// Convert list as array


ArrayOb= Listob.toArray(ArrayOb);

Вам также может понравиться