Click here to Skip to main content
15,911,891 members
Home / Discussions / Java
   

Java

 
AnswerRe: java programmimg Pin
Member 1494135617-Sep-20 19:39
Member 1494135617-Sep-20 19:39 
AnswerRe: java programmimg Pin
Sandeep Mewara17-Sep-20 20:23
mveSandeep Mewara17-Sep-20 20:23 
AnswerRe: java programmimg Pin
Gerry Schmitz18-Sep-20 0:44
mveGerry Schmitz18-Sep-20 0:44 
GeneralRe: java programmimg Pin
Dave Kreskowiak18-Sep-20 4:33
mveDave Kreskowiak18-Sep-20 4:33 
QuestionIntegrating keycloak with spring security Pin
arunken16-Sep-20 21:30
arunken16-Sep-20 21:30 
QuestionHelp me! Pin
Member 1492738231-Aug-20 8:30
Member 1492738231-Aug-20 8:30 
AnswerRe: Help me! Pin
ZurdoDev31-Aug-20 9:37
professionalZurdoDev31-Aug-20 9:37 
AnswerRe: Help me! Pin
ZurdoDev31-Aug-20 9:38
professionalZurdoDev31-Aug-20 9:38 
AnswerRe: Help me! Pin
Afzaal Ahmad Zeeshan31-Aug-20 17:52
professionalAfzaal Ahmad Zeeshan31-Aug-20 17:52 
AnswerRe: Help me! Pin
Richard MacCutchan31-Aug-20 21:11
mveRichard MacCutchan31-Aug-20 21:11 
QuestionHow can I solve the following error: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext; Pin
Member 1469623621-Jul-20 18:41
Member 1469623621-Jul-20 18:41 
AnswerRe: How can I solve the following error: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext; Pin
Richard MacCutchan21-Jul-20 21:41
mveRichard MacCutchan21-Jul-20 21:41 
AnswerRe: How can I solve the following error: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext; Pin
ZurdoDev31-Aug-20 9:37
professionalZurdoDev31-Aug-20 9:37 
QuestionBLOB TO MySQL Pin
Member 1489223717-Jul-20 6:28
Member 1489223717-Jul-20 6:28 
QuestionJava tuition/courses Pin
jopag80003-Jul-20 8:54
jopag80003-Jul-20 8:54 
AnswerRe: Java tuition/courses Pin
Richard MacCutchan10-Jul-20 22:08
mveRichard MacCutchan10-Jul-20 22:08 
QuestionSSLSocket.getInputStream() hangs when called Java 11 Pin
nrmad1-Jul-20 8:35
nrmad1-Jul-20 8:35 
AnswerMessage Closed Pin
16-Feb-22 23:05
Weent1916-Feb-22 23:05 
AnswerMessage Closed Pin
16-Feb-22 23:05
Weent1916-Feb-22 23:05 
QuestionIntegration Pin
GauravSahu911-Jul-20 0:39
GauravSahu911-Jul-20 0:39 
AnswerRe: Integration Pin
OriginalGriff1-Jul-20 0:41
mveOriginalGriff1-Jul-20 0:41 
QuestionHow to dynamically RDF resources in java ? Pin
Member 1487416726-Jun-20 8:26
Member 1487416726-Jun-20 8:26 
AnswerRe: How to dynamically RDF resources in java ? Pin
Richard MacCutchan26-Jun-20 22:24
mveRichard MacCutchan26-Jun-20 22:24 
QuestionNeed help to simplify the solution Pin
User-862169523-Jun-20 18:02
User-862169523-Jun-20 18:02 
Hi,
I am learning Java and doing some operation related to HashMap. I am trying to add details in hashmap based on city. So suppose below is the list we have :
List<Student> studentList= new LinkedList<>();
        studentList.add(new Student(1, "Test1", "US"));
        studentList.add(new Student(2, "Test2", "US"));
        studentList.add(new Student(3, "Test3", "India"));
        studentList.add(new Student(4, "Test4", "Canada"));
        studentList.add(new Student(5, "Test5", "Canada"));
        studentList.add(new Student(6, "Test6", "India"));

For this I am looking output something like
{US=[Test1, Test2], India=[Test3, Test6], Canada=[Test4, Test5]}
mainly creating map based on Cities.
I have implemented the code for this but it's time complexity is very high so looking for some optimize solution. Can someone Please help.
Java
private void getDetailsGroupByCity(List<Student> studentList)
    {
        Map<String,ArrayList<String>> studentHashMap= new HashMap<>();
        List<Student> arrayList= new ArrayList<>(studentList);
        List<String> names= new ArrayList<>();

        Set<String> setKey= new HashSet<>();
        for (Student student : arrayList) {
            setKey.add(student.getCity());
        }
        for (String s : setKey) {
            for (Student student : arrayList) {
                if (s==student.getCity())
                {
                    names.add(student.getName());
                    studentHashMap.put(s, (ArrayList<String>) names);
                }
            }
            names= new ArrayList<>();
        }

        System.out.println(studentHashMap);
    }


Thanks for your help in advance Smile | :) Happy Learning for me Smile | :)
AnswerRe: Need help to simplify the solution Pin
Richard MacCutchan23-Jun-20 21:53
mveRichard MacCutchan23-Jun-20 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.