HW2.java - package HW2 import java.io import java.util import java.util.Map.Entry public class HW2 Prints all movies that occur in both lists public

HW2.java - package HW2 import java.io import java.util...

This preview shows page 1 - 2 out of 2 pages.

package HW2; import java.io.*; import java.util.*; import java.util.Map.Entry; public class HW2 { // Prints all movies that occur in both lists. public static void intersection(List<String> list1, List<String> list2) { Set<String> set = new HashSet<>(); for(String s1 : list1) { set.add(s1); } for(String s2 : list2) { if(set.contains(s2)) { System.out.println(s2); } } } // Prints all movies in the list that occur at least k times // (print the movie followed by the number of occurrences in parentheses). public static void frequent(List<String> list, int k) { Map<String, Integer> map = new HashMap<>(); for(String s : list) { if(map.containsKey(s)) { map.put(s, map.get(s) + 1); } else map.put(s, 1); } for(Map.Entry<String, Integer> entry : map.entrySet()) { String key = entry.getKey(); Integer value = entry.getValue(); if(value >= k) { System.out.println(key + " (" + value + ")"); } } } // Prints all movies in the list, grouped by number of characters. // All movies with the same number of characters are printed on the same line. // Movies with fewer characters are listed first. public static void groupByNumChars(List<String> list) { Map<Integer, ArrayList<String>> map = new TreeMap<>(); for(String s : list) {
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

  • Fall '19
  • Malcolm McDowell, public class hw2, public static void intersection

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes