Erinevus lehekülje "ITI0011RUS:task 05" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
15. rida: 15. rida:
  
 
<source lang="java">
 
<source lang="java">
 +
import java.util.ArrayList;
 +
import java.util.HashMap;
 +
import java.util.HashSet;
 +
import java.util.List;
 +
import java.util.Map;
 +
import java.util.Set;
 +
 
public class CollectionTask {
 
public class CollectionTask {
 
 
 
public static String text = "Garbage Collection in Java\r\n" +
+
/**
"\r\n" +
+
* The function should read the contents of the file
"This is the first in a series of posts about Garbage Collection (GC). " +
+
* designated by the filename and return its contents
"I hope to be able to cover a bit of theory and all the major collectors in the hotspot virtual machine over the course of the series. This post just explains what garbage collection is and elements common to different collectors.\r\n" +
+
* as a string.
"\r\n" +
+
* @param filename - A file name to read.
"Why should I care?\r\n" +
+
* @return null if file is inaccessible (cannot be read or does not exist),
"\r\n" +
+
*        a string containing file contents otherwise.
"Your Java virtual machine manages memory for you – which is highly convenient – but it might not be optimally tuned by default. By understanding some of the theory behind garbage collection you can more easily tune your collector. A common concern is collector efficiency, that is to say how much time your program spends executing program code rather than collecting garbage. Another common concern is long that application pauses for.\r\n" +
+
*/
"\r\n" +
+
public static String readFile(String filename) {
"There’s also a lot of hearsay and folklore out there about garbage collection and so understanding the algorithms in a bit more detail really helps avoid falling into common pitfalls and traps. Besides – for anyone interested in how computer science principles are applied and used, JVM internals are a great thing to look at.\r\n" +
+
String text = "";
"\r\n" +
+
"What does stop-the-world mean?\r\n" +
+
return text;
"\r\n" +
+
}
"Your program (or mutator in GC-Speak) will be allocating objects as it runs. At some point your heap needs to be collected and all of the collectors in hotspot pause your application. The term ‘stop-the-world’ is used to mean that all of the mutator’s threads are paused.\r\n" +
+
"\r\n" +
+
/**
"Its possible to implement a garbage collector that doesn’t need to pause. Azul have implemented an effectively pauseless collector in their Zing virtual machine. I won’t be covering how it works but there’s a really interesting whitepaper if you want to know more.\r\n" +
+
* The function returns a list containing
"\r\n" +
+
* all the words from the input string.  
"The Young/Weak Generational Hypothesis\r\n" +
+
* @param text - an input string.
"\r\n" +
+
* @return null, if the string is not supplied,
"Simply stated: Most allocated objects die young 1. This concept was demonstrated by empirically analysing the memory allocation and liveness patterns of a large group of programs during the 1980s. What researchers found was that not only do most objects die young but once they live past a certain age they tend to live for a long time. The graph below is taken from a SUN/Oracle study looking at the lifespan of objects as a histogram.\r\n" +
+
*        an empty list, if the string contains no words,
"\r\n" +
+
*        a list of words otherwise.
"190245\r\n" +
+
*/
"\r\n" +
+
public static List<String> getWords(String text) {
"How is Heap organised?\r\n" +
+
List<String> words = new ArrayList<String>();
"\r\n" +
+
"The young generational hypothesis has given rise to the idea of generational garbage collection in which heap is split up into several regions, and the placement of objects within each region corresponds to their age. One element that is common to the above these garbage collectors (other than G1) is the way that heap is organised into different spaces.\r\n" +
+
// TODO Enter your code here
"\r\n" +
+
"java-memory\r\n" +
+
return words;
"\r\n" +
+
}
"When objects are initially allocated, if they fit, they are stored in the Eden space. If the object survives a collection then it ends up in a survivor space. If it survives a few times (your tenuring threshold) then the object ends up in the tenured space. The specifics of the algorithms for collecting these spaces differs by collector, so I’ll be covering them seperately in a future blog post.\r\n" +
 
"\r\n" +
 
"This split is beneficial because it allows you to use different algorithms on different spaces. Some GC algorithms are more efficient if most of your objects are dead and some are more efficient if most of your objects are alive. Due to the generational hypothesis usually when it comes time to collect most objects in Eden and survivor spaces are dead, and most objects in tenured are alive.\r\n" +
 
"\r\n" +
 
"There is also the permgen – or permanent generation. This is a special generation that holds objects that are related to the Java language itself. For example information about loaded classes is held here. Historically Strings that were interened or were constants were also held here. The permanent generation is being removed in favour of metaspace.\r\n" +
 
"\r\n" +
 
"Multiple Collectors\r\n" +
 
"\r\n" +
 
"The hotspot virtual machine actually has a variety of different Garbage Collectors. Each has a different set of performance characteristics and is more (or less) suited for different tasks. The key Garbage Collectors that I’ll be looking at are:\r\n" +
 
"\r\n" +
 
"Parallel Scavenge (PS): the default collector in recently released JVMs. This stops-the-world in order to collect, but collects in parallel (ie using multiple threads).\r\n" +
 
"Concurrent Mark Sweep (CMS): this collector has several phases, some of which stop the world, but runs concurrently with the program for several of its phases as well.\r\n" +
 
"Incremental Concurrent Mark Sweep (iCMS): a variant of CMS designed for lower pauses. It sometimes achieves this!\r\n" +
 
"Garbage First (G1): a newish collector that’s recently become more stable and is in slowly increasing usage.\r\n" +
 
"Conclusions\r\n" +
 
"\r\n" +
 
"I’ve given a few introductory points of thought about garbage collection, in the next post I’ll be covering the Parallel Scavenge collector – which is currently the default collector. I’d also like to provide a link to my employer who have a GC log analyser which we think is pretty useful.\r\n" +
 
"\r\n" +
 
"“hotspot” is the name given to the codebase common behind openjdk and the official Oracle JVM. As of Java 7 openjdk is the reference implementation for Java SE.\r\n" +
 
"Technically what I described above is the ‘weak generational hypothesis’ which has empirical validation. There’s also a strong variant which can be stated as The mean lifetime of a heap allocated object is equal to the mean amount of reachable storage. This is actually mathematically provable by taking Little’s Law and setting Λ to 1. Simple proof!\r\n" +
 
"I’ll cover the way heap is organised within G1 on a G1-specific blog post.";
 
 
 
 +
/**
 +
* The function returns the set containing only
 +
* unique words from the input string.
 +
* @param text - an input string
 +
* @return null, if the string is not supplied,
 +
*        an empty set, if the string contains no words,
 +
*        a set of unique words otherwise.
 +
*/
 +
public static Set<String> getUniqueWords(String text) {
 +
Set<String> unique_words = new HashSet<String>();
 +
 +
// TODO Enter your code here
 +
 +
return unique_words;
 +
}
 
 
public static void main(String[] args) {
 
// TODO Auto-generated method stub
 
System.out.print(text);
 
  
 +
/**
 +
* The function counts how many times each word
 +
* can be found in the text and saves this
 +
* information in the Map object, where the key is
 +
* the word, and the value is the amount of times
 +
* the considered word can be found in the text.
 +
* @param text - an input string
 +
* @return null, if the string is not supplied,
 +
*              an empty set, if the string contains no words,
 +
*              a map of words otherwise.
 +
*/
 +
public static Map<String,Integer> getWordCount(String text) {
 +
Map<String,Integer> wordCount = new HashMap<String,Integer>();
 +
 +
// TODO Enter your code here
 +
 +
return wordCount;
 
}
 
}
 
 
+
/**
 +
* The main function should print out
 +
* the result of the getWordCount() method.
 +
* @param args - input parameters.
 +
*/
 +
public static void main(String[] args) {
 +
// TODO Enter your code here
 +
}
 +
 
 
}
 
}
 
</source>
 
</source>

Redaktsioon: 24. veebruar 2015, kell 23:24

Срок сдачи упражнения 9-е занятие (4-е марта).

Общая информация об упражнениях: ITI0011RUS_Practice.
Обратно на страницу предмета.

Описание

1. Прочитать текст из переменной text. Занести в ArrayList все слова.

2. Из полученного списка слов выбрать уникальные (ArrayList -> Set).

3. Для каждого слова посчитать сколько раз оно встречается. (ArrayList -> Map).

Шаблон

<source lang="java"> import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set;

public class CollectionTask {

/** * The function should read the contents of the file * designated by the filename and return its contents * as a string. * @param filename - A file name to read. * @return null if file is inaccessible (cannot be read or does not exist), * a string containing file contents otherwise. */ public static String readFile(String filename) { String text = "";

return text; }

/** * The function returns a list containing * all the words from the input string. * @param text - an input string. * @return null, if the string is not supplied, * an empty list, if the string contains no words, * a list of words otherwise. */ public static List<String> getWords(String text) { List<String> words = new ArrayList<String>();

// TODO Enter your code here

return words; }

/** * The function returns the set containing only * unique words from the input string. * @param text - an input string * @return null, if the string is not supplied, * an empty set, if the string contains no words, * a set of unique words otherwise. */ public static Set<String> getUniqueWords(String text) { Set<String> unique_words = new HashSet<String>();

// TODO Enter your code here

return unique_words; }


/** * The function counts how many times each word * can be found in the text and saves this * information in the Map object, where the key is * the word, and the value is the amount of times * the considered word can be found in the text. * @param text - an input string * @return null, if the string is not supplied, * an empty set, if the string contains no words, * a map of words otherwise. */ public static Map<String,Integer> getWordCount(String text) { Map<String,Integer> wordCount = new HashMap<String,Integer>();

// TODO Enter your code here

return wordCount; }

/** * The main function should print out * the result of the getWordCount() method. * @param args - input parameters. */ public static void main(String[] args) { // TODO Enter your code here }

} </source>