Reputation: 1639
I have a simple problem. I have an ArrayList (of type String) with 100'000 Names. I want to create another ArrayList (of type Integer) which also has 100'000 Elements and assignes each Element of the String ArrayList an ID-Number. Equal Names should have equal ID Numbers assigned.
VERY BASIC EXAMPLE:
i have: (hans, max, hans, hans, frank)
i want: ( 1 , 2 , 1 , 1 , 3 )
I implemented a Solution which works, but is very slow (for my big dataset of 100'000 Names). I wonder someone can find a better/faster way of doing this. Thanks everyone for any tipps!
public static void main(String[] args) {
// initialize arraylists
ArrayList<String> Names = new ArrayList<String>();
ArrayList<Integer> Id = new ArrayList<Integer>();
// sample data // I want the integer Arraylist to have values:
Names.add("Hans"); // 1
Names.add("Max"); // 2
Names.add("Hans"); // 1
Names.add("Hans"); // 1
Names.add("Frank"); // 3
// my solution (works, but is slow and confusing)
int N = Names.size();
int ID_Count = 0;
for (int i=0; i<N; i++) {
boolean match_found = false;
String curr_Name = Names.get(i);
for (int check=0; check<i; check++) {
if (curr_Name.equals(Names.get(check))) {
Id.add(Id.get(check));
match_found = true;
break;
}
}
if (match_found==false) {
ID_Count++;
Id.add(ID_Count);
}
}
// show result
for (int i=0; i<N; i++) {
System.out.println(Id.get(i) + " " + Names.get(i));
}
}
Upvotes: 0
Views: 615
Reputation: 1189
This is a faster way of doing it:
public static void main(String[] args) {
// initialize arraylists
ArrayList<String> Names = new ArrayList<String>();
Map<String,Integer> map = new HashMap<>();
// sample data // I want the integer Arraylist to have values:
Names.add("Hans"); // 1
Names.add("Max"); // 2
Names.add("Hans"); // 1
Names.add("Hans"); // 1
Names.add("Frank"); // 3
int N = Names.size();
int id = 0;
for (int i = 0; i < N; i++) {
String name = Names.get(i);
if (map.get(name) == null) {
map.put(name,++id);
}
}
// show result
for (int i=0; i<N; i++) {
String name = Names.get(i);
System.out.println(map.get(name) + " " +name);
}
}
Upvotes: 1
Reputation: 708
If I understand your question correctly, you ought to use a HashMap, as it will be a lot faster/easier than working with two arrays. I would do it like this:
Map<String,Integer> map = new HashMap<>();
This way you will be able to do map.put("name", int id)
, and it won't allow duplicate keys (key is the "name" in your case) so you will automatically have only one key/value pair for names that are the same.
Upvotes: 0