Click here to Skip to main content
14,580,589 members
   

Java

 
PinnedForum Guidelines - PLEASE READ PinPopular
Chris Maunder28-Jul-09 2:36
cofounderChris Maunder28-Jul-09 2:36 
QuestionMessage Closed Pin
8-Jul-20 23:07
MemberMember 148849118-Jul-20 23:07 
QuestionMessage Closed Pin
5-Jul-20 21:58
Memberathenna5-Jul-20 21:58 
QuestionJava tuition/courses Pin
jopag80003-Jul-20 8:54
Memberjopag80003-Jul-20 8:54 
QuestionSSLSocket.getInputStream() hangs when called Java 11 Pin
nrmad1-Jul-20 8:35
Membernrmad1-Jul-20 8:35 
QuestionIntegration Pin
GauravSahu911-Jul-20 0:39
MemberGauravSahu911-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
MemberMember 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
MemberUser-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.
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 
QuestionSSL Error erroneous? Pin
GenJerDan15-Jun-20 0:45
MemberGenJerDan15-Jun-20 0:45 
Questiondiagram of gantt and java swing Pin
Member 1484673228-May-20 15:12
MemberMember 1484673228-May-20 15:12 
QuestionBuying a computer for development in Java ecosystem Pin
Member 1484653628-May-20 7:55
MemberMember 1484653628-May-20 7:55 
AnswerRe: Buying a computer for development in Java ecosystem Pin
Dave Kreskowiak28-May-20 15:54
mveDave Kreskowiak28-May-20 15:54 
AnswerRe: Buying a computer for development in Java ecosystem Pin
stonde202029-Jun-20 20:36
Memberstonde202029-Jun-20 20:36 
Questionplz, someone should help me to debug this error my i can asses my database but my next button is not working with it it keep getting this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Employees.Workers.btnNextActio Pin
Member 1484280524-May-20 22:47
MemberMember 1484280524-May-20 22:47 
AnswerRe: plz, someone should help me to debug this error my i can asses my database but my next button is not working with it it keep getting this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Employees.Workers.btnNextA Pin
Richard MacCutchan24-May-20 22:56
mveRichard MacCutchan24-May-20 22:56 
GeneralMessage Closed Pin
25-May-20 3:26
MemberMember 1484309125-May-20 3:26 
GeneralRe: plz, someone should help me to debug this error my i can asses my database but my next button is not working with it it keep getting this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Employees.Workers.btnNextA Pin
Richard MacCutchan25-May-20 4:38
mveRichard MacCutchan25-May-20 4:38 
GeneralRe: plz, someone should help me to debug this error ... Pin
Richard Deeming27-May-20 1:08
mveRichard Deeming27-May-20 1:08 
GeneralRe: plz, someone should help me to debug this error ... Pin
Richard MacCutchan27-May-20 1:50
mveRichard MacCutchan27-May-20 1:50 
QuestionJava 1st Year Project Pin
Member 1483530117-May-20 18:12
MemberMember 1483530117-May-20 18:12 
AnswerRe: Java 1st Year Project Pin
Richard MacCutchan17-May-20 21:05
mveRichard MacCutchan17-May-20 21:05 
QuestionReturn negative number as 0 Pin
and180y17-May-20 3:38
Memberand180y17-May-20 3:38 

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.