Algoritmlash asoslari
Download 1.32 Mb.
|
Algoritmlash asoslari
Set set = hmap.entrySet();
Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry me = (Map.Entry)iterator.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } Map System.out.println("Saralashdan keyin:"); Set set2 = map.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry me2 = (Map.Entry)iterator2.next(); System.out.print(me2.getKey() + ": "); System.out.println(me2.getValue()); } } } Ekranda: Saralashdan oldin: 0: R 4: Z 5: A 66: Q 9: P 77: Y 11: C Saralashdan keyin: 0: R 4: Z 5: A 9: P 11: C 66: Q 77: Y HashMap ni Comerator dan foydalanib, value(qiymat)ini saralashga misol: import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; public class HMapSortingByvalues { public static void main(String[] args) { HashMap hmap.put(5, "A"); hmap.put(11, "C"); hmap.put(4, "Z"); hmap.put(77, "Y"); hmap.put(9, "P"); hmap.put(66, "Q"); hmap.put(0, "R"); System.out.println("Saralashdan oldin:"); Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry me = (Map.Entry)iterator.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } Map System.out.println("Saralashdan keyin:"); Set set2 = map.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry me2 = (Map.Entry)iterator2.next(); System.out.print(me2.getKey() + ": "); System.out.println(me2.getValue()); } } private static HashMap sortByValues(HashMap map) { List list = new LinkedList(map.entrySet()); // Bu yerda custom komparator aniqlandi Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()) .compareTo(((Map.Entry) (o2)).getValue()); } }); // Bu yerda men Hashmap'da tartiblangan ro'yhatni ko'chiryapman // LinkedHashMap'dan foydalanib, kiritish tartibini saqlash uchun HashMap sortedHashMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); sortedHashMap.put(entry.getKey(), entry.getValue()); } return sortedHashMap; } } ekranda: Saralashdan oldin: 0: R 4: Z 5: A 66: Q 9: P 77: Y 11: C Saralashdan keyin: 5: A 11: C 9: P 66: Q 0: R 77: Y 4: Z TreeMap TreeMap ham huddi TreeSetga o'xshab ma'lumotlarni saralaydi, farqi TreeMap keybo'yicha saralaydi. TreeMap ham huddi HashMap'ga o'xshab Map'dan implement oladi. Farqi HashMap ma'lumotlarni saralab chiqarmaydi. Key bo'yicha saralashga misol: import java.util.TreeMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class Details { public static void main(String args[]) { /* Bu yerda TreeMap'ni qanday e'lon qilish */ TreeMap new TreeMap /*Adding elements to TreeMap*/ tmap.put(1, "Data1"); tmap.put(23, "Data2"); tmap.put(70, "Data3"); tmap.put(4, "Data4"); tmap.put(2, "Data5"); /* Iterator'dan foydalangan holda kontentni ko'rsatish */ Set set = tmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mentry = (Map.Entry)iterator.next(); System.out.print("key is: "+ mentry.getKey() + " & Value is: "); System.out.println(mentry.getValue()); } } } ekranda: key is: 1 & Value is: Data1 key is: 2 & Value is: Data5 key is: 4 & Value is: Data4 key is: 23 & Value is: Data2 key is: 70 & Value is: Data3 ko'rib turganingizdek ma'lumotlar key bo'yicha saralanib chiqarildi. TreeMap'da ham elementlarni olish, qo'shish jarayonlari huddi HashMapga o'xshash bo'ladi. 27-dars. Javada Stringlar JAVADA STRING Javada String asosan char tipidagi qiymatlar ketma-ketligini ifodalovchi ob’yekt hisoblanadi. Belgili massiv esa javadagi String kabi ishlaydi, ya`ni:
1. char[] ch={'j','a','v','a','t','p','o','i','n','t'}; 2. String s=new String(ch); Kodimiz
1. String s="javatpoint"; Kabi ishlaydi. Java String klasi string ustida compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() kabi bir necha funksiyalarni ishlatib bir qator amallarni bajarish imkonini beradi. java.lang.String klasi Serializable, Comparable va CharSequence interfeyslarini implement qiladi. CharSequence interfeysi belgilar ketma-ketligini ifodalashda ishlatiladi. U String, StringBuffer va StringBuilder klaslari tomonidan implement qilinadi. Bundan kelib chiqadiki, biz Javada stringni shu uch klasdan istalgani bilan yasay olamiz. String doimiydir, ya’ni uni o`zgartirib bo`lmaydi. Qachonki biz stringga o’zgartirish kiritsak, yangi instance variable – doimiy qiymat hosil qilinadi. O`zgaruvchan string uchun StringBuffer va StringBuilder klaslaridan foydalanish mumkin. Biz o`zgarmas(immutable) stringlarga keyinroq to`xtalamiz. Avval biz javadagi string haqida to`liq ma’lumot olamiz.
Funksiyalarga misollar Java String contains()
1. class ContainsExample{ 2. public static void main(String args[]){ 3. String name="what do you know about me"; 4. System.out.println(name.contains("do you know")); 5. System.out.println(name.contains("about")); 6. System.out.println(name.contains("hello")); 7. } 8. } Java String replace(CharSequence target, CharSequence replacement)
1. public class ReplaceExample2{ 2. public static void main(String args[]){ 3. String s1="my name is khan my name is java"; 4. String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was" 5. System.out.println(replaceString); 6. } 7. } Java String equalsIgnoreCase()
1. public class EqualsIgnoreCaseExample{ 2. public static void main(String args[]){ 3. String s1="javatpoint"; 4. String s2="javatpoint"; 5. String s3="JAVATPOINT"; 6. String s4="python"; 7. System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same 8. System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored 9. System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same 10. } 11. } Java String valueOf()
1. public class StringValueOfExample{ 2. public static void main(String args[]){ 3. Download 1.32 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling