while(iterator2.hasNext()) {
Map.Entry mentry2 = (Map.Entry)iterator2.next();
System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
System.out.println(mentry2.getValue());
}
}
}
ekranda:
key is: 49 & Value is: Fayzullo
key is: 2 & Value is: Zafar
key is: 3 & Value is: Jamshid
key is: 7 & Value is: Jalol
key is: 12 & Value is: Aziz
Map key and values after removal:
Key is: 49 & Value is: Fayzullo
Key is: 2 & Value is: Zafar
Key is: 7 & Value is: Jalol
Key is: 12 & Value is: Aziz
HashMap'ni elementlarni olishining ikki xil usuli bor
For loop orqali
While loop + iterator
misol:
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
public class Details
{
public static void main(String [] args)
{
HashMapString> hmap = new HashMapString>();
// HashMap'ga elementlarni qo'shish
hmap.put(11, "AB");
hmap.put(2, "CD");
hmap.put(33, "EF");
hmap.put(9, "GH");
hmap.put(3, "IJ");
// FOR sikli
System.out.println("For Loop:");
for (Map.Entry me : hmap.entrySet()) {
System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());
}
//WHILE sikli & ITERATOR
System.out.println("While Loop:");
Iterator iterator = hmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry me2 = (Map.Entry) iterator.next();
System.out.println("Key: "+me2.getKey() + " & Value: " + me2.getValue());
}
}
}
ekranda:
For sikli:
Key: 2 & Value: CD
Key: 3 & Value: IJ
Key: 33 & Value: EF
Key: 9 & Value: GH
Key: 11 & Value: AB
While sikli:
Key: 2 & Value: CD
Key: 3 & Value: IJ
Key: 33 & Value: EF
Key: 9 & Value: GH
Key: 11 & Value: AB
HashMap'ni key orqali saralash
Bu misolda HashMap'ning key'ini TreeMap'dan foydalanib saralash usulini ko'ramiz.
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;
import java.util.Iterator;
public class Details {
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 = 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 key bo'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.
26-dars - Javada Stringlar.
JAVADA STRING
Javada String asosan char tipidagi qiymatlar ketma-ketligini ifodalovchi obyekt hisoblanadi. Belgili massiv esa javadagi String kabi ishlaydi, ya`ni:
char[] ch={'j','a','v','a','t','p','o','i','n','t'};
String s=new String(ch);
Kodimiz
String s="javatpoint";
Kabi ishlaydi.
Java String klassi 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 klassi Serializable, Comparable va CharSequence interfeyslarini implement qiladi.
CharSequence interfeysi belgilar ketma-ketligini ifodalashda ishlatiladi. U String, StringBuffer va StringBuilder klasslari tomonidan implement qilinadi. Bundan kelib chiqadiki, biz Javada stringni shu uch klassdan 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 klasslaridan foydalanish mumkin.
Biz o`zgarmas(immutable) stringlarga keyinroq to`xtalamiz. Avval biz javadagi string haqida to`liq ma'lumot olamiz.
No
|
Funksiyalar
|
Ta`rifi
|
1
|
char charArt(int index)
|
ma'lum bir indeks uchun char qiymat qaytaradi
|
2
|
int length()
|
Stringni uzunligini 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 indeks uchun
|
6
|
String substring(int beginIndex, int endIndex)
|
Boshlang'ich va oxirgi indeks 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, Iterable
extends CharSequence> elements)
|
Stringlarni qo'shadi
|
10
|
boolean equals(Object another)
|
String va obyektlarini 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 indeksini qaytaradi
|
20
|
int indexOf(int ch, int fromIndex)
|
Charning boshlanish qiymati bilan aniq indeks qaytaradi
|
21
|
int indexOf(String substring)
|
Stringni qaysi indeks da turganini qaytaradi
|
22
|
int indexOf(String substring, int fromIndex)
|
Substring ko'rsatkichidan indeks 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()
class ContainsExample{
public static void main(String args[]){
String name="what do you know about me";
System.out.println(name.contains("do you know"));
System.out.println(name.contains("about"));
System.out.println(name.contains("hello"));
}
}
Java String replace(CharSequence target, CharSequence replacement)
public class ReplaceExample2{
public static void main(String args[]){
String s1="my name is khan my name is java";
String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was"
System.out.println(replaceString);
}
}
Java String equalsIgnoreCase()
public class EqualsIgnoreCaseExample{
public static void main(String args[]){
String s1="javatpoint";
String s2="javatpoint";
String s3="JAVATPOINT";
String s4="python";
System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same
System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored
System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same
}
}
Java String valueOf()
public class StringValueOfExample{
public static void main(String args[]){
int value=30;
String s1=String.valueOf(value);
System.out.println(s1+10);//concatenating string with 10
}
}
String charAt()
public class CharAtExample{
public static void main(String args[]){
String name="javatpoint";
char ch=name.charAt(4);//to'rtinchi elementini qaytaradi
System.out.println(ch);
}
}
Natija: t
String length()
public class LengthExample{
public static void main(String args[]){
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//javatpoint ning uzunligi 10 ga teng
System.out.println("string length is: "+s2.length());// python uzunligi 6 ga teng
}
}
Natija:
string length is: 10
string length is: 6
String format()
public class FormatExample{
public static void main(String args[]){
String name="sonoo";
String sf1=String.format("name is %s",name);
String sf2=String.format("value is %f",32.33434);
String sf3=String.format("value is %32.12f",32.33434);
System.out.println(sf1);
System.out.println(sf2);
System.out.println(sf3);
}}
Natija:
name is sonoo
value is 32.334340
value is 32.334340000000
String substring()
public class SubstringExample{
public static void main(String args[]){
String s1="javatpoint";
System.out.println(s1.substring(2,4));//va ni qaytaradi
System.out.println(s1.substring(2));// vatpoint ni qaytaradi
}}
Natija:
va
vatpoint
String join()
public class StringJoinExample{
public static void main(String args[]){
String joinString1=String.join("-","welcome","to","javatpoint");
System.out.println(joinString1);
}}
Natija:
welcome-to-javatpoint
String equals()
public class EqualsExample{
public static void main(String args[]){
String s1="javatpoint";
String s2="javatpoint";
String s3="JAVATPOINT";
String s4="python";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false
System.out.println(s1.equals(s4));//false
}}
Natija: true false false String isEmpty()
String isEmpty()
public class IsEmptyExample{
public static void main(String args[]){
String s1="";
String s2="javatpoint";
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}}
Natija: true false
String concat()
public class ConcatExample{
public static void main(String args[]){
String s1="java string";
s1.concat("is immutable");
System.out.println(s1);
s1=s1.concat(" is immutable so assign it explicitly");
System.out.println(s1);
}}
Natija: java string java string is immutable so assign it explicitly
String replace(char old, char new)
public class ReplaceExample1{
public static void main(String args[]){
String s1="javatpoint is a very good website";
String replaceString=s1.replace('a','e');// hamma 'a' larni 'e' ga almashtirdi
System.out.println(replaceString);
}}
Natija: jevetpoint is e very good website
String trim()
Do'stlaringiz bilan baham: |