Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: HashMap , trying to get the size of the value and NOT the keys.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default HashMap , trying to get the size of the value and NOT the keys.

    Hello guys , I am asked in my assignment to make a program that accepts a text file as an example a novel and i have to sort each word as a PERSON or ORGANIZATION or LOCATION or O as in Other , example :
    Microsoft/ORGANIZATION ,/O Nelly/PERSON !/O '/O
    Now we notice that microsoft is and organitzation and "," is Other and Nelly is a person's name and so on ..

    Now I am asked to return the numbers of tags in the text which is 4 in our case because we have (ORGANIZATION,PERSON,LOCATION,OTHER)

    My question here is my logic true ? And since i made a map of String,String ; Is there any way that i can get the size of the values in our case the values are the organization etc.. ??
    Thank you.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;
     
    public class NovelDataMining {
     
    	/**
    	 * @param args
    	 * @throws FileNotFoundException
    	 */
    	public static void main(String[] args) throws FileNotFoundException {
    		Scanner console = new Scanner(new File(
    				"C:\\Users\\User\\AppData\\Local\\Temp\\novel.txt"));
    System.out.println(numberOfTags(console));
     
    	}
     
    	public static int numberOfTags(Scanner console) {
    		Map<String, String> map = new HashMap<String, String>();
    		while (console.hasNext()) {
    			String nextWord = console.next();
    			if(nextWord.substring(nextWord.length()-1, nextWord.length()).equalsIgnoreCase("O"))// Here we are checking if the String is of Other Type
    				map.put(nextWord, "O");
    			if(nextWord.substring(nextWord.length()-1, nextWord.length()).equalsIgnoreCase("PERSON"))
    				map.put(nextWord, "PERSON");
    			if(nextWord.substring(nextWord.length()-1, nextWord.length()).equalsIgnoreCase("ORGANIZATION"))
    				map.put(nextWord, "ORGANIZATION");
    			if(nextWord.substring(nextWord.length()-1, nextWord.length()).equalsIgnoreCase("LOCATION"))
    				map.put(nextWord, "LOCATION");
    			for (String word : map.keySet()){
     
    			}
    		}
    		return 0;// just to remove the compliation error
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    way that i can get the size of the values
    What is the "size" of a value?

    Should the value returned by the put() method be used?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    Thank you for your reply,
    What I meant is that in my assignment the first question asked is " what is the number of the tags of the novel ?" and by tags i mean " Location " or "Person"...
    The keys of the map are the words from the novel , and the value are the tags.
    Your text file will look like this :

    I/O ,/O am/O a/O table/OBJECT microsoft/ORGANIZATION george/PERSON !/O

    I hope you understood what is my question here , let me know if not i will explain more.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    Sorry, I still don't understand what you mean by "size". Can you give some examples?
    For example:
    "adf" has a length of 3
    {1, 2, 3, 4} has a length of 4
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    It is very simple , map.size() will give us the length of key from the HashMap , i just want the length of the values not the keys.

    Here i sketched this :Untitled.jpg

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    want the length of the values not the keys.
    Are you asking for how many (a count) values there are in a HashMap?
    There is one value for each key.
    So if you know the number of keys, there will be that number of values.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    The length of the keys in the picture that i attached is 6 , but the length or ( count ) of the values is 4 .
    If you use map.size(); a value of 6 will be returned .
    What can i code or what can i do to get the value of 4 ? Should i make a for loop that iterates over the value and increment each time or how ?!
    This is getting frustrating I have no other ways of explanation honestly i give up

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    I think the number of values matches the number of keys. The keys will be unique, but it is possible for there to be duplicate values. In fact all the values could be the same so that there would only be one unique value.
    A Map doesn't care about the values. If you want unique values, use a Set.

    Can you make a small, simple program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    Take a look at this example code:
    		Object a = new Object();
    		Object b = new Object();
     
    		Map<String, Object> map = new HashMap<>();
    		map.put("1", a);
    		map.put("2", a);
    		map.put("3", a);
    		map.put("4", a);
    		map.put("5", b);
    		map.put("6", b);
    		map.put("7", b);
    		map.put("8", b);
    		map.put("9", b);
     
    		Set<Object> set = new HashSet<>(map.values());
     
    		System.out.println(set.size()); // => 2
    This is one way to get the number of unique values inside a map.
    There are probably many ways to get this and possibly more effective ways, but this is a very simple solution.

  10. The Following User Says Thank You to Cornix For This Useful Post:

    Jad_Asmar (April 5th, 2014)

  11. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    Thank you , thank you , thank you.
    You sir have made my day !

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap , trying to get the size of the value and NOT the keys.

    What about the case where adding a new key replaces an existing key?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. disabling keys
    By game06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 4th, 2013, 02:15 PM
  2. [SOLVED] quick keys
    By Gerardgrundy in forum Java IDEs
    Replies: 7
    Last Post: November 5th, 2012, 03:59 PM
  3. Jpanel preffered size exceed the nactual screen size
    By manish.ctae@gmail.com in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2012, 01:29 AM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  5. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM

Tags for this Thread