ArrayList holatini optimallashtirish

Mening Arraylarim ro'yxatida ba'zi bir qiymatlar bor va shartlarga asoslanib ba'zi bir maxsus qiymatlarni olib tashlashim kerak. IF holatidan foydalanmoqdaman, lekin taqqoslash uchun juda ko'p shartlar mavjud, shuning uchun taqqoslash vaqtini optimallashtirish kerak. mening ro'yxatim

Msisdn_array = {45,85,79,60,502}

va agar shartlar to'g'ri kelsa, kiritishni olib tashlang.

Quyida shartlar, ushbu shartlarni optimallashtirishning mumkin bo'lgan usullari bormi?

if(Msisdn_array.contains("60") && Msisdn_array.contains("910"))
{
Msisdn_array.remove("60");
Msisdn_array.remove("910");
}

if(Msisdn_array.contains("75") && Msisdn_array.contains("500"))
{
Msisdn_array.remove("75");
Msisdn_array.remove("500");
}

if(Msisdn_array.contains("76") && Msisdn_array.contains("502"))
{
Msisdn_array.remove("76");
Msisdn_array.remove("502");
}

if(Msisdn_array.contains("61") && Msisdn_array.contains("911"))
{
Msisdn_array.remove("61");
Msisdn_array.remove("911");
}

if(Msisdn_array.contains("77") && Msisdn_array.contains("503"))
{
Msisdn_array.remove("77");
Msisdn_array.remove("503");
}

if(Msisdn_array.contains("78") && Msisdn_array.contains("505"))
{
Msisdn_array.remove("78");
Msisdn_array.remove("505");
}

if(Msisdn_array.contains("79") && Msisdn_array.contains("507"))
{
Msisdn_array.remove("79");
Msisdn_array.remove("507");
}

if(Msisdn_array.contains("62") && Msisdn_array.contains("912"))
{
Msisdn_array.remove("62");
Msisdn_array.remove("912");
}

if(Msisdn_array.contains("63") && Msisdn_array.contains("913"))
{
Msisdn_array.remove("63");
Msisdn_array.remove("913");
}

if(Msisdn_array.contains("64") && Msisdn_array.contains("914"))
{
Msisdn_array.remove("64");
Msisdn_array.remove("914");
}

if(Msisdn_array.contains("65") && Msisdn_array.contains("915"))
{
Msisdn_array.remove("65");
Msisdn_array.remove("915");
}

if(Msisdn_array.contains("66") && Msisdn_array.contains("916"))
{
Msisdn_array.remove("66");
Msisdn_array.remove("916");
}

if(Msisdn_array.contains("67") && Msisdn_array.contains("917"))
{
Msisdn_array.remove("67");
Msisdn_array.remove("917");
}

if(Msisdn_array.contains("68") && Msisdn_array.contains("918"))
{
Msisdn_array.remove("68");
Msisdn_array.remove("918");
}

if(Msisdn_array.contains("69") && Msisdn_array.contains("919"))
{
Msisdn_array.remove("69");
Msisdn_array.remove("919");
}

if(Msisdn_array.contains("70") && Msisdn_array.contains("920"))
{
Msisdn_array.remove("70");
Msisdn_array.remove("920");
}

if(Msisdn_array.contains("71") && Msisdn_array.contains("921"))
{
Msisdn_array.remove("71");
Msisdn_array.remove("921");
}

if(Msisdn_array.contains("72") && Msisdn_array.contains("922"))
{
Msisdn_array.remove("72");
Msisdn_array.remove("922");
}

if(Msisdn_array.contains("73") && Msisdn_array.contains("923"))
{
Msisdn_array.remove("73");
Msisdn_array.remove("923");
}

if(Msisdn_array.contains("74") && Msisdn_array.contains("924"))
{
Msisdn_array.remove("74");
Msisdn_array.remove("924");
}

if(Msisdn_array.contains("80") && Msisdn_array.contains("926"))
{
Msisdn_array.remove("80");
Msisdn_array.remove("926");
}

if(Msisdn_array.contains("81") && Msisdn_array.contains("927"))
{
Msisdn_array.remove("81");
Msisdn_array.remove("927");
}

if(Msisdn_array.contains("82") && Msisdn_array.contains("928"))
{
Msisdn_array.remove("82");
Msisdn_array.remove("928");
}
0
removeBoth (String one, String two) usulini yaratib, umumiy mantiqni bajaring. Bu optimallashtirilgan koddan ko'ra o'qish va disk raskadrovka qilishni osonlashtiradi
qo'shib qo'ydi muallif Scary Wombat, manba
OBga kerak bo'lgan ikkita element bor-yo'qligini tekshirish kerak, va agar qilsa, ikkalasini ham olib tashlang, aks holda hech narsa qilmang. Shunday qilib, faqat qo'ng'iroqni olib tashlash elementlarning faqat bittasi mavjud bo'lgan holatda kerakli natijani bermayapti.
qo'shib qo'ydi muallif Matt, manba

6 javoblar

Agar potentsial optimallashtirish, siz ikkinchi maqola ro'yxatda mavjud emasligini tekshirishingiz shart emas. Buning o'rniga uni olib tashlashga urinib ko'ring. Agar u o'chirilsa, remove usuli to'g'ri qaytadi va siz shuningdek birinchi elementni .

if(Msisdn_array.contains("60") && Msisdn_array.remove("910")){
    Msisdn_array.remove("60");
}

Agar har ifodani yozishni istamasangiz, siz guruhlarni xaritada saqlay olasiz, birinchi element esa kalit va ikkinchi qiymat esa qiymat sifatida saqlanadi.

Map rules = new HashMap<>();
rules.put("60", "910");
rules.put("75", "500");
rules.put("76", "502");
...
...
for(Entry entry : rules.values()) {
    if(Msisdn_array.contains(entry.getKey()) && Msisdn_array.remove(entry.getValue())){
        Msisdn_array.remove(entry.getKey());
    }
}
1
qo'shib qo'ydi

Menimcha, barcha guruhdagi qadriyatlar ro'yxatda mavjudligini tekshirish va keyin hammasini olib tashlash uchun bitta usulni ajratish kerak. Misol uchun:

    private void removeIfAllExist(List list, String[] values) {
    for (String v : values) {
        if (!list.contains(v)) {
            return;
        }
    }
    list.removeAll(Arrays.asList(values));
}

public void yourLogic() {
    List list = new ArrayList<>(Arrays.asList("45", "85", "79", "60", "502"));
    String[][] conditions = new String[][]{
            new String[]{"60", "910"},
            new String[]{"75", "500"},
            new String[]{"76", "502"},
            new String[]{"61", "911"},
            new String[]{"77", "503"},
           //more conditions
    };
    for (String[] values : conditions) {
        removeIfAllExist(list, values);
    }
}
1
qo'shib qo'ydi

List.contains tufayli sekinlashdi va List.remove ham sekinroq bo'lgani uchun (bu bo'shliqni to'ldirish uchun kattaroq indeksli barcha elementlarni ko'chirish kerak). Kodni takrorlash sababli u yomon.

Msisdn_array is against Java naming convention and it's no array, so lets call it inputList. Let's use a HashSet for the lookups and another one to track what should be removed.

class Worker {
    private final Set inputListAsSet = new HashSet<>();
    private final Set removals = new HashSet<>();

    public static void process(List inputList) {
        final Worker worker = new Worker(inputList);
        worker.removeIfBothPresent("60", "910");
        worker.removeIfBothPresent("75", "500");
        worker.removeIfBothPresent("76", "502");
        worker.removeIfBothPresent("61", "911");
        worker.removeIfBothPresent("72", "922");
        worker.removeIfBothPresent("73", "923");
        worker.removeIfBothPresent("74", "924");
        worker.removeIfBothPresent("80", "926");
        worker.removeIfBothPresent("81", "927");
        worker.removeIfBothPresent("82", "928");
        inputList.removeAll(worker.removals);
    }

    private Worker(List inputList) {
        inputListAsSet.addAll(inputList);
    }

    private void removeIfBothPresent(String first, String second) {
        if (inputListAsSet.contains(first) && inputListAsSet.contains(second)) {
            removals.add(first);
            removals.add(second);
        }
    }
}

Sinf namunasini ishlatish o'rniga, to'siq argument sifatida qabul qilinishi mumkin, ammo ishchi yaratish tozalovchi ko'rinadi. Optimallashuvlarga qaramasdan, kodim asli aslidan ham qisqa. Ikki nusxada bo'lsa, u to'liq teng emas.

0
qo'shib qo'ydi

olib tashlash kabi boolean muvaffaqiyatli o'chirilishi mumkin

if (list.remove ("60") && list.remove ("90"))
{
  //everything ok
}
else {
 //put back "60"
  list.add ("60");
}

but personally I would go for readability a just create a method

removeBoth (String one, String two) {
  if(list.contains(one) && list.contains(two))
  {
     list.remove(one);
     list.remove(two);
  }
}
0
qo'shib qo'ydi

contains is an O(n) operation. The list is traversed until the element is found. So every time you call contains you are potentially traversing the entire list.

Optimallashtirish ro'yxatni bir marta qo'lda kesib o'tish va mavjud elementlarning mavjudligini tekshirish va so'ngra boolean o'zgaruvchilarni saqlash uchun qo'shimcha xotira xarajatlaridan keyin olib tashlashingiz mumkin:

boolean exists_72 = false;
boolean exists_922 = false;

for(String element : Msisdn_array) {
    if(element.equals("72")) {
        exists_72 = true;
    } else if(element.equals("922")) {
        exists_922 = true;
    }
}

if(exists_72 && exists_922) }
    Msisdn_array.remove("72");
    Msisdn_array.remove("922");
}
0
qo'shib qo'ydi

Ro'yxatda tez-tez yineleme qilmasangiz, sizga Set-dan foydalanishingizni tavsiya qilaman. Ushbu to'plamda qidiruv tezkor.

0
qo'shib qo'ydi