import java.util.HashMap; import java.util.Random; public class HashMap1_rand { public static void main(String args[]) { Random rnd=new Random(); HashMap<Integer, Integer> m=new HashMap<Integer, Integer>(); for (int i = 1; i <= 1000; i++) { int r=rnd.nextInt(10); Integer occr= m.get(r); if(occr==null) occr=1; else occr=occr+1; m.put(r,occr ); } System.out.println(m); } }
Output {0=106,1=94,2=100,3=96,4=94,5=108,6=94,7=112,8=93,9=103}
import java.util.*; //Arrays.toString(array Variable): to print all elemetns of array class Subject { String name; Subject(String n) { name=n; } public String toString() { return (name); } } class array { public String toString(){ return "oke";} void primitives() //using Array.toString() { int a[]={2,3,4,5,6,78,8,9}; System.out.println("toString()-Primitives:"+Arrays.toString(a)); } void nonPrimitive() //using Array.toString() { String []ar={"CPP","Java","dotNet"}; String all=Arrays.toString(ar); System.out.println("toString()-Objects:"+all); } //initializing integer values in arraylist void asList_prmtv()//using Array.asList() { List <Integer>lst=new ArrayList<Integer>(Arrays.asList(2,4,5,7,8,9,9)); System.out.println("asList()-Primitive:"+lst); } void asList_objs()// initializing objects in arraylist { Subject s1=new Subject("Java"); Subject s2=new Subject("PHP"); List <Subject>lst=new ArrayList<Subject>(Arrays.asList(s1,s2)); System.out.println("asList()-Objects:"+lst); } void deep()// using Array.deepCopy() { int a[][]={ {3,4,5,6},{7,8,9,10} }; System.out.println("deepCopy():"+Arrays.deepToString(a)); } void fill() // using Arrays.fill() { int a[]=new int[10]; Arrays.fill(a,100); System.out.println("fill():"+Arrays.toString(a)); Arrays.fill(a, 5,10,200);// fill 200 from 5th to 9th index System.out.println("fill():"+Arrays.toString(a)); } void copy()// copy array to other { int a[]={4,6,7,8,9}; int b[]=new int[5]; System.arraycopy(a, 0, b, 0, a.length); System.out.println("arrayCopy():"+Arrays.toString(b)); } void match() { int a[]={1,2,3,4}; int b[]={1,2,3,4}; System.out.println("equals():"+Arrays.equals(a, b)); } void sorting()//sorting array { int a[]={1,12,8,4}; Arrays.sort(a); System.out.println("sort():"+Arrays.toString(a)); //- desc order String names[]={"Raka","shaka","sandy","aman"}; Arrays.sort(names, Collections.reverseOrder()); System.out.println("rev. sort():"+Arrays.toString(names)); //--------------------------ignore case matter-------- String names3[]={"Raka","shaka","sandy","aman"}; Arrays.sort(names3,String.CASE_INSENSITIVE_ORDER); System.out.println("ign. case sort():"+Arrays.toString(names3)); } void Pmtv_search()// binary search { int qty[]={4,6,78,99,100,800}; int pos= Arrays.binarySearch(qty, 99); System.out.println("binarySearch():"+pos); } void shuffels() { ArrayList<String> nos=new ArrayList<String>( Arrays.asList("101","103","104","106")); Collections.shuffle(nos,new Random()); System.out.println("suffle():"+ nos); } } public class Array_A_CLass { public static void main(String[] args) { array a=new array(); a.primitives(); a.nonPrimitive(); a.asList_prmtv(); a.asList_objs(); a.deep(); a.fill(); a.copy(); a.match(); a.sorting(); a.shuffels(); a.Pmtv_search(); } }
Output toString()-Primitives:[2,3,4,5,6,78,8,9] toString()-Objects:[CPP, Java, dotNet] asList()-Primitive:[2, 4, 5, 7, 8, 9, 9] asList()-Objects:[Java, PHP] deepCopy():[[3, 4, 5, 6],[7, 8, 9, 10]] fill():[100, 100, 100, 100, 100, 100, 100, 100, 100, 100] fill():[100, 100, 100, 100, 100, 200, 200, 200, 200, 200] arrayCopy():[4, 5, 6, 7, 8, 9] equals():true sort():[1, 4, 8, 12] rev.sort():[shaka, sandy, aman, Raka] ignore-case sort():[aman, Raka, sandy, shaka] suffle():[103, 106, 101, 104] binarySearch(): 3
import java.util.*; public class Collections_Class { public static void main(String[] args) { ArrayList<String> names=new ArrayList<String>(); names.add("Sandy"); names.add("Mandy"); names.add("Aman"); names.add("Raman"); System.out.println("Unsorted List:"+names); Collections.sort(names); System.out.println("Sorted List:"+names); int pos=Collections.binarySearch(names, "Raman"); System.out.println("Found at:"+pos); Collections.reverse(names); System.out.println("Reversed List:"+names); Collections.shuffle(names); System.out.println("Shuffled List:"+names); Collections.swap(names, 1, 2); System.out.println("After Swap List:"+names); } }
Output Unsorted List:[Sandy, Mandy, Aman, Raman] Sorted List:[Aman, Mandy,Raman, Sandy] Found at:2 Reversed List:[Sandy, Raman, Mandy, Aman] Shuffled List:[Aman, Mandy, Sandy, Raman] After Swap List:[Aman, Sandy, Mandy, Raman]