Java - The Set Interface
Example of Collection -- ArrayList
package org.collection;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Class1 {
public Class1() {
}
public static void main(String[] args) {
List l = new ArrayList();
l.add(new Integer(20));
l.add(new Float(69.40));
l.add(new Integer(56));
l.add(new Double(49399.9929300));
List<String> l1 = new ArrayList<String>();
l1.add("Aman Kumar");
l1.add("Nitin Rajput");
l1.add("Mukesh Kumar");
l1.add("Nidhi Goyal");
l1.add("Brijesh Bhagat");
l1.add("Kapil vardhan");
l1.add("Kapil Gupta");
l1.add("Chetan Shishodia");
l1.add("Kapil Kumar");
System.out.println("Before Sort :"+l1);
Collections.sort(l1);
System.out.println("After Sort :"+l1);
}
}
// ------------ Search Elements in Array List -------------- //
package arraylist;
import java.util.ArrayList;
public class SearchAnElementInArrayListExample {
public static void main(String args[]){
ArrayList arraylist= new ArrayList();
arraylist.add("5");
arraylist.add("10");
arraylist.add("1");
arraylist.add("4");
arraylist.add("8");
arraylist.add("3");
arraylist.add("9");
arraylist.add("2");
boolean found = arraylist.contains("40");
System.out.println("Does ArrayList contains 4 "+found);
int index = arraylist.indexOf("80");
if(index == -1)
System.out.println("element 80 not found...");
else
System.out.println("element 80 found...");
int lastindex = arraylist.lastIndexOf("9");
if(lastindex== -1)
System.out.println("element 9 not exist...");
else
System.out.println("element 9 is exist");
}
}
No comments:
Post a Comment