Results 1 to 4 of 4
  1. #1
    peliasrojo is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Collections java

    Hi, i'm trying to do a program that memorizes the elements formed by a word and a number. For example, if I write ABB 3, AB 2, ABB -2, ABC 5, ZZ 2 the program will write:

    ABB 1 (the result of abb (3-2))
    ABC 5
    AB 2
    ZZ 2

    Also, should write:

    2 AB ZZ
    5 ABC
    1 ABB

    I've tried to do it with a TreeMap but i don't know how it works.

    I hope you can help me. Thanks.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Collections java

    Not sure I understand how the data is saved in the map.
    Is the key the number and the data is a list of the Strings with that number?
    With the added complication that the number is computed first: (3-2)
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Collections java

    You need two maps, one is a Map<String, Integer> which stores the key and the running total and the other one is a Map<Integer, List<String>> that can be build after the first map has been built; it stores the total from the fist Map as its key and a list of Strings (the keys in the fist map).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    peliasrojo is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Re: Collections java

    Quote Originally Posted by JosAH View Post
    You need two maps, one is a Map<String, Integer> which stores the key and the running total and the other one is a Map<Integer, List<String>> that can be build after the first map has been built; it stores the total from the fist Map as its key and a list of Strings (the keys in the fist map).

    kind regards,

    Jos
    Thanks you both. I've used two maps and all ok.
    Java Code:
    import java.util.Map;
    import java.util.TreeMap;
    
    public class Ejercicio3 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		// TODO Auto-generated method stub
    				acm.io.IODialog dialog=new acm.io.IODialog();
    				TreeMap<String, Integer> map=new TreeMap<String, Integer>();
    				TreeMap<Integer,String> map1=new TreeMap<Integer, String>();
    				Integer sumas =0;
    				
    				while (dialog.readBoolean("Continuar?")){
    					String aux=dialog.readLine("Cadena");
    					Integer s=dialog.readInt("Número");
    					//------------------------------
    					 	if(map.containsKey(aux)){
    					 		sumas=s+map.get(aux);
    					 		} 
    					 	else{
    					 		sumas=s;
    					 		}
    					 	map.put(aux, sumas);
    					//-----------------------------
    					 	if(map1.containsKey(s)){
    					 		aux=aux+" "+map1.get(s);
    					 		
    					 			}
    					 	map1.put(s, aux);
    				}
    				//-----------------------------
    				//-----------------------------
    			System.out.println("SUMA DE LAS PALABRAS");
    		     for (Map.Entry<String, Integer> entry: map.entrySet()){
    		    	 String w=entry.getKey();
    		    	 Integer v=entry.getValue();
    		    	 System.out.println(w +" "+v);
    		    	 
    		     }
    		   //-----------------------------
    		   //-----------------------------
    		    System.out.println("NÚMEROS DE LAS PALABRAS");
    		    for (Map.Entry<Integer, String> entry:map1.entrySet()){
    		    String R=entry.getValue();
    		    Integer T=entry.getKey();
    		    	 System.out.println(T +" "+R);
    		     }
    	}
    
    }

Similar Threads

  1. Java collections maps and lists
    By Razorfc in forum New To Java
    Replies: 1
    Last Post: 04-11-2012, 10:14 AM
  2. java collections framework
    By mamoonrizwan in forum New To Java
    Replies: 1
    Last Post: 11-27-2011, 06:19 PM
  3. java collections
    By hemanth123 in forum New To Java
    Replies: 1
    Last Post: 10-24-2011, 09:00 AM
  4. Java collections
    By learn2java in forum Advanced Java
    Replies: 6
    Last Post: 05-01-2011, 04:12 AM
  5. Generic Collections---Dysfuntional Java
    By beezerbutt in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •