Algoritmlash asoslari


Download 1.32 Mb.
bet15/16
Sana12.06.2020
Hajmi1.32 Mb.
#117719
1   ...   8   9   10   11   12   13   14   15   16
Bog'liq
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());

}

MapString> map = new TreeMapString>(hmap);

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) {

HashMapString> hmap = new HashMapString>();

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());

}

MapString> map = sortByValues(hmap);

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 */

TreeMapString> tmap =

new TreeMapString>();

/*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:

Source code

   

1. char[] ch={'j','a','v','a','t','p','o','i','n','t'};

2. String s=new String(ch);

Kodimiz

Source code

   

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.




Funksiyalar

Ta`rifi

1

char charArt(int index)

ma’lum bir index uchun char qiymat qaytaradi

2

int length()

Stringni uzuligini qaytaradi

3

static
String format(String format, Object … args)


String format qaytaradi

4

static String
format(Locale I, String format, Object …args)


Berilgan local string format qaytaradi

5

String substring(int beginIndex)

boshlang’ich index uchun

6

String substring(int beginIndex, int endIndex)

Boshlang’ich va oxirgi index uchun

7

boolean contains(CharSequence s)

Ketma ket kelgan char larnig true yoki false qiymatlarini qaytaradi

8

static String
join(CharSequence delimiter, CharSequence …elements)


Stringlarni qo’shadi

9

static
String join(CharSequence delimiter, Iterableextends CharSequence> elements)


Stringlarni qo’shadi

10

boolean  equals(Object another)

String va ob’yektlarini tekshiradi

11

boolean isEmpty()

Stringni bo’sh ekanini tekshiradi

12

String concat(String str)

Muayyan stringlarni birlashtiradi

13

String replace(char old, char new)

Belgilangan char larni almashtiradi

14

String replace(CharSequence old, CharSequence new)

Ketma ket kelgan char’lari almashtiradi

15

String trim()

Bo’sh joyni tushirib qoldiradi

16

String split(String regex)

Stringlarni teng bo’ladi

17

String split(String regex, int limit)

Stringlarni limit bilan bo’ladi

18

String intern()

String ni biror bir qiymatda ushlab turadi

19

int indexOf(int ch)

Charlarni aniq index ‘ini qaytaradi

20

int indexOf(int ch, int fromIndex)

Charning boshlanish qiymati bilan aniq index qaytaradi

21

int indexOf(String substring)

Stringni qaysi index da turganini qaytaradi

22

int indexOf(String substring, int fromIndex)

Substring ko’rsatkichidan index o’rnini

23

String
toLowerCase()


Harflarni kichigi bilan almashtiradi

24

String toLowerCase(Locale l)

Local berilgan harflarni kichigiga almashtiradi

25

String toUpperCase()

Berilgan harflarni kattasiga almashtiradi

26

String toUpperCase(Locale l)

Local berilgan harflarni kattasiga almashtiradi


Funksiyalarga misollar

Java String contains()

Source code

   

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)

Source code

   

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()

Source code

   

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()

Source code

   

1. public class StringValueOfExample{

2. public static void main(String args[]){

3.
Download 1.32 Mb.

Do'stlaringiz bilan baham:
1   ...   8   9   10   11   12   13   14   15   16




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling