java - How to Add Sequence to Names(If Duplicated) Stored in a List -


given following code:

import java.util.arraylist; import java.util.arrays; import java.util.collections; import java.util.linkedhashset; import java.util.list;  public class filenameseq {      public static void main(string[] args) {          //list containing duplicate names          list<string> al = new arraylist<string>(arrays.aslist("java", "java", "java", "cpp", "java", "cpp" ,"c"));         arraylist<string> filenamelist = new arraylist<>();          (int = 0; al.size() > 0;) {             int freq = collections.frequency(al, al.get(i));             string filename = al.get(i);             filenamelist.add(filename.concat("-").concat("" + freq));             al.remove(i); /* removing element */         }          system.out.println(filenamelist);     }  } 

i have developed piece of code generate sequence if there similar names stored in list, first occurrence of name should appended "-1" ,second "-2" , on till "-n"and if there no duplicate name should remains same. while running program getting following output

[java-4, java-3, java-2, cpp-2, java-1, cpp-1, c-1] 

but output should like

[java-4, java-3, java-2, cpp-2, java-1, cpp-1, c] 

c should not have "-1" appended it.

what changes should need make produce later output?

you have test there 1 occurrence in collection.

/**  * @param args  */ public static void main(string[] args) {          //list containing duplicate names          list<string> al = new arraylist<string>(arrays.aslist("java", "java", "java", "cpp", "java", "cpp" ,"c"));         arraylist<string> filenamelist = new arraylist<string>();         //creates copy of collection, remain unchanged         list<string> alcopy = new arraylist<string>(al);         (int = 0; al.size() > 0;) {             string filename = al.get(i);             int freq = collections.frequency(al,filename);             boolean toconcat = freq != 1;             if(!toconcat){                 //checks if there 1 occurrence of filename                  //or if last 1                 toconcat =( collections.frequency(alcopy,filename) != 1);             }             if(toconcat){                 filenamelist.add(filename.concat("-").concat("" + freq));                 //why not filenamelist.add(filename + "-" + freq)); ??             } else {                 filenamelist.add(filename);             }             al.remove(i); /* removing element */         }          system.out.println(filenamelist); } 

please note code generate output mentioned in question

[java-4, java-3, java-2, cpp-2, java-1, cpp-1, c]

if want output suggested :

[java-1, java-2, java-3, cpp-1, java-4, cpp-2, c]

you can use following code uses map count each occurrence of filename.

/**  * @param args  */ public static void main(string[] args) {          //list containing duplicate names          list<string> al = new arraylist<string>(arrays.aslist("java", "java", "java", "cpp", "java", "cpp" ,"c"));         arraylist<string> filenamelist = new arraylist<string>();         map<string, integer>counters = new hashmap<string, integer>();         for(string filename : al){             integer count = counters.get(filename);             boolean toconcat = true;             if(count == null){                 //first occurence. one?                 count = 0;                 if(collections.frequency(al,filename) == 1){                     toconcat = false;                 }             }             count += 1;             counters.put(filename, count);             if(toconcat){                 filenamelist.add(filename.concat("-").concat("" + count));                 //why not filenamelist.add(filename + "-" + count)); ??             } else {                 filenamelist.add(filename);             }         }         system.out.println(filenamelist); } 

note in case don't have modify original collection, cleaner.

edit

as others mentioned in answer first solution not effective because collection.frequency scans whole collection. in addition that, removing elements original collection not clean in opinion.

we can use code similar last example here above generate first output ([java-4, java-3, java-2, cpp-2, java-1, cpp-1, c-1]). based on map of counters in case put number of occurrences per filename , decrease counter on each occurrence instead of starting 0 , increasing counter.

public static void main(string[] args) {     //list containing duplicate names      list<string> al = new arraylist<string>(arrays.aslist("java", "java", "java", "cpp", "java", "cpp" ,"c"));     arraylist<string> filenamelist = new arraylist<string>();     map<string, integer>counters = new hashmap<string, integer>();     for(string filename : al){         integer count = counters.get(filename);         boolean toconcat = true;         if(count == null){             //first occurrence.              //the number appended filename number of occurrences              count = collections.frequency(al,filename);             //is one? if won't append filename             //beware count integer, not int '==' doesn't work              if(count.equals(1)){                 toconcat = false;             }         } else {             //we can decrease counter             count -= 1;         }         counters.put(filename, count);         if(toconcat){             filenamelist.add(filename.concat("-").concat("" + count));             //why not filenamelist.add(filename + "-" + count)); ??         } else {             filenamelist.add(filename);         }     }     system.out.println(filenamelist); } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -