Answer To: tweet_data (5) Pos1459@Blackberry & @Facebook U R really about to make me throw this @Blackberry...
Valupadasu answered on Feb 09 2021
console output.png
Main.java
Main.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
public class Main {
public static void printHead(ArrayList list,int n) {
for(int idx = 0 ; idx < n ; idx++) {
System.out.println(list.get(idx).toString());
}
}
public static void printHead(Tweet[] list,int n) {
for(int idx = 0 ; idx < n ; idx++) {
System.out.println(list[idx].toString());
}
}
public static void prinTail(ArrayList list,int n) {
for(int idx = list.size() ; idx > list.size() - n ; idx--) {
System.out.println(list.get(idx-1).toString());
}
}
public static void prinTail(Tweet[] list,int n) {
list = Arrays.stream(list).filter(Objects::nonNull).toArray(Tweet[]::new); // since we created array dynamically , end of array we have have some null values so removed those
for(int idx = list.length ; idx > list.length - n ; idx--) {
System.out.println(list[idx-1].toString());
}
}
public static void main(String[] args) {
String fileName = "tweets_data.csv";
int tweetCnt = 5; // used to print first and last n tweets from tweets array and array lists
ArrayList tweetList = TweetLoader.loadAsArrayList(fileName);
Tweet[] tweetArr = TweetLoader.loadAsArray(fileName);
System.out.println("First "+tweetCnt +" tweets using array");
System.out.println("================================================");
printHead(tweetArr,tweetCnt);
System.out.println();
System.out.println("First "+tweetCnt +" tweets using array list");
System.out.println("================================================");
printHead(tweetList,tweetCnt);
System.out.println();
System.out.println("Last "+tweetCnt +" tweets using array list");
System.out.println("================================================");
prinTail(tweetList,tweetCnt);
System.out.println();
System.out.println("Last "+tweetCnt +" tweets using array");
System.out.println("================================================");
prinTail(tweetArr,tweetCnt);
}
}
Tweet.java
Tweet.java
public class Tweet {
private int uniqueId;
private String segmentCategory;
private String text;
public int getUniqueId() {
return uniqueId;
}
public void setUniqueId(int uniqueId) {
this.uniqueId = uniqueId;
}
public String getSegmentCategory() {
return segmentCategory;
}
public void setSegmentCategory(String segmentCategory) {
this.segmentCategory = segmentCategory;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Tweet(int uniqueId, String segmentCategory, String text) {
this.uniqueId = uniqueId;
this.segmentCategory = segmentCategory;
this.text = text;
}
public String toString() {
return this.uniqueId +" ("+this.segmentCategory+") : "+this.text;
}
}
TweetLoader.java
TweetLoader.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class TweetLoader {
public static ArrayList loadAsArrayList(String fileName) {
ArrayList tweetsData = new ArrayList<>();
BufferedReader csvReader = null;
try {
csvReader = new BufferedReader(new FileReader("src/" + fileName));
String row = null;
Tweet tweet = null;
while ((row = csvReader.readLine()) != null && !"".equals(row)) {
String tempStr[] = row.split("\\,");
tweet = new Tweet(Integer.parseInt(tempStr[1].trim()), tempStr[0].trim(), tempStr[2].trim());
tweetsData.add(tweet);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
csvReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return tweetsData;
}
public static Tweet[] loadAsArray(String fileName) {
int arraySize = 10;
Tweet[] tweetsData = new Tweet[arraySize];
BufferedReader csvReader = null;
int indexPos = 0;
try {
csvReader = new BufferedReader(new FileReader("src/" + fileName));
String row = null;
Tweet tweet = null;
while ((row = csvReader.readLine()) != null && !"".equals(row)) {
if (tweetsData.length == indexPos) {
tweetsData = Arrays.copyOf(tweetsData,tweetsData.length*2); // doubling the array size
}
String tempStr[] = row.split("\\,");
tweet = new Tweet(Integer.parseInt(tempStr[1].trim()), tempStr[0].trim(), tempStr[2].trim());
tweetsData[indexPos] = tweet;
indexPos++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
csvReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return tweetsData;
}
}
tweets_data.csv
Pos,1459,@Blackberry & @Facebook U R really about to make me throw this @Blackberry in the trash an get an @Apple iPhone! @Facebook upload issues!
Neg,2228,Pissed with whoever designs keyboards with @apple for not having a home and end key. working on the CLI i use those keys often
Pos,1400,My iPhone 4S battery lasted longer than a day. That hasn't happened since my edge iPhone. Nice job @apple.
Neg,1818,9% now on my second full charge of the day. Pissed @Apple
Neg,1652,Correction: @ Best Buy kudos to Chris @ Alamo Ranch S.A. TX-fixed issues couldn't resolve after 1/2 day w/ @ATT & @Apple. Hero of my day!
Neg,2354,Damn you @apple for breaking wififofum! It continued to work in ios 4. Was one of my most used apps. Damn Apple's overbearing policies!
Pos,1283,RT @MN2NOVA: Love ios5 Easter eggs. Pull down from middle top to bottom and see what pulls down. Awesome little feature! #ios5 @apple
Pos,1137,Lmfao look at the argument I had with Siri !!@ijustine @apple http://t.co/D4VjL7SI
Neg,1546,good article about why @apple fucked it all up with lion and their future. http://t.co/zNDP9Vr6 #fb
Pos,1270,Been off twitter for a few days as I smashed my iPhone but @apple were very nice and gave me a new one :)
Pos,1095,We're one of a few Featured Education Apps on the @Apple **Website** today sweet! http://t.co/0yWvbe1Z
Neg,1952,RT @CBM: Lies @apple. the battery on this new iPhone4S is definitely not any better.
Neg,2427,@apple thanks apple for ios 5 and losing all my contacts!
Neg,1612,removing all @apple shit.
Pos,1039,Lmao I think @apple is onto something magical! I am DYING!!! haha. Siri suggested where to find whores and where to hide a body lolol
Neg,2457,Also @EricGreenspan don't forget @Apple stop closing the app store after each bloody purchase...
Pos,1145,Save me from #HP's unwanted OS! Help me buy an #iPhone! I have seen the light! #lol http://t.co/8gUP9Acz #backchannel @apple
Pos,1015,I just realized that the reason I got into twitter was ios5 thanks @apple
Neg,2141,@apple why don't you guys test your upgrades before you put them out. 15 1/2 hrs u guys wasted of my time yesterday. Thanks.
Neg,1644,Total chaos at @apple store regent street. Like an Ethiopian feeding station. Can't believe this is same co. that makes all that cool shit.
Neg,1661,@APPLE Wow @MOTOROLA Just crushed your dreams....
Neg,1559,@paulens It surprises me that @Apple throws up an error alert about authorizing and there's no Authorize this computer button.
Neg,2386,@hbogo @apple - why don't you play nice and let me use HDMI from iPad? Pretty lame.
Pos,1063,Thank you @apple for Find My Mac - just located and wiped my stolen Air. #smallvictory #thievingbastards
Pos,1008,Hilarious @youtube video - guy does a duet with @apple 's Siri. Pretty much sums up the love affair! http://t.co/8ExbnQjY
Pos,1012,@RIM you made it too easy for me to switch to @Apple iPhone. See ya!
Neg,1758,Only thing bad about the new @canon camera is that it has two compact flash cards... not one of SD. SD goes in my @apple computer. Damn you!
Pos,1394,Impressive service @apple genius bar metro centre. Power cable replaced free n booked in for screen replacement for free :- D
Pos,1252,Sorry @BlackBerry I'm moving to @Apple.
Neg,2366,By far the worst experience upgrading out of any version of iOS @apple #iOS5
Neg,2304,If @apple doesn't fix iOS5 this week I'm buying a droid #emptythreats
Pos,1241,Yessss! I'm lovin the iPhone update especially the slide down bar at top of screen =) good job @Apple.
Pos,1463,#iOS5 update submitted to @apple! Thanks for all the support!
Neg,2165,Having major battery drain issue since updating iPhone 4 to iOS 5. Anyone else? @AppStore @iPhone @apple
Neg,2090,@Apple - why do I have to have apps I am updating in their original 'folder' location? That was painful.
Pos,1188,@apple loving the new IOS5 upgrade for the iPhone!
Neg,1965,Ok Hindi keyboard in #iOS5 is something to cheer about. But @apple what about support for 20+ missing Indian languages!? #FAIL
Pos,1021,The 16 strangest things Siri has said so far. I am SOOO glad that @Apple gave Siri a sense of humor! http://t.co/TWAeUDBp via @HappyPlace
Neg,2300,RT @MikeFerri: If @apple doesn't fix iOS5 this week I'm buying a droid #emptythreats
Neg,2318,The new iOS5 completely failed at upgrading the phone. Now I'm stuck in this restore loop. Thanks @apple for wasting my day. Can i undo?
Neg,1846,@apple u guys are gay
Neg,2126,but seriously @apple finance me a damn phone you greedy bastards! OR Santa...get your jolly ass in gear and get us a 4S - white plz
Pos,1386,I <3 @apple http://t.co/ondXWpEr
Pos,1361,Thank you @apple for your innovations. Exhibit A: Guy playing with Facetime instead of watching game at sports bar. http://t.co/oU7K39ge
Neg,2334,Ok @iTunesMusic idk wth is up but after syncing my phone my album art is out of order! WTF @Apple!
Neg,1834,i update to ios 5 and lose everything on my phone and it wont let me sign into my itunes account... thanks @Apple
Neg,2142,what is going on with all my @Apple products?! First my #iPad freezes (still is frozen) and then my #iPhone won't turn on!!! #techieTroubles
Neg,1815,I know a few others having same issue RT @Joelplane: 9% now on my second full charge of the day. Pissed @Apple
Neg,2453,Well @apple seems 2b screwing #iphone 4s resellars. They still dont have 4s. Yet were promised. Similar 2 Ma Bell's monopolistic prctices?
Neg,1937,@carlton858: I really hate dealing with the brain dead people at the @apple store. For such good products customer service sucks. in NZ?
Neg,2494,#Siri went down for a little while last night. Come on @apple! Get your shit together on this launch!
Pos,1036,RT @JamaicanIdler: Lmao I think @apple is onto something magical! I am DYING!!! haha. Siri suggested where to find whores and where to h ...
Neg,2080,@Apple would do better to inform customers properly about migrating to #iCloud - many complaints lead to #image damage http://t.co/QL7zmPpO
Neg,1905,@apple I committed to your cloud storage iDisk and now it's raining :-( so soon no more iDisk. Any plans for a new service for storage?
Pos,1185,#iCloud set up was flawless and works like a champ! To the Cloud @Apple
Pos,1324,i love this. so much. thank you @apple. http://t.co/Ui8lOEzX
Neg,1894,What was @apple thinking making #Siri totally dependent on a network connection? Siri + @ATT = utter frustration.
Neg,1721,@apple $319 to repair my iPad 2 Apple you've lost me and my $700 a year Android here I come!
Pos,1130,@bkad5161 than apologize to @apple ;)
Pos,1370,Thank you Steve @apple store 5th av. http://t.co/nSAisriP
Neg,2407,@amandbrar @apple Siri has worked for me less than 10% of the time. If service doesn't improve by Monday I'm returning my iPhone 4S.
Pos,1318,@SteveJobs being honored tonite @Apple...A truly great loss to the world.He will so be missed
Neg,2244,So glad that I paid $69 for this @apple care protection plan. Been on hold for over 20 minutes now. Great customer service. #NOT
Neg,1984,@philipgrey: dear @apple why you gotta go change the way <input type=number> is handled out of the blue?
Neg,1604,So apparently @apple put MB cap on your SMS with the new update. 25mb storage before it tells you your inbox is full. What is this 2001?
Neg,2376,Never purchasing another item from @apple.
Pos,1309,@alexlindsay My wife upgraded her iPhone 4. I think Siri alone is worth the upgrade. Looking forward to @Apple continuing to enhance Siri.
Pos,1047,Just registered as an @apple developer... Here's hoping I can actually do it... Any help greatly appreciated!
Pos,1105,<3 #ios5 @apple
Neg,1543,@apple AirDrop #fail - Immediate declined your request. every time
Neg,1593,Dear @apple My new Air is now a notbook since your update killed #wifi #bug #destroying #productivity
Pos,1215,So yeah... @apple #iOS5 #readinglists have changed my life. #nowicanspendevenmoretimeonmyphone.
Pos,1141,Incredible: 4 million iPhone 4Ss in 3 days. 135% better than the iPhone 4 http://t.co/1FMJxTMM @apple #iphone4s
Neg,2348,Asked #Siri Where's Baby Lisa? and was told sorry I'm having trouble connecting to the network right now. @apple server fail. #ios5
Neg,2492,Houston we have a problem!! My iPad has been restoring for 12+ hours after installing @apple IOS5. This can't be right....
Neg,1657,@phxguy88 @Apple @BGR That's why all the ppl who stand in line for hrs to get the newest model are suckers...
Neg,2314,After a week or so with @Apple's #iOS5 I'm considering going back to #jailbroken 4.2 - same features as 5 plus more. Not as stable though.
Neg,2202,RT @JDougyJ: hey @apple do you wanna fix my fucking playlist order you piece of shit
Neg,1678,@Apple on the #iPad with #iOS5 why has the Messages Icon been included when it can't be used?
Neg,1709,@Apple #iOS5 gm on ipad1 is very slow wash better on beta7/8 solve this problem or give me the ipad2
Neg,1928,Dear @apple: Why did all my PDFs and ePub files disappear from ibooks in my iPhone post ios5 upgrade? This hurts. #3GS #needsomethingtoread
Neg,1648,@FishMama: If you made a purchase just wait for the @apple survey! hate going b/c of the bad #custserv
Neg,1567,Oh @apple. Steve obviously had nothing to do with iPhoto as it's the perfect opposite of 'insanely great'. Get it fixed please.
Neg,1909,Hey @apple why can't I share a reminder list from my iPhone?Also why can't I login to the iCloud webpage to modify sharing from my phone?
Neg,2471,@apple #siri can't connect to the network -- it's LESS useful that VoiceControl now... :(
Neg,2005,@apple: Multiple times siri is having trouble connecting to the network. Siri needs servers (and some exercise!)
Neg,1868,@chascouponmom @apple I get they are busy w/ the new phone but I just wanted to buy a stylus. they made me wait forever outside & never
Pos,1306,Loving the new iPod update @apple
Neg,1754,Hey @Apple: stop sending your automatically-depreciate-all-iPhones-older-than-the-4s signal. My #iPhone4 is dying rapidly processor-wise.
Neg,1600,@apple thank you for ruining my 3GS with #iOS5. You've just turned my phone into an utterly useless pile of shit.
Neg,1766,why the fuck dose my phone decide its just ganna freeze every time i try to update it so fucking sick of @apple
Neg,2392,and only the first 23 images in my Photo Roll made it over? Seriously @apple how did you fuck this up so much?
Pos,1227,#10twitterpeopleiwouldliketomeet @coollike @TheGadgetShow @thelittleappkid @Jon4Lakers @BenRubery @Apple @twitter @FXhomeHitFilm (-2)
Neg,2255,@robert_terpstra no worries bud ps: they're saying that the 4s is having a lot of network issues right now. (I'm @apple for mac problems)
Neg,1829,iTunes is @apple's worst product. Worse than the #Newton or the hockey puck mouse. It's utterly painful to use.
Neg,1881,Hey @apple the SMS full message is complete shit. Yes I'm annoyed.
Pos,1292,Updated my iOS and started using cloud services. Pretty bad ass @apple my #iPhone 3GS still the champ.
Neg,2286,I sure as hell better be able to restore my calendar events. Terrible bug apple! @apple #icloud #anothericloudfail
Pos,1465,Awesome service from the @apple store in pc. Thanks chris!
Neg,2061,Spoke to an @Apple rep on phone & could hear her tv blaring in the bg. She said I can't actually help u just direct u back to the web
Neg,2128,Pondering the iOS update... From the sounds of @MikeFerri's difficulties I may just hold off :/ @Apple FIX IT!
Neg,2483,Dear @apple One day you're going to release a version of iTunes that doesn't freeze more often than it works. One day.
Pos,1210,Play on ma man. Loving the camera in the...