मैं जावा रेस्टफुल एपीआई में सुरक्षा के साथ स्प्रिंग बूट का उपयोग कर रहा हूं। मैं अपनी आवश्यकता के अनुसार उचित प्रतिक्रिया चाहता हूं। मैं आपको अपनी डेटाबेस तालिका दिखाऊंगा और प्रतिक्रिया की आवश्यकता होगी। मैं आपको अपना आजमाया हुआ कोड भी दिखाऊंगा। मैंने कोशिश की है लेकिन मेरी आवश्यकता के अनुसार प्रतिक्रिया उचित नहीं हो रही है। किसी के पास विचार है ?? कैसा कैसे करूं ?

  1. डेटाबेस टेबलयह मेरी इवेंट टाइप टेबल है

  2. मुझे उपरोक्त तालिका से यह प्रतिक्रिया चाहिए

"eventColorLegends": [
    {
      "color": "Red",
      "items": [
        "Musician1",
        "Workshop",
        "Seminart"
        ]
    },
    {
      "color": "Black",
      "items": [
        "Product Launches",
        "Incentive Trips"
      ]
    },
    {
      "color": "Green",
      "items": [
        "Dancing",
        "Rotary - Service Club",
        "Historical/Remembrance"
      ]
    },
    {
      "color": "Yellow",
      "items": [
        "Party",
        "Incubator - Accelerator",
        "Leads - Referral"
      ]
    },
    {
      "color": "Orange",
      "items": [
        "Charity",
        "Non-profit",
        "Rotary - Service Club",
        "Historical/Remembrance",

      ]
    },
    {
      "color": "Coffee",
      "items": [
        "Incubator - Accelerator"
      ]
    },
    {
      "color": "Blue",
      "items": [
        "Leads - Referral",
        "Historical/Remembrance",
        "Rotary - Service Club",
        "For-Profit Company",

      ]
    },
]   
  1. मेरे पास serviceImpl वर्ग में कोड का प्रयास है
public GenericResponse findAllTypeOfEvents() {
        try {
            List<TypesOfEvents> typesOfEventsList = typeOfEventsDao.findAll();
            List<String> listEvents = new ArrayList<String>();
            for (int i = 0; i < typesOfEventsList.size(); i++) {
                for (int j = i + 1; j < typesOfEventsList.size()- 1; j++) {
                    if (typesOfEventsList.get(i).getColor().equalsIgnoreCase(typesOfEventsList.get(j).getColor())) {
                        if (typesOfEventsList.get(j).getType().equalsIgnoreCase(typesOfEventsList.get(j).getType())){
                            listEvents.add(i,typesOfEventsList.get(j).getType());
                            System.out.println(typesOfEventsList.get(i).getColor() + " : " + typesOfEventsList.get(j).getType());
                        }
                    }
                }
            }
            return APIResponseBuilder.build(true, listEvents.toArray(), typesOfEventsList.size() > 0 ? commonMessages.findAllTrue : commonMessages.findAllFalse);
        } catch (Exception e) {
            logger.error(commonMessages.findAllException + "TypesOfEvents " + e.getMessage());
            return APIResponseBuilder.build(false, e.getMessage(), commonMessages.exception);
        }
    }

4.मेरी प्रतिक्रिया

"data": [
    "Seminart",
    "For-Profit Company",
    "For-Profit Company",
    "Historical/Remembrance",
    "Leads - Referral",
    "Historical/Remembrance",
    "Historical/Remembrance",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "For-Profit Company",
    "For-Profit Company",
    "For-Profit Company",
    "Historical/Remembrance",
    "Seminart",
    "Seminart",
    "Historical/Remembrance",
    "Workshop",
    "Leads - Referral",
    "Leads - Referral",
    "For-Profit Company",
    "Incubator - Accelerator",
    "Seminart",
    "Historical/Remembrance",
    "Leads - Referral",
    "Leads - Referral",
    "Leads - Referral",
    "Incubator - Accelerator",
    "For-Profit Company",
    "Incubator - Accelerator",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Incubator - Accelerator",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Leads - Referral",
    "Workshop",
    "Seminart",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Non-profit",
    "Incubator - Accelerator",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Leads - Referral",
    "Incubator - Accelerator",
    "Rotary - Service Club",
    "Historical/Remembrance",
    "Historical/Remembrance",
    "Incentive Trips",
    "Workshop",
    "Seminart",
    "Workshop"
  ]
}
0
Rajesh Bhushan 17 अप्रैल 2020, 17:01

1 उत्तर

सबसे बढ़िया उत्तर

प्रत्येक रंग के लिए प्रकार की सूची को मैप करने के लिए एक Map<String, List<String>> बनाएं।

Map<String, List<String>> map = new HashMap<>();
for (int i = 0; i < typesOfEventsList.size(); i++) {
     if (fruits.containsKey(typesOfEventsList.get(i).getColor())){
          List<String> list = map.get(typesOfEventsList.get(i).getColor());
          list.add(typesOfEventsList.get(i).getType());
          map.put(typesOfEventsList.get(i).getColor(), list);
     } else {
         List<String> list =  new ArrayList<String>();
         list.add(typesOfEventsList.get(i).getType());
         map.put(typesOfEventsList.get(i).getColor(), list);
    }
}

फिर कक्षा बनाएं TypeByColor

class TypeByColor {
    String color;
    List<String> items;
}

फिर मानचित्र से सूची बनाएं

    List<TypeByColor> data = new ArrayList<>();
    for (String name : map.keySet())  
    { 
        list.add(new TypeByColor(name,map.get(name)));
    } 
1
Eklavya 20 अप्रैल 2020, 05:23